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