1 | #include "ShadowMapping.h"
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "RenderState.h"
|
---|
4 | #include "RenderTraverser.h"
|
---|
5 | #include "Light.h"
|
---|
6 | #include "Polygon3.h"
|
---|
7 | #include "Polyhedron.h"
|
---|
8 |
|
---|
9 | #include <IL/il.h>
|
---|
10 | #include <assert.h>
|
---|
11 |
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 |
|
---|
16 | namespace CHCDemoEngine
|
---|
17 | {
|
---|
18 |
|
---|
19 | static CGprogram sCgShadowProgram;
|
---|
20 | static CGparameter sShadowParam;
|
---|
21 |
|
---|
22 |
|
---|
23 | static Polyhedron *polyhedron = NULL;
|
---|
24 | static Polyhedron *lightPoly = NULL;
|
---|
25 | static Vector3 dummyPt;
|
---|
26 | static Matrix4x4 dummyMat;
|
---|
27 |
|
---|
28 |
|
---|
29 | static void PrintGLerror(char *msg)
|
---|
30 | {
|
---|
31 | GLenum errCode;
|
---|
32 | const GLubyte *errStr;
|
---|
33 |
|
---|
34 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
35 | {
|
---|
36 | errStr = gluErrorString(errCode);
|
---|
37 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix, const AxisAlignedBox3 &sceneBox)
|
---|
43 | {
|
---|
44 | Frustum frustum(lightMatrix);
|
---|
45 |
|
---|
46 | vector<Plane3> clipPlanes;
|
---|
47 |
|
---|
48 | for (int i = 0; i < 6; ++ i)
|
---|
49 | {
|
---|
50 | ////////////
|
---|
51 | //-- normalize the coefficients
|
---|
52 |
|
---|
53 | // the clipping planes look outward the frustum,
|
---|
54 | // so distances > 0 mean that a point is outside
|
---|
55 | const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal);
|
---|
56 |
|
---|
57 | frustum.mClipPlanes[i].mD *= invLength;
|
---|
58 | frustum.mClipPlanes[i].mNormal *= invLength;
|
---|
59 |
|
---|
60 | //clipPlanes.push_back(frustum.mClipPlanes[i]);
|
---|
61 | }
|
---|
62 |
|
---|
63 | clipPlanes.push_back(frustum.mClipPlanes[5]);
|
---|
64 | clipPlanes.push_back(frustum.mClipPlanes[0]);
|
---|
65 | clipPlanes.push_back(frustum.mClipPlanes[1]);
|
---|
66 | clipPlanes.push_back(frustum.mClipPlanes[2]);
|
---|
67 | clipPlanes.push_back(frustum.mClipPlanes[3]);
|
---|
68 | clipPlanes.push_back(frustum.mClipPlanes[4]);
|
---|
69 |
|
---|
70 | return Polyhedron::CreatePolyhedron(clipPlanes, sceneBox);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | static void GrabDepthBuffer(float *data, GLuint depthTexture)
|
---|
75 | {
|
---|
76 | glEnable(GL_TEXTURE_2D);
|
---|
77 | glBindTexture(GL_TEXTURE_2D, depthTexture);
|
---|
78 |
|
---|
79 | glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
|
---|
80 |
|
---|
81 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
82 | glDisable(GL_TEXTURE_2D);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | static void ExportDepthBuffer(float *data, int size)
|
---|
87 | {
|
---|
88 | ilInit();
|
---|
89 | assert(ilGetError() == IL_NO_ERROR);
|
---|
90 |
|
---|
91 | ILstring filename = ILstring("shadow.tga");
|
---|
92 | ilRegisterType(IL_FLOAT);
|
---|
93 |
|
---|
94 | const int depth = 1;
|
---|
95 | const int bpp = 1;
|
---|
96 |
|
---|
97 | if (!ilTexImage(size, size, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
|
---|
98 | {
|
---|
99 | cerr << "IL error " << ilGetError() << endl;
|
---|
100 |
|
---|
101 | ilShutDown();
|
---|
102 | assert(ilGetError() == IL_NO_ERROR);
|
---|
103 |
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (!ilSaveImage(filename))
|
---|
108 | {
|
---|
109 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
110 | }
|
---|
111 |
|
---|
112 | ilShutDown();
|
---|
113 | assert(ilGetError() == IL_NO_ERROR);
|
---|
114 |
|
---|
115 | cout << "exported depth buffer" << endl;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 | static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &m,
|
---|
121 | const VertexArray &pts)
|
---|
122 | {
|
---|
123 | AxisAlignedBox3 extremalPoints;
|
---|
124 | extremalPoints.Initialize();
|
---|
125 |
|
---|
126 | VertexArray::const_iterator it, it_end = pts.end();
|
---|
127 |
|
---|
128 | for (it = pts.begin(); it != it_end; ++ it)
|
---|
129 | {
|
---|
130 | Vector3 pt = *it;
|
---|
131 | pt = m * pt;
|
---|
132 |
|
---|
133 | extremalPoints.Include(pt);
|
---|
134 | }
|
---|
135 |
|
---|
136 | return extremalPoints;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | ShadowMap::ShadowMap(Light *light, int size, const AxisAlignedBox3 &sceneBox, Camera *cam):
|
---|
141 | mSceneBox(sceneBox), mSize(size), mCamera(cam), mLight(light)
|
---|
142 | {
|
---|
143 | mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true);
|
---|
144 | // the diffuse color buffer
|
---|
145 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
146 | //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
147 |
|
---|
148 | mShadowCam = new Camera(mSize, mSize);//mSceneBox.Size().x * 0.5f, mSceneBox.Size().y * 0.5f);
|
---|
149 | mShadowCam->SetOrtho(true);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | ShadowMap::~ShadowMap()
|
---|
154 | {
|
---|
155 | DEL_PTR(mFbo);
|
---|
156 | DEL_PTR(mShadowCam);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | void ShadowMap::DrawPoly(Polyhedron *poly, const Vector3 &color)
|
---|
161 | {
|
---|
162 | if (!poly) return;
|
---|
163 |
|
---|
164 | for (size_t i = 0; i < poly->NumPolygons(); ++ i)
|
---|
165 | {
|
---|
166 | glColor3f(color.x, color.y, color.z);
|
---|
167 |
|
---|
168 | glBegin(GL_LINE_LOOP);
|
---|
169 |
|
---|
170 | Polygon3 *p = poly->GetPolygons()[i];
|
---|
171 |
|
---|
172 | for (size_t j = 0; j < p->mVertices.size(); ++ j)
|
---|
173 | {
|
---|
174 | Vector3 v = p->mVertices[j];
|
---|
175 | glVertex3d(v.x, v.y, v.z);
|
---|
176 | }
|
---|
177 |
|
---|
178 | glEnd();
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | void ShadowMap::DrawPolys()
|
---|
184 | {
|
---|
185 | DrawPoly(lightPoly, Vector3(1, 0, 1));
|
---|
186 | DrawPoly(polyhedron, Vector3(0, 1, 0));
|
---|
187 |
|
---|
188 | glPointSize(10.0f);
|
---|
189 |
|
---|
190 | Vector3 pt = Vector3::ZERO();
|
---|
191 |
|
---|
192 | Matrix4x4 myMat = Invert(dummyMat);
|
---|
193 | pt = myMat * pt;
|
---|
194 |
|
---|
195 | glBegin(GL_POINTS);
|
---|
196 | glVertex3f(pt.x, pt.y, pt.z);
|
---|
197 | glEnd();
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const
|
---|
202 | {
|
---|
203 | const float n = mCamera->GetNear();
|
---|
204 |
|
---|
205 | const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
|
---|
206 |
|
---|
207 | const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection());
|
---|
208 | const float sinGamma = sin(fabs(acos(dotProd)));
|
---|
209 |
|
---|
210 | return (n + sqrt(n * (n + d * sinGamma))) / sinGamma;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightSpace,
|
---|
215 | const AxisAlignedBox3 &extremalPoints,
|
---|
216 | const VertexArray &body
|
---|
217 | )
|
---|
218 | {
|
---|
219 | //return IdentityMatrix();
|
---|
220 |
|
---|
221 | AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
|
---|
222 |
|
---|
223 | ///////////////
|
---|
224 | //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
|
---|
225 | //-- first find the free parameter values n, and P (the projection center), and the projection depth
|
---|
226 |
|
---|
227 | const float n = 1e1f;
|
---|
228 | //const float n = 1e6f;
|
---|
229 | //const float n = ComputeN(bounds_ls) * 100;
|
---|
230 |
|
---|
231 | cout << "n: " << n << endl;
|
---|
232 |
|
---|
233 | const Vector3 nearPt = GetNearCameraPointE(body);
|
---|
234 |
|
---|
235 | //get the coordinates of the near camera point in light space
|
---|
236 | const Vector3 lsNear = lightSpace * nearPt;
|
---|
237 |
|
---|
238 | //c start has the x and y coordinate of e, the z coord of the near plane of the light volume
|
---|
239 | //const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
|
---|
240 | const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
|
---|
241 |
|
---|
242 | cout << "mx: " << bounds_ls.Max() << endl;
|
---|
243 | cout << "mn: " << bounds_ls.Min() << endl;
|
---|
244 |
|
---|
245 | // the new projection center
|
---|
246 | Vector3 projCenter = startPt + Vector3::UNIT_Z() * n;
|
---|
247 |
|
---|
248 | cout <<"start: " << startPt << " " << projCenter << " " << Distance(lightSpace * mCamera->GetPosition(), startPt) << endl;
|
---|
249 | //dummyPt = -projCenter;
|
---|
250 |
|
---|
251 | //construct a translation that moves to the projection center
|
---|
252 | const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter);
|
---|
253 |
|
---|
254 | // light space y size
|
---|
255 | const float d = fabs(bounds_ls.Max()[2] - bounds_ls.Min()[2]);
|
---|
256 |
|
---|
257 | const float dy = fabs(bounds_ls.Max()[1] - bounds_ls.Min()[1]);
|
---|
258 | const float dx = fabs(bounds_ls.Max()[0] - bounds_ls.Min()[0]);
|
---|
259 |
|
---|
260 | cout << "d: " << d << " dy: " << dy << " dx: " << dx << endl;
|
---|
261 |
|
---|
262 |
|
---|
263 |
|
---|
264 | //////////
|
---|
265 | //-- now apply these values to construct the perspective lispsm matrix
|
---|
266 |
|
---|
267 | Matrix4x4 matLispSM;
|
---|
268 |
|
---|
269 | matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
|
---|
270 |
|
---|
271 | //cout << "lispsm\n" << matLispSM << endl;
|
---|
272 |
|
---|
273 | // translate to the projection center
|
---|
274 | matLispSM = projectionCenter * matLispSM;
|
---|
275 |
|
---|
276 | //cout << "new\n" << matLispSM << endl;
|
---|
277 |
|
---|
278 | // transform into OpenGL right handed system
|
---|
279 | Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f);
|
---|
280 | matLispSM *= refl;
|
---|
281 |
|
---|
282 | return matLispSM;
|
---|
283 | }
|
---|
284 |
|
---|
285 | #if 0
|
---|
286 | Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
|
---|
287 | {
|
---|
288 | float maxDist = -1e25f;
|
---|
289 | Vector3 nearest = Vector3::ZERO();
|
---|
290 |
|
---|
291 | Matrix4x4 eyeView;
|
---|
292 | mCamera->GetModelViewMatrix(eyeView);
|
---|
293 |
|
---|
294 | VertexArray newPts;
|
---|
295 | polyhedron->CollectVertices(newPts);
|
---|
296 |
|
---|
297 | //the LVS volume is always in front of the camera
|
---|
298 | VertexArray::const_iterator it, it_end = pts.end();
|
---|
299 |
|
---|
300 | for (it = pts.begin(); it != it_end; ++ it)
|
---|
301 | {
|
---|
302 | Vector3 pt = *it;
|
---|
303 | Vector3 ptE = eyeView * pt;
|
---|
304 | //cout<<"i"<< pt.z;
|
---|
305 | if (ptE.z > 0) cerr <<"should not happen " << ptE.z << endl;
|
---|
306 | else
|
---|
307 | if (ptE.z > maxDist)
|
---|
308 | {
|
---|
309 | cout << " d " << ptE.z;
|
---|
310 |
|
---|
311 | maxDist = ptE.z;
|
---|
312 | nearest = pt;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | // return Invert(eyeView) * nearest;
|
---|
317 | return nearest;
|
---|
318 | }
|
---|
319 |
|
---|
320 | #else
|
---|
321 |
|
---|
322 | Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
|
---|
323 | {
|
---|
324 | VertexArray newPts;
|
---|
325 | polyhedron->CollectVertices(newPts);
|
---|
326 |
|
---|
327 | Vector3 nearest = Vector3::ZERO();
|
---|
328 | float minDist = 1e25f;
|
---|
329 |
|
---|
330 | const Vector3 camPos = mCamera->GetPosition();
|
---|
331 |
|
---|
332 | VertexArray::const_iterator it, it_end = newPts.end();
|
---|
333 |
|
---|
334 | for (it = newPts.begin(); it != it_end; ++ it)
|
---|
335 | {
|
---|
336 | Vector3 pt = *it;
|
---|
337 |
|
---|
338 | const float dist = SqrDistance(pt, camPos);
|
---|
339 |
|
---|
340 | if (dist < minDist)
|
---|
341 | {
|
---|
342 | minDist = dist;
|
---|
343 | nearest = pt;
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | return nearest;
|
---|
348 | }
|
---|
349 |
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace,
|
---|
353 | const VertexArray &pts) const
|
---|
354 | {
|
---|
355 | //get the point in the LVS volume that is nearest to the camera
|
---|
356 | const Vector3 e = GetNearCameraPointE(pts);
|
---|
357 |
|
---|
358 | //construct edge to transform into light-space
|
---|
359 | const Vector3 b = e + mCamera->GetDirection();
|
---|
360 | //transform to light-space
|
---|
361 | const Vector3 e_lp = lightSpace * e;
|
---|
362 | const Vector3 b_lp = lightSpace * b;
|
---|
363 |
|
---|
364 | Vector3 projDir(b_lp - e_lp);
|
---|
365 |
|
---|
366 | Matrix4x4 dummy = Invert(lightSpace);
|
---|
367 | Invert(dummy);
|
---|
368 | Vector3 dummyVec = dummy * e_lp;
|
---|
369 | Vector3 dummyVec2 = dummy * b_lp;
|
---|
370 |
|
---|
371 | //projDir.z = -projDir.z;
|
---|
372 |
|
---|
373 | cout << "dummy: " << Normalize(dummyVec2 - dummyVec) << endl;
|
---|
374 | //project the view direction into the shadow map plane
|
---|
375 | projDir.y = .0f;
|
---|
376 |
|
---|
377 | return Normalize(projDir);
|
---|
378 | //return projDir;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj)
|
---|
383 | {
|
---|
384 | ///////////////////
|
---|
385 | //-- First step: calc frustum clipped by scene box
|
---|
386 |
|
---|
387 | DEL_PTR(polyhedron);
|
---|
388 | polyhedron = CalcClippedFrustum(mSceneBox);
|
---|
389 |
|
---|
390 | if (!polyhedron) return false; // something is wrong
|
---|
391 |
|
---|
392 | // include the part of the light volume that "sees" the frustum
|
---|
393 | // we only require frustum vertices
|
---|
394 |
|
---|
395 | VertexArray frustumPoints;
|
---|
396 | IncludeLightVolume(*polyhedron, frustumPoints, mShadowCam->GetDirection(), mSceneBox);
|
---|
397 |
|
---|
398 |
|
---|
399 | ///////////////
|
---|
400 | //-- transform points from world view to light view and calculate extremal points
|
---|
401 |
|
---|
402 | Matrix4x4 lightView;
|
---|
403 | mShadowCam->GetModelViewMatrix(lightView);
|
---|
404 |
|
---|
405 | const AxisAlignedBox3 extremalPoints = GetExtremalPoints(lightView, frustumPoints);
|
---|
406 |
|
---|
407 | // we use directional lights, so the projection can be set to identity
|
---|
408 | lightProj = IdentityMatrix();
|
---|
409 |
|
---|
410 | // switch coordinate system to that used in the lispsm algorithm for calculations
|
---|
411 | Matrix4x4 transform2LispSM = ZeroMatrix();
|
---|
412 |
|
---|
413 | transform2LispSM.x[0][0] = 1.0f;
|
---|
414 | transform2LispSM.x[1][2] = -1.0f; // y => -z
|
---|
415 | transform2LispSM.x[2][1] = 1.0f; // z => y
|
---|
416 | transform2LispSM.x[3][3] = 1.0f;
|
---|
417 |
|
---|
418 | //switch to the lightspace used in the article
|
---|
419 | lightProj = lightProj * transform2LispSM;
|
---|
420 |
|
---|
421 | const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
|
---|
422 |
|
---|
423 |
|
---|
424 | cout << "projViewDir: " << projViewDir << " orig " << mCamera->GetDirection() << endl;
|
---|
425 |
|
---|
426 | //do Light Space Perspective shadow mapping
|
---|
427 | //rotate the lightspace so that the projected light view always points upwards
|
---|
428 | //calculate a frame matrix that uses the projViewDir[lightspace] as up vector
|
---|
429 | //look(from position, into the direction of the projected direction, with unchanged up-vector)
|
---|
430 | const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), -projViewDir, -Vector3::UNIT_Y());
|
---|
431 | //const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Z());
|
---|
432 |
|
---|
433 | cout << "frame\n " << frame << endl;
|
---|
434 | lightProj = lightProj * frame;
|
---|
435 |
|
---|
436 | cout << "here9\n" << lightProj << endl;
|
---|
437 |
|
---|
438 | const Matrix4x4 matLispSM =
|
---|
439 | CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
|
---|
440 |
|
---|
441 | lightProj = lightProj * matLispSM;
|
---|
442 |
|
---|
443 | // change back to GL coordinate system
|
---|
444 | Matrix4x4 transformToGL = ZeroMatrix();
|
---|
445 |
|
---|
446 | transformToGL.x[0][0] = 1.0f;
|
---|
447 | transformToGL.x[1][2] = 1.0f; // z => y
|
---|
448 | transformToGL.x[2][1] = -1.0f; // y => -z
|
---|
449 | transformToGL.x[3][3] = 1.0f;
|
---|
450 |
|
---|
451 | lightProj = lightProj * transformToGL;
|
---|
452 | //cout << "here4 \n" << lightProj << endl;
|
---|
453 |
|
---|
454 | AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
|
---|
455 |
|
---|
456 | //cout << "ma2: " << lightPts.Max() << endl;
|
---|
457 | //cout << "mi2: " << lightPts.Min() << endl;
|
---|
458 |
|
---|
459 | // focus projection matrix on the extremal points => scale to unit cube
|
---|
460 | Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
|
---|
461 | lightProj *= scaleTranslate;
|
---|
462 |
|
---|
463 | cout << "max: " << lightProj * extremalPoints.Max() << endl;
|
---|
464 | cout << "min: " << lightProj * extremalPoints.Min() << endl;
|
---|
465 |
|
---|
466 | // we have to flip the signs in order to tranform to opengl right handed system
|
---|
467 | Matrix4x4 refl = ScaleMatrix(1, 1, -1);
|
---|
468 | lightProj *= refl;
|
---|
469 |
|
---|
470 | return true;
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | Polyhedron *ShadowMap::CalcClippedFrustum(const AxisAlignedBox3 &box) const
|
---|
475 | {
|
---|
476 | Polyhedron *p = mCamera->ComputeFrustum();
|
---|
477 |
|
---|
478 | Polyhedron *clippedPolyhedron = box.CalcIntersection(*p);
|
---|
479 |
|
---|
480 | DEL_PTR(p);
|
---|
481 |
|
---|
482 | return clippedPolyhedron;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | //calculates the up vector for the light coordinate frame
|
---|
487 | static Vector3 CalcUpVec(const Vector3 viewDir, const Vector3 lightDir)
|
---|
488 | {
|
---|
489 | //we do what gluLookAt does...
|
---|
490 | //left is the normalized vector perpendicular to lightDir and viewDir
|
---|
491 | //this means left is the normalvector of the yz-plane from the paper
|
---|
492 | Vector3 left = CrossProd(lightDir, viewDir);
|
---|
493 |
|
---|
494 | //we now can calculate the rotated(in the yz-plane) viewDir vector
|
---|
495 | //and use it as up vector in further transformations
|
---|
496 | Vector3 up = CrossProd(left, lightDir);
|
---|
497 |
|
---|
498 | return Normalize(up);
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | void ShadowMap::GetTextureMatrix(Matrix4x4 &m) const
|
---|
503 | {
|
---|
504 | m = mTextureMatrix;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | unsigned int ShadowMap::GetDepthTexture() const
|
---|
509 | {
|
---|
510 | return mFbo->GetDepthTex();
|
---|
511 | }
|
---|
512 |
|
---|
513 | unsigned int ShadowMap::GetShadowColorTexture() const
|
---|
514 | {
|
---|
515 | return mFbo->GetColorBuffer(0)->GetTexture();
|
---|
516 |
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 | void ShadowMap::IncludeLightVolume(const Polyhedron &polyhedron,
|
---|
521 | VertexArray &frustumPoints,
|
---|
522 | const Vector3 lightDir,
|
---|
523 | const AxisAlignedBox3 &sceneBox
|
---|
524 | )
|
---|
525 | {
|
---|
526 | // we don't need closed form anymore => just store vertices
|
---|
527 | VertexArray vertices;
|
---|
528 | polyhedron.CollectVertices(vertices);
|
---|
529 |
|
---|
530 | // we 'look' at each point and calculate intersections of rays with scene bounding box
|
---|
531 | VertexArray::const_iterator it, it_end = vertices.end();
|
---|
532 |
|
---|
533 | for (it = vertices.begin(); it != it_end; ++ it)
|
---|
534 | {
|
---|
535 | Vector3 v = *it;
|
---|
536 |
|
---|
537 | frustumPoints.push_back(v);
|
---|
538 |
|
---|
539 | // hack: get point surely outside of box
|
---|
540 | v -= Magnitude(mSceneBox.Diagonal()) * lightDir;
|
---|
541 |
|
---|
542 | SimpleRay ray(v, lightDir);
|
---|
543 |
|
---|
544 | float tNear, tFar;
|
---|
545 |
|
---|
546 | if (sceneBox.Intersects(ray, tNear, tFar))
|
---|
547 | {
|
---|
548 | Vector3 newpt = ray.Extrap(tNear);
|
---|
549 | frustumPoints.push_back(newpt);
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 |
|
---|
555 | void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView)
|
---|
556 | {
|
---|
557 | const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
|
---|
558 | const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
|
---|
559 |
|
---|
560 | //const Vector3 dir = mLight->GetDirection();
|
---|
561 | const Vector3 dir(0, 0, -1);
|
---|
562 |
|
---|
563 | // set position so that we can see the whole scene
|
---|
564 | //Vector3 pos = mSceneBox.Center();
|
---|
565 | //pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
|
---|
566 | //mShadowCam->SetPosition(pos);
|
---|
567 | mShadowCam->SetPosition(mCamera->GetPosition());
|
---|
568 |
|
---|
569 | Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
|
---|
570 | Matrix4x4 lightView = MyLookAt2(mShadowCam->GetPosition(), -dir, upVec);
|
---|
571 |
|
---|
572 | mShadowCam->mViewOrientation = lightView;
|
---|
573 |
|
---|
574 | //cout << "here45:\n" << lightView << endl;
|
---|
575 |
|
---|
576 | //mShadowCam->SetDirection(dir);
|
---|
577 | //mShadowCam->GetModelViewMatrix(lightView);
|
---|
578 | //cout << "here46:\n" << lightView << endl;
|
---|
579 |
|
---|
580 | mFbo->Bind();
|
---|
581 |
|
---|
582 | glDrawBuffers(1, mrt);
|
---|
583 |
|
---|
584 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
585 | glViewport(0, 0, mSize, mSize);
|
---|
586 |
|
---|
587 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
588 |
|
---|
589 | glDisable(GL_LIGHTING);
|
---|
590 | glDisable(GL_TEXTURE_2D);
|
---|
591 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
592 |
|
---|
593 | glPolygonOffset(1.0f, 2000.0f);
|
---|
594 | glEnable(GL_POLYGON_OFFSET_FILL);
|
---|
595 |
|
---|
596 | glShadeModel(GL_FLAT);
|
---|
597 | glEnable(GL_DEPTH_TEST);
|
---|
598 |
|
---|
599 | Matrix4x4 lightProj;
|
---|
600 |
|
---|
601 | CalcLightProjection(lightProj);
|
---|
602 |
|
---|
603 | glMatrixMode(GL_PROJECTION);
|
---|
604 | glPushMatrix();
|
---|
605 | glLoadMatrixf((float *)lightProj.x);
|
---|
606 |
|
---|
607 | mLightProjView = lightView * lightProj;
|
---|
608 | ;
|
---|
609 | cout << "here3" << endl;
|
---|
610 | DEL_PTR(lightPoly);
|
---|
611 | lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
|
---|
612 | cout << "here4\n" << lightView << endl;
|
---|
613 |
|
---|
614 | glMatrixMode(GL_MODELVIEW);
|
---|
615 | glPushMatrix();
|
---|
616 | glLoadIdentity();
|
---|
617 |
|
---|
618 | mShadowCam->SetupCameraView();
|
---|
619 |
|
---|
620 | dummyMat = mLightProjView;
|
---|
621 |
|
---|
622 |
|
---|
623 | //////////////
|
---|
624 | //-- compute texture matrix
|
---|
625 |
|
---|
626 | static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
|
---|
627 | 0.0f, 0.5f, 0.0f, 0.5f,
|
---|
628 | 0.0f, 0.0f, 0.5f, 0.5f,
|
---|
629 | 0.0f, 0.0f, 0.0f, 1.0f);
|
---|
630 |
|
---|
631 | mTextureMatrix = mLightProjView * biasMatrix;
|
---|
632 |
|
---|
633 |
|
---|
634 |
|
---|
635 |
|
---|
636 | /////////////
|
---|
637 | //-- render scene into shadow map
|
---|
638 |
|
---|
639 | renderer->RenderScene();
|
---|
640 |
|
---|
641 |
|
---|
642 | glDisable(GL_POLYGON_OFFSET_FILL);
|
---|
643 | glMatrixMode(GL_MODELVIEW);
|
---|
644 | glPopMatrix();
|
---|
645 |
|
---|
646 | glMatrixMode(GL_PROJECTION);
|
---|
647 | glPopMatrix();
|
---|
648 |
|
---|
649 | glPopAttrib();
|
---|
650 |
|
---|
651 |
|
---|
652 | glEnable(GL_LIGHTING);
|
---|
653 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
654 |
|
---|
655 | #if 0
|
---|
656 | float *data = new float[mSize * mSize];
|
---|
657 |
|
---|
658 | GrabDepthBuffer(data, mFbo->GetDepthTex());
|
---|
659 | ExportDepthBuffer(data, mSize);
|
---|
660 |
|
---|
661 | delete [] data;
|
---|
662 |
|
---|
663 | PrintGLerror("shadow map");
|
---|
664 | #endif
|
---|
665 | FrameBufferObject::Release();
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView)
|
---|
670 | {
|
---|
671 | //const Vector3 dir = mLight->GetDirection();
|
---|
672 | const Vector3 dir(0, 0, -1);
|
---|
673 |
|
---|
674 | mShadowCam->SetDirection(dir);
|
---|
675 |
|
---|
676 | // set position so that we can see the whole scene
|
---|
677 | Vector3 pos = mSceneBox.Center();
|
---|
678 | pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
|
---|
679 |
|
---|
680 | mShadowCam->SetPosition(mCamera->GetPosition());
|
---|
681 |
|
---|
682 | Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
|
---|
683 | Matrix4x4 lightView = MyLookAt2(mShadowCam->GetPosition(), -dir, upVec);
|
---|
684 |
|
---|
685 | mShadowCam->mViewOrientation = lightView;
|
---|
686 |
|
---|
687 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
688 |
|
---|
689 | glEnable(GL_LIGHTING);
|
---|
690 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
691 |
|
---|
692 | glPolygonOffset(1.0f, 2000.0f);
|
---|
693 | glEnable(GL_POLYGON_OFFSET_FILL);
|
---|
694 |
|
---|
695 | glEnable(GL_DEPTH_TEST);
|
---|
696 |
|
---|
697 | Matrix4x4 lightProj;
|
---|
698 |
|
---|
699 | CalcLightProjection(lightProj);
|
---|
700 |
|
---|
701 | glMatrixMode(GL_PROJECTION);
|
---|
702 | glPushMatrix();
|
---|
703 | glLoadMatrixf((float *)lightProj.x);
|
---|
704 |
|
---|
705 | mLightProjView = lightView * lightProj;
|
---|
706 |
|
---|
707 | dummyMat = mLightProjView;
|
---|
708 |
|
---|
709 | DEL_PTR(lightPoly);
|
---|
710 | lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
|
---|
711 |
|
---|
712 | glMatrixMode(GL_MODELVIEW);
|
---|
713 | glPushMatrix();
|
---|
714 | glLoadIdentity();
|
---|
715 |
|
---|
716 | //glDisable(GL_CULL_FACE);
|
---|
717 |
|
---|
718 | mShadowCam->SetupCameraView();
|
---|
719 |
|
---|
720 |
|
---|
721 | /////////////
|
---|
722 | //-- render scene into shadow map
|
---|
723 |
|
---|
724 | renderer->RenderScene();
|
---|
725 |
|
---|
726 |
|
---|
727 | glDisable(GL_POLYGON_OFFSET_FILL);
|
---|
728 |
|
---|
729 | glPushAttrib(GL_CURRENT_BIT);
|
---|
730 | glDisable(GL_LIGHTING);
|
---|
731 | glDisable(GL_DEPTH_TEST);
|
---|
732 | glDepthMask(GL_FALSE);
|
---|
733 |
|
---|
734 | Polyhedron *hpoly = CreatePolyhedron(projView, mSceneBox);
|
---|
735 | //Polyhedron *hpoly = CalcClippedFrustum(mSceneBox);
|
---|
736 |
|
---|
737 | DrawPoly(hpoly, Vector3(1, 1, 1));
|
---|
738 |
|
---|
739 | DEL_PTR(hpoly);
|
---|
740 |
|
---|
741 | glEnable(GL_CULL_FACE);
|
---|
742 |
|
---|
743 | glEnable(GL_DEPTH_TEST);
|
---|
744 | glDepthMask(GL_TRUE);
|
---|
745 | glPopAttrib();
|
---|
746 |
|
---|
747 | glMatrixMode(GL_MODELVIEW);
|
---|
748 | glPopMatrix();
|
---|
749 |
|
---|
750 | glMatrixMode(GL_PROJECTION);
|
---|
751 | glPopMatrix();
|
---|
752 |
|
---|
753 |
|
---|
754 | #if 0
|
---|
755 | float *data = new float[mSize * mSize];
|
---|
756 |
|
---|
757 | GrabDepthBuffer(data, mFbo->GetDepthTex());
|
---|
758 | ExportDepthBuffer(data, mSize);
|
---|
759 |
|
---|
760 | delete [] data;
|
---|
761 |
|
---|
762 | PrintGLerror("shadow map");
|
---|
763 | #endif
|
---|
764 | FrameBufferObject::Release();
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 |
|
---|
769 |
|
---|
770 | } // namespace
|
---|