1 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | OgreDepthShadowMapRenderingRun::OgreDepthShadowMapRenderingRun(OgreSharedRuns* sharedRuns,
|
---|
6 | String name,
|
---|
7 | Light* light,
|
---|
8 | unsigned int resolutionX,
|
---|
9 | unsigned int resolutionY,
|
---|
10 | String materialName
|
---|
11 | )
|
---|
12 | :DepthShadowMapRenderingRun(resolutionX, resolutionY)
|
---|
13 | , OgreRenderingRun(1, 1)
|
---|
14 | , RenderingRun(1, 1)
|
---|
15 | {
|
---|
16 | this->light = light;
|
---|
17 | this->sharedRuns = sharedRuns;
|
---|
18 | this->name = name;
|
---|
19 | this->blurredname = name + "blurred";
|
---|
20 |
|
---|
21 | this->materialName = materialName;
|
---|
22 |
|
---|
23 | createDepthMap();
|
---|
24 | }
|
---|
25 |
|
---|
26 | void OgreDepthShadowMapRenderingRun::createDepthMap()
|
---|
27 | {
|
---|
28 |
|
---|
29 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name,
|
---|
30 | "default",
|
---|
31 | TEX_TYPE_2D,
|
---|
32 | resolutionX,
|
---|
33 | resolutionY,
|
---|
34 | 0,
|
---|
35 | 0,
|
---|
36 | PF_FLOAT16_RGBA,
|
---|
37 | TU_RENDERTARGET);
|
---|
38 | depthMapTexture = texPtr.getPointer();
|
---|
39 | depthMapCamera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
|
---|
40 | //add viewport to rendertarget
|
---|
41 | HardwarePixelBuffer* hpb = (depthMapTexture->getBuffer()).getPointer();
|
---|
42 | RenderTarget* rt = hpb->getRenderTarget();
|
---|
43 | Viewport* v = rt->addViewport(depthMapCamera);
|
---|
44 | v->setOverlaysEnabled(false);
|
---|
45 | rt->setAutoUpdated(false);
|
---|
46 |
|
---|
47 | texPtr = Ogre::TextureManager::getSingleton().createManual(blurredname,
|
---|
48 | "default",
|
---|
49 | TEX_TYPE_2D,
|
---|
50 | resolutionX,
|
---|
51 | resolutionY,
|
---|
52 | 6,
|
---|
53 | 0,
|
---|
54 | PF_FLOAT16_RGBA,
|
---|
55 | TU_RENDERTARGET);
|
---|
56 | blurredDepthMapTexture = texPtr.getPointer();
|
---|
57 | hpb = (blurredDepthMapTexture->getBuffer()).getPointer();
|
---|
58 | rt = hpb->getRenderTarget();
|
---|
59 | v = rt->addViewport(depthMapCamera);
|
---|
60 | v->setOverlaysEnabled(false);
|
---|
61 | rt->setAutoUpdated(false);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum)
|
---|
65 | {
|
---|
66 | refreshLight(frameNum);
|
---|
67 |
|
---|
68 | if(light->getType() == Light::LT_POINT)
|
---|
69 | {
|
---|
70 | updateDepthMap();
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | updateDepthMap();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void OgreDepthShadowMapRenderingRun::updateDepthCubeFace(int facenum)
|
---|
79 | {
|
---|
80 | }
|
---|
81 |
|
---|
82 | void OgreDepthShadowMapRenderingRun::updateDepthMap()
|
---|
83 | {
|
---|
84 | setMaterialForVisibles(materialName, depthMapCamera, false);
|
---|
85 | RenderTarget* rt = depthMapTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
86 | Viewport* vp = rt->getViewport(0);
|
---|
87 |
|
---|
88 | rt->update();
|
---|
89 | restoreMaterials();
|
---|
90 |
|
---|
91 | rt = blurredDepthMapTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
92 | Material* mat = (Material*) MaterialManager::getSingleton().getByName("GameTools/Blur").getPointer();
|
---|
93 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(depthMapTexture->getName());
|
---|
94 | renderFullscreenQuad("GameTools/Blur", rt);
|
---|
95 |
|
---|
96 |
|
---|
97 | //rt->writeContentsToFile("shadowmap1.dds");
|
---|
98 |
|
---|
99 | //depthMapTexture->();
|
---|
100 | }
|
---|
101 |
|
---|
102 | void setnear()
|
---|
103 | {
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | void OgreDepthShadowMapRenderingRun::refreshLight(unsigned long frameNum)
|
---|
108 | {
|
---|
109 |
|
---|
110 | if(light!= 0)
|
---|
111 | {
|
---|
112 | if(light->getType() == Light::LT_DIRECTIONAL)
|
---|
113 | {
|
---|
114 | Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
115 |
|
---|
116 | OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*)
|
---|
117 | OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun();
|
---|
118 | frun->setCameraMatrices(viewcam->getViewMatrix(), viewcam->getProjectionMatrix());
|
---|
119 |
|
---|
120 | Vector3 lightpos = light->getPosition();
|
---|
121 | Vector3 lightdir = light->getDirection();
|
---|
122 | lightdir.normalise();
|
---|
123 |
|
---|
124 | Vector3 min;
|
---|
125 | Vector3 max;
|
---|
126 |
|
---|
127 | bool uselispsm = true;
|
---|
128 | if(uselispsm)
|
---|
129 | {
|
---|
130 | Camera* maincam = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
131 | Vector3 viewdir = maincam->getDirection();
|
---|
132 | viewdir.normalise();
|
---|
133 | Vector3 campos = maincam->getPosition();
|
---|
134 | float nearclip = maincam->getNearClipDistance();
|
---|
135 |
|
---|
136 | float dotprod = viewdir.dotProduct(lightdir);
|
---|
137 | float sinGamma = sqrt(1.0 - dotprod * dotprod);
|
---|
138 |
|
---|
139 | Vector3 left = lightdir.crossProduct(viewdir);
|
---|
140 | Vector3 up = left.crossProduct(lightdir);
|
---|
141 | up.normalise();
|
---|
142 | //left = lightdir.crossProduct(up);
|
---|
143 | left.normalise();
|
---|
144 | Matrix4 viewMatrix( left.x, left.y, left.z, 0,
|
---|
145 | up.x, up.y, up.z, 0,
|
---|
146 | lightdir.x, lightdir.y, lightdir.z, 0,
|
---|
147 | 0, 0, 0, 1);
|
---|
148 |
|
---|
149 | frun->setLightMatrix(viewMatrix);
|
---|
150 | OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP,
|
---|
151 | frameNum);
|
---|
152 | frun->getMinMax(min, max);
|
---|
153 |
|
---|
154 | float n;
|
---|
155 | float f;
|
---|
156 | float d = abs(max.y - min.y);
|
---|
157 |
|
---|
158 | float z_n = nearclip / sinGamma;
|
---|
159 | float one_min_singamma = (1 - sinGamma);
|
---|
160 | float fact = d * pow(one_min_singamma, 2);
|
---|
161 | float z_f = z_n + d * sinGamma * fact;
|
---|
162 | n = (z_n + sqrt(z_f * z_n)) / sinGamma;
|
---|
163 | ///n = 100000;
|
---|
164 | f = n + d;
|
---|
165 |
|
---|
166 | Vector3 pos = campos - up * (n - nearclip);
|
---|
167 | Matrix4 newViewMatrix( left.x, left.y, left.z, -left.dotProduct(pos),
|
---|
168 | up.x, up.y, up.z, -up.dotProduct(pos),
|
---|
169 | lightdir.x, lightdir.y, lightdir.z, -lightdir.dotProduct(pos),
|
---|
170 | 0, 0, 0, 1);
|
---|
171 |
|
---|
172 | Matrix4 lispMat = Matrix4::IDENTITY;
|
---|
173 |
|
---|
174 | lispMat[1][1] = f / (f - n);
|
---|
175 | lispMat[1][3] = f * n / (f - n);
|
---|
176 | lispMat[3][1] = 1;
|
---|
177 | lispMat[3][3] = 0;
|
---|
178 |
|
---|
179 | depthMapCamera->setCustomProjectionMatrix(true, Matrix4::IDENTITY);
|
---|
180 | depthMapCamera->setCustomViewMatrix(true, lispMat * newViewMatrix);
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | depthMapCamera->setCustomProjectionMatrix(true, Matrix4::IDENTITY);
|
---|
185 | depthMapCamera->setCustomViewMatrix(false);
|
---|
186 | depthMapCamera->setPosition(lightpos);
|
---|
187 | depthMapCamera->setDirection(lightdir);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | Matrix4 lightMatrix = depthMapCamera->getViewMatrix();
|
---|
192 | frun->setLightMatrix(lightMatrix);
|
---|
193 |
|
---|
194 | OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP,
|
---|
195 | frameNum);
|
---|
196 |
|
---|
197 | frun->getMinMax(min, max);
|
---|
198 |
|
---|
199 |
|
---|
200 | Vector3 scale = (max - min);
|
---|
201 | scale *= 1.5; // just for safety
|
---|
202 | scale.z = 200; //TODO: get scene bounding box to set z scale
|
---|
203 | Matrix4 projection = Matrix4::IDENTITY;
|
---|
204 | projection.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, -2.0 / scale.z));
|
---|
205 | projection.setTrans(Vector3(-(max.x + min.x) / scale.x,
|
---|
206 | -(max.y + min.y) / scale.y,
|
---|
207 | -(max.z + min.z) / scale.z));
|
---|
208 | depthMapCamera->setCustomProjectionMatrix(true, projection);
|
---|
209 | }
|
---|
210 | else if(light->getType() == Light::LT_SPOTLIGHT)
|
---|
211 | {
|
---|
212 | /* Vector3 dir = light->getDirection();
|
---|
213 | depthMapCamera->setDirection( dir );
|
---|
214 | Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
215 | depthMapCamera->setPosition(pos);
|
---|
216 | depthMapCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
217 | depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/
|
---|
218 | }
|
---|
219 | else//point light
|
---|
220 | {
|
---|
221 | /* Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
222 | Vector3 dir = -pos;
|
---|
223 | depthMapCamera->setDirection( dir );
|
---|
224 | depthMapCamera->setPosition(pos);
|
---|
225 | depthMapCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
226 | depthMapCamera->setFOVy(Radian(Degree(140)));
|
---|
227 | depthMapCamera->setNearClipDistance(1);
|
---|
228 | // depthMapCamera->setFarClipDistance(200);
|
---|
229 |
|
---|
230 | //OGRE_EXCEPT(0, "NOT implemented for Pointlight", "OgreDepthShadowMapRenderingRun::refreshLight");
|
---|
231 | */
|
---|
232 | /*Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
233 | Vector3 dir = -pos;
|
---|
234 | depthMapCamera->setDirection( dir );
|
---|
235 | depthMapCamera->setPosition(pos);
|
---|
236 | depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
237 | depthMapCamera->*/
|
---|
238 |
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix()
|
---|
244 | {
|
---|
245 | return depthMapCamera->getViewMatrix();
|
---|
246 | }
|
---|
247 |
|
---|
248 | Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix()
|
---|
249 | {
|
---|
250 | return depthMapCamera->getProjectionMatrixWithRSDepth() * depthMapCamera->getViewMatrix();
|
---|
251 | }
|
---|
252 |
|
---|