source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp @ 3001

Revision 3001, 17.6 KB checked in by mattausch, 16 years ago (diff)

reverted back from trying to use less components in fbo for faster sampling

Line 
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
13using namespace std;
14
15
16namespace CHCDemoEngine
17{
18
19static CGprogram sCgShadowProgram;
20static CGparameter sShadowParam;
21
22
23static Polyhedron *polyhedron = NULL;
24static Polyhedron *lightPoly = NULL;
25
26
27static void PrintGLerror(char *msg)
28{
29        GLenum errCode;
30        const GLubyte *errStr;
31       
32        if ((errCode = glGetError()) != GL_NO_ERROR)
33        {
34                errStr = gluErrorString(errCode);
35                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
36        }
37}
38
39
40static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix,
41                                                                        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
60        // first create near plane because of precision issues
61        clipPlanes.push_back(frustum.mClipPlanes[4]);
62
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[5]);
68
69        return Polyhedron::CreatePolyhedron(clipPlanes, sceneBox);
70}
71
72
73static 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
85static 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
119static 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
139ShadowMap::ShadowMap(DirectionalLight *light, int size, const AxisAlignedBox3 &sceneBox, Camera *cam):
140mSceneBox(sceneBox), mSize(size), mCamera(cam), mLight(light)
141{
142        mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true);
143
144        // need a color buffer to keep opengl happy
145        mFbo->AddColorBuffer(ColorBufferObject::RGBA_UBYTE,
146                                 ColorBufferObject::WRAP_CLAMP_TO_EDGE,
147                                                 ColorBufferObject::FILTER_NEAREST);
148
149
150        mShadowCam = new Camera(mSize, mSize);
151        mShadowCam->SetOrtho(true);
152}
153
154
155ShadowMap::~ShadowMap()
156{
157        DEL_PTR(mFbo);
158        DEL_PTR(mShadowCam);
159}
160
161
162static void DrawPolyhedron(Polyhedron *poly, const Vector3 &color)
163{
164        if (!poly) return;
165
166        for (size_t i = 0; i < poly->NumPolygons(); ++ i)
167        {
168                glColor3f(color.x, color.y, color.z);
169
170                glBegin(GL_LINE_LOOP);
171
172                Polygon3 *p = poly->GetPolygons()[i];
173
174                for (size_t j = 0; j < p->mVertices.size(); ++ j)
175                {
176                        Vector3 v = p->mVertices[j];
177                        glVertex3d(v.x, v.y, v.z);
178                }
179
180                glEnd();
181        }
182}
183
184
185void ShadowMap::VisualizeFrustra()
186{
187        DrawPolyhedron(lightPoly, Vector3(1, 0, 1));
188        DrawPolyhedron(polyhedron, Vector3(0, 1, 0));
189}
190
191
192// z0 is the point that lies on the parallel plane to the near plane through e (A)
193//and on the near plane of the C frustum (the plane z = bZmax) and on the line x = e.x
194Vector3 ShadowMap::GetLightSpaceZ0(const Matrix4x4 &lightSpace,
195                                                                   const Vector3 &e,
196                                                                   const float maxZ,
197                                                                   const Vector3 &eyeDir) const
198{
199        // to calculate the parallel plane to the near plane through e we
200        // calculate the plane A with the three points
201        Plane3 planeA(e, eyeDir);
202
203        planeA.Transform(lightSpace);
204       
205        // get the parameters of A from the plane equation n dot d = 0
206        const float d = planeA.mD;
207        const Vector3 n = planeA.mNormal;
208       
209        // transform to light space
210        const Vector3 e_ls = lightSpace * e;
211
212        Vector3 z0;
213
214        z0.x = e_ls.x;
215        z0.y = (d - n.z * maxZ - n.x * e_ls.x) / n.y;
216        z0.z = maxZ;
217
218        return z0;
219        //return V3(e_ls.x(),(d-n.z()*b_lsZmax-n.x()*e_ls.x())/n.y(),b_lsZmax);
220}
221
222
223float ShadowMap::ComputeNOpt(const Matrix4x4 &lightSpace,
224                                                         const AxisAlignedBox3 &extremalPoints,
225                                                         const VertexArray &body) const
226{
227        const Vector3 nearPt = GetNearCameraPointE(body);
228        const Vector3 eyeDir = mCamera->GetDirection();
229
230        Matrix4x4 eyeView;
231        mCamera->GetModelViewMatrix(eyeView);
232
233        const Matrix4x4 invLightSpace = Invert(lightSpace);
234
235        const Vector3 z0_ls = GetLightSpaceZ0(lightSpace, nearPt, extremalPoints.Max().z, eyeDir);
236        const Vector3 z1_ls = Vector3(z0_ls.x, z0_ls.y, extremalPoints.Min().z);
237       
238        // transform back to world space
239        const Vector3 z0_ws = invLightSpace * z0_ls;
240        const Vector3 z1_ws = invLightSpace * z1_ls;
241
242        // transform to eye space
243        const Vector3 z0_es = eyeView * z0_ws;
244        const Vector3 z1_es = eyeView * z1_ws;
245
246        const float z0 = z0_es.z;
247        const float z1 = z1_es.z;
248
249        cout << "z0 ls: " << z0_ls << " z1 ls: " << z1_ls << endl;
250        cout << "z0: " << z0_es << " z1: " << z1_es << endl;
251
252        const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
253
254        const float n = d / (sqrt(z1 / z0) - 1.0f);
255
256        return n;
257}
258
259
260float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const
261{
262        const float nearPlane = mCamera->GetNear();
263       
264        const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
265       
266        const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection());
267        const float sinGamma = sin(fabs(acos(dotProd)));
268
269        // test for values close to zero
270        if (sinGamma < 1e-6f) return 1e6f;
271       
272        const float scale = 2.0f;
273        return scale * (nearPlane + sqrt(nearPlane * (nearPlane + d * sinGamma))) /  sinGamma;
274}
275
276
277Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightSpace,
278                                                                                 const AxisAlignedBox3 &extremalPoints,
279                                                                                 const VertexArray &body
280                                                                                 )
281{
282        AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
283
284        ///////////////
285        //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
286        //-- first find the free parameter values n, and P (the projection center), and the projection depth
287
288        const float n = ComputeN(bounds_ls);
289        //const float n = ComputeNOpt(lightSpace, extremalPoints, body); cout << "n: " << n << endl;
290
291        if (n >= 1e6f) // light direction nearly parallel to view => switch to uniform
292                return IdentityMatrix();
293
294        const Vector3 nearPt = GetNearCameraPointE(body);
295       
296        //get the coordinates of the near camera point in light space
297        const Vector3 lsNear = lightSpace * nearPt;
298
299        // the start point has the x and y coordinate of e, the z coord of the near plane of the light volume
300        const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
301       
302        // the new projection center
303        const Vector3 projCenter = startPt + Vector3::UNIT_Z() * n;
304
305        //construct a translation that moves to the projection center
306        const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter);
307
308        // light space y size
309        const float d = fabs(bounds_ls.Max()[2] - bounds_ls.Min()[2]);
310
311        const float dy = fabs(bounds_ls.Max()[1] - bounds_ls.Min()[1]);
312        const float dx = fabs(bounds_ls.Max()[0] - bounds_ls.Min()[0]);
313
314       
315
316        //////////
317        //-- now apply these values to construct the perspective lispsm matrix
318
319        Matrix4x4 matLispSM;
320       
321        matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
322
323        // translate to the projection center
324        matLispSM = projectionCenter * matLispSM;
325
326        // transform into OpenGL right handed system
327        Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f);
328        matLispSM *= refl;
329       
330        return matLispSM;
331}
332
333#if 0
334Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
335{
336        float maxDist = -1e25f;
337        Vector3 nearest = Vector3::ZERO();
338
339        Matrix4x4 eyeView;
340        mCamera->GetModelViewMatrix(eyeView);
341
342        VertexArray newPts;
343        polyhedron->CollectVertices(newPts);
344       
345        //the LVS volume is always in front of the camera
346        VertexArray::const_iterator it, it_end = pts.end();     
347
348        for (it = pts.begin(); it != it_end; ++ it)
349        {
350                Vector3 pt = *it;
351                Vector3 ptE = eyeView * pt;
352               
353                if (ptE.z > 0) cerr <<"should not happen " << ptE.z << endl;
354                else
355                if (ptE.z > maxDist)
356                {
357                        cout << " d " << ptE.z;
358       
359                        maxDist = ptE.z;
360                        nearest = pt;
361                }
362        }
363
364        //      return Invert(eyeView) * nearest;
365        return nearest;
366}
367
368#else
369
370Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
371{
372        VertexArray newPts;
373        polyhedron->CollectVertices(newPts);
374
375        Vector3 nearest = Vector3::ZERO();
376        float minDist = 1e25f;
377
378        const Vector3 camPos = mCamera->GetPosition();
379
380        VertexArray::const_iterator it, it_end = newPts.end();
381
382        for (it = newPts.begin(); it != it_end; ++ it)
383        {
384                Vector3 pt = *it;
385
386                const float dist = SqrDistance(pt, camPos);
387
388                if (dist < minDist)
389                {
390                        minDist = dist;
391                        nearest = pt;
392                }
393        }
394
395        return nearest;
396}
397
398#endif
399
400Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace,
401                                                                  const VertexArray &pts) const
402{
403        //get the point in the LVS volume that is nearest to the camera
404        const Vector3 e = GetNearCameraPointE(pts);
405
406        //construct edge to transform into light-space
407        const Vector3 b = e + mCamera->GetDirection();
408        //transform to light-space
409        const Vector3 e_lp = lightSpace * e;
410        const Vector3 b_lp = lightSpace * b;
411
412        Vector3 projDir(b_lp - e_lp);
413
414        //project the view direction into the shadow map plane
415        projDir.y = .0f;
416
417        return Normalize(projDir);
418}
419
420
421bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj)
422{
423        ///////////////////
424        //-- First step: calc frustum clipped by scene box
425
426        DEL_PTR(polyhedron);
427        polyhedron = CalcClippedFrustum(mSceneBox);
428
429        if (!polyhedron) return false; // something is wrong
430
431        // include the part of the light volume that "sees" the frustum
432        // we only require frustum vertices
433
434        VertexArray frustumPoints;
435        IncludeLightVolume(*polyhedron, frustumPoints, mLight->GetDirection(), mSceneBox);
436
437
438        ///////////////
439        //-- transform points from world view to light view and calculate extremal points
440
441        Matrix4x4 lightView;
442        mShadowCam->GetModelViewMatrix(lightView);
443
444        const AxisAlignedBox3 extremalPoints = GetExtremalPoints(lightView, frustumPoints);
445
446        // we use directional lights, so the projection can be set to identity
447        lightProj = IdentityMatrix();
448
449        // switch coordinate system to that used in the lispsm algorithm for calculations
450        Matrix4x4 transform2LispSM = ZeroMatrix();
451
452        transform2LispSM.x[0][0] =  1.0f;
453        transform2LispSM.x[1][2] =  -1.0f; // y => -z
454        transform2LispSM.x[2][1] =  1.0f; // z => y
455        transform2LispSM.x[3][3] =  1.0f;
456
457
458        //switch to the lightspace used in the article
459        lightProj *= transform2LispSM;
460
461        const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
462
463        //do DirectionalLight Space Perspective shadow mapping
464        //rotate the lightspace so that the projected light view always points upwards
465        //calculate a frame matrix that uses the projViewDir[lightspace] as up vector
466        //look(from position, into the direction of the projected direction, with unchanged up-vector)
467        //const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
468        const Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
469
470        lightProj *= frame;
471
472        const Matrix4x4 matLispSM =
473                CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
474
475        lightProj *= matLispSM;
476
477        // change back to GL coordinate system
478        Matrix4x4 transformToGL = ZeroMatrix();
479       
480        transformToGL.x[0][0] =   1.0f;
481        transformToGL.x[1][2] =   1.0f; // z => y
482        transformToGL.x[2][1] =  -1.0f; // y => -z
483        transformToGL.x[3][3] =   1.0f;
484
485        lightProj *= transformToGL;
486
487        AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
488
489        // focus projection matrix on the extremal points => scale to unit cube
490        Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
491
492        lightProj = lightProj * scaleTranslate;
493
494        Matrix4x4 mymat = lightView * lightProj;
495
496        AxisAlignedBox3 lightPtsNew = GetExtremalPoints(mymat, frustumPoints);
497
498        // we have to flip the signs in order to tranform to opengl right handed system
499        Matrix4x4 refl = ScaleMatrix(1, 1, -1);
500        lightProj *= refl;
501       
502        return true;
503}
504
505
506Polyhedron *ShadowMap::CalcClippedFrustum(const AxisAlignedBox3 &box) const
507{
508        Polyhedron *p = mCamera->ComputeFrustum();
509       
510        Polyhedron *clippedPolyhedron = box.CalcIntersection(*p);
511
512        DEL_PTR(p);
513       
514        return clippedPolyhedron;
515}
516
517
518//calculates the up vector for the light coordinate frame
519static Vector3 CalcUpVec(const Vector3 viewDir, const Vector3 lightDir)
520{
521        //we do what gluLookAt does...
522        //left is the normalized vector perpendicular to lightDir and viewDir
523        //this means left is the normalvector of the yz-plane from the paper
524        Vector3 left = CrossProd(lightDir, viewDir);
525       
526        //we now can calculate the rotated(in the yz-plane) viewDir vector
527        //and use it as up vector in further transformations
528        Vector3 up = CrossProd(left, lightDir);
529
530        return Normalize(up);
531}
532
533
534void ShadowMap::GetTextureMatrix(Matrix4x4 &m) const
535{
536        m = mTextureMatrix;
537}
538
539 
540unsigned int ShadowMap::GetDepthTexture() const
541{
542        return mFbo->GetDepthTex();
543}
544
545unsigned int ShadowMap::GetShadowColorTexture() const
546{
547        return mFbo->GetColorBuffer(0)->GetTexture();
548       
549}
550
551
552void ShadowMap::IncludeLightVolume(const Polyhedron &polyhedron,
553                                                                   VertexArray &frustumPoints,
554                                                                   const Vector3 lightDir,
555                                                                   const AxisAlignedBox3 &sceneBox
556                                                                   )
557{
558        // we don't need closed form anymore => just store vertices
559        VertexArray vertices;
560        polyhedron.CollectVertices(vertices);
561
562        // we 'look' at each point and calculate intersections of rays with scene bounding box
563        VertexArray::const_iterator it, it_end = vertices.end();
564
565        for (it = vertices.begin(); it != it_end; ++ it)
566        {
567                Vector3 v  = *it;
568
569                frustumPoints.push_back(v);
570               
571                // hack: start at point which is guaranteed to be outside of box
572                v -= Magnitude(mSceneBox.Diagonal()) * lightDir;
573
574                SimpleRay ray(v, lightDir);
575
576                float tNear, tFar;
577
578                if (sceneBox.Intersects(ray, tNear, tFar))
579                {
580                        Vector3 newpt = ray.Extrap(tNear);
581                        frustumPoints.push_back(newpt);                 
582                }
583        }
584}
585
586
587void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView)
588{
589        mFbo->Bind();
590       
591        glDrawBuffers(1, mrt);
592
593        glPushAttrib(GL_VIEWPORT_BIT);
594        glViewport(0, 0, mSize, mSize);
595
596        glDisable(GL_LIGHTING);
597        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
598
599        glShadeModel(GL_FLAT);
600
601
602        /////////////
603        //-- render scene into shadow map
604
605        _Render(renderer);
606
607
608        //////////////
609        //-- compute texture matrix
610
611        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
612                                                                0.0f, 0.5f, 0.0f, 0.5f,
613                                                                0.0f, 0.0f, 0.5f, 0.5f,
614                                                                0.0f, 0.0f, 0.0f, 1.0f);
615
616        mTextureMatrix = mLightProjView * biasMatrix;
617
618        glPopAttrib();
619       
620        glShadeModel(GL_SMOOTH);
621        glEnable(GL_LIGHTING);
622        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
623
624#if 0
625        float *data = new float[mSize * mSize];
626
627        GrabDepthBuffer(data, mFbo->GetDepthTex());
628        ExportDepthBuffer(data, mSize);
629
630        delete [] data;
631       
632        PrintGLerror("shadow map");
633#endif
634       
635        FrameBufferObject::Release();
636}
637
638
639void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView)
640{
641        glEnable(GL_LIGHTING);
642       
643        _Render(renderer);
644       
645        /*glDisable(GL_LIGHTING);
646        glDisable(GL_DEPTH_TEST);
647
648        //glLineWidth(2);
649        Polyhedron *hpoly = CreatePolyhedron(projView, mSceneBox);
650        DrawPoly(hpoly, Vector3(1, 1, 1));
651
652        DEL_PTR(hpoly);
653
654        glEnable(GL_LIGHTING);
655        glEnable(GL_DEPTH_TEST);*/
656
657        glDisable(GL_POLYGON_OFFSET_FILL);
658}
659
660
661void ShadowMap::_Render(RenderTraverser *renderer)
662{
663        const Vector3 dir = mLight->GetDirection();
664
665        mShadowCam->SetDirection(dir);
666
667        // set position so that we can see the whole scene
668        Vector3 pos = mSceneBox.Center();
669        pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
670
671        mShadowCam->SetPosition(mCamera->GetPosition());
672
673        Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
674        Matrix4x4 lightView = LookAt(mShadowCam->GetPosition(), dir, upVec);
675
676        mShadowCam->mViewOrientation = lightView;
677
678        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
679
680        glPolygonOffset(5.0f, 100.0f);
681        glEnable(GL_POLYGON_OFFSET_FILL);
682       
683        Matrix4x4 lightProj;
684        CalcLightProjection(lightProj);
685
686        glMatrixMode(GL_PROJECTION);
687        glPushMatrix();
688        glLoadMatrixf((float *)lightProj.x);
689
690        mLightProjView = lightView * lightProj;
691
692        DEL_PTR(lightPoly);
693        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
694
695        glMatrixMode(GL_MODELVIEW);
696        glPushMatrix();
697       
698        mShadowCam->SetupCameraView();
699
700       
701        /////////////
702        //-- render scene into shadow map
703
704        renderer->RenderScene();
705
706
707        glMatrixMode(GL_PROJECTION);
708        glPopMatrix();
709
710        glMatrixMode(GL_MODELVIEW);
711        glPopMatrix();
712
713       
714        glDisable(GL_POLYGON_OFFSET_FILL);
715}
716} // namespace
Note: See TracBrowser for help on using the repository browser.