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

Revision 2932, 18.5 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2891]1#include "ShadowMapping.h"
2#include "FrameBufferObject.h"
3#include "RenderState.h"
4#include "RenderTraverser.h"
5#include "Light.h"
[2911]6#include "Polygon3.h"
7#include "Polyhedron.h"
8
[2891]9#include <IL/il.h>
10#include <assert.h>
11
[2911]12
[2891]13using namespace std;
14
15
16namespace CHCDemoEngine
17{
18
[2931]19static CGprogram sCgShadowProgram;
20static CGparameter sShadowParam;
[2891]21
[2931]22
[2911]23static Polyhedron *polyhedron = NULL;
[2929]24static Polyhedron *lightPoly = NULL;
[2932]25static Matrix4x4 dummy = IdentityMatrix();
[2911]26
27
[2891]28static 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
[2931]41static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix, const AxisAlignedBox3 &sceneBox)
42{
43        Frustum frustum(lightMatrix);
[2891]44
[2931]45        vector<Plane3> clipPlanes;
[2891]46
[2931]47        for (int i = 0; i < 6; ++ i)
48        {
49                ////////////
50                //-- normalize the coefficients
[2891]51
[2931]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
[2891]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
[2924]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
[2897]139ShadowMap::ShadowMap(Light *light, int size, const AxisAlignedBox3 &sceneBox, Camera *cam):
140mSceneBox(sceneBox), mSize(size), mCamera(cam), mLight(light)
[2891]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);
[2928]145        //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2891]146
[2928]147        mShadowCam = new Camera(mSize, mSize);//mSceneBox.Size().x * 0.5f, mSceneBox.Size().y * 0.5f);
[2891]148        mShadowCam->SetOrtho(true);
149}
150
151
[2894]152ShadowMap::~ShadowMap()
[2891]153{
154        DEL_PTR(mFbo);
[2897]155        DEL_PTR(mShadowCam);
[2891]156}
157
158
[2929]159void ShadowMap::DrawPoly(Polyhedron *poly, const Vector3 &color)
[2891]160{
[2929]161        if (!poly) return;
[2911]162
[2929]163        for (size_t i = 0; i < poly->NumPolygons(); ++ i)
[2911]164        {
[2929]165                glColor3f(color.x, color.y, color.z);
[2911]166
167                glBegin(GL_LINE_LOOP);
168
[2929]169                Polygon3 *p = poly->GetPolygons()[i];
[2911]170
[2929]171                for (size_t j = 0; j < p->mVertices.size(); ++ j)
[2911]172                {
[2929]173                        Vector3 v = p->mVertices[j];
[2911]174                        glVertex3d(v.x, v.y, v.z);
175                }
176
177                glEnd();
178        }
179}
180
181
[2929]182void ShadowMap::DrawPolys()
[2911]183{
[2929]184        DrawPoly(lightPoly, Vector3(1, 0, 1));
185        DrawPoly(polyhedron, Vector3(0, 1, 0));
[2932]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();
[2913]196}
[2911]197
[2913]198
[2920]199float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const
[2913]200{
[2920]201        const float n = mCamera->GetNear();
[2916]202       
[2920]203        const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
204       
[2916]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
[2924]212Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightSpace,
[2920]213                                                                                 const AxisAlignedBox3 &extremalPoints,
[2924]214                                                                                 const VertexArray &body
[2920]215                                                                                 )
[2917]216{
[2924]217        //return IdentityMatrix();
[2913]218
[2928]219        AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
220
[2913]221        ///////////////
[2915]222        //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
[2924]223        //-- first find the free parameter values n, and P (the projection center), and the projection depth
224
[2932]225        const float n = 1e2f;
226        //const float n = 1e6f;
[2929]227        //const float n = ComputeN(bounds_ls) * 100;
[2924]228
[2916]229        cout << "n: " << n << endl;
[2915]230
[2924]231        const Vector3 nearPt = GetNearCameraPointE(body);
232        const float dummy = 0;
[2915]233        //get the coordinates of the near camera point in light space
[2924]234        const Vector3 lsNear = lightSpace * nearPt;
[2915]235
[2922]236        //c start has the x and y coordinate of e,  the z coord of the near plane of the light volume
[2924]237        const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
[2915]238
[2924]239        cout << "mx: " <<  bounds_ls.Max() << endl;
240        cout << "mn: " << bounds_ls.Min() << endl;
[2922]241
[2915]242        // the new projection center
[2928]243        Vector3 projCenter = startPt + Vector3::UNIT_Z() * n;
[2915]244
[2924]245        cout <<"start: " << startPt << " " << projCenter << " " << Distance(lightSpace * mCamera->GetPosition(), startPt) << endl;
[2922]246
[2915]247        //construct a translation that moves to the projection center
248        const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter);
249
[2916]250        // light space y size
[2924]251        const float d = fabs(bounds_ls.Max()[2] - bounds_ls.Min()[2])+dummy;
[2922]252
[2924]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]);
[2922]255
256        cout << "d: " << d << " dy: " << dy << " dx: " << dx << endl;
257
[2924]258       
[2915]259
[2928]260        //////////
[2924]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
[2922]267        //cout << "lispsm\n" << matLispSM << endl;
[2915]268
[2924]269        // translate to the projection center
[2920]270        matLispSM = projectionCenter * matLispSM;
[2915]271
[2922]272        //cout << "new\n" << matLispSM << endl;
[2917]273
[2915]274        // transform into OpenGL right handed system
[2920]275        Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f);
276        matLispSM *= refl;
[2924]277       
[2920]278        return matLispSM;
[2915]279}
280
[2925]281#if 0
282Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
283{
284        float maxDist = -1e25f;
285        Vector3 nearest = Vector3::ZERO();
[2915]286
[2925]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
[2920]318Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
[2919]319{
[2925]320        VertexArray newPts;
321        polyhedron->CollectVertices(newPts);
322
[2922]323        Vector3 nearest = Vector3::ZERO();
324        float minDist = 1e25f;
[2920]325
[2924]326        const Vector3 camPos = mCamera->GetPosition();
[2920]327
[2925]328        VertexArray::const_iterator it, it_end = newPts.end();
[2920]329
[2925]330        for (it = newPts.begin(); it != it_end; ++ it)
[2920]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
[2925]346#endif
[2920]347
348Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const
349{
[2919]350        //get the point in the LVS volume that is nearest to the camera
[2920]351        const Vector3 e = GetNearCameraPointE(pts);
352
[2919]353        //construct edge to transform into light-space
[2920]354        const Vector3 b = e + mCamera->GetDirection();
[2919]355        //transform to light-space
[2920]356        const Vector3 e_lp = lightSpace * e;
357        const Vector3 b_lp = lightSpace * b;
358
359        Vector3 projDir(b_lp - e_lp);
360
[2924]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;
[2919]369        //project the view direction into the shadow map plane
[2932]370        projDir.y = .0f;
[2920]371
372        return Normalize(projDir);
[2924]373        //return projDir;
[2919]374}
375
376
[2915]377bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj)
378{
379        ///////////////////
380        //-- First step: calc frustum clipped by scene box
381
[2922]382        DEL_PTR(polyhedron);
[2915]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
[2920]400        const AxisAlignedBox3 extremalPoints = GetExtremalPoints(lightView, frustumPoints);
[2913]401
[2920]402        // we use directional lights, so the projection can be set to identity
403        lightProj = IdentityMatrix();
[2913]404
[2922]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
[2924]410        transform2LispSM.x[2][1] =  1.0f; // z => y
[2922]411        transform2LispSM.x[3][3] =  1.0f;
412
413        //switch to the lightspace used in the article
[2932]414        //lightProj = lightProj * transform2LispSM;
[2922]415
[2924]416        const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
[2916]417
[2915]418
[2924]419        cout << "projViewDir: " << projViewDir << " orig " << mCamera->GetDirection() << endl;
420
[2917]421        //do Light Space Perspective shadow mapping
[2922]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
[2917]424        //look(from position, into the direction of the projected direction, with unchanged up-vector)
[2932]425        const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
[2917]426
[2924]427        cout << "frame\n " << frame << endl;
[2932]428        //lightProj = lightProj * frame;
[2924]429
[2922]430        cout << "here9\n" << lightProj << endl;
431
[2917]432        const Matrix4x4 matLispSM =
[2920]433                CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
[2917]434
[2923]435        lightProj = lightProj * matLispSM;
[2917]436
[2922]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
[2932]445        //lightProj = lightProj * transformToGL;
[2922]446        //cout << "here4 \n" << lightProj << endl;
447
[2920]448        AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
449
[2922]450        //cout << "ma2: " << lightPts.Max() << endl;
451        //cout << "mi2: " << lightPts.Min() << endl;
[2920]452
[2917]453        // focus projection matrix on the extremal points => scale to unit cube
[2922]454        Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
455        lightProj *= scaleTranslate;
[2917]456
[2922]457        cout << "max: " << lightProj * extremalPoints.Max() << endl;
458        cout << "min: " << lightProj * extremalPoints.Min() << endl;
[2920]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;
[2916]463       
[2913]464        return true;
[2911]465}
466
467
[2913]468Polyhedron *ShadowMap::CalcClippedFrustum(const AxisAlignedBox3 &box) const
469{
[2931]470        Polyhedron *p = mCamera->ComputeFrustum();
471       
472        Polyhedron *clippedPolyhedron = box.CalcIntersection(*p);
[2913]473
[2931]474        DEL_PTR(p);
[2913]475       
[2931]476        return clippedPolyhedron;
477}
[2913]478
479
[2931]480//calculates the up vector for the light coordinate frame
481static 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);
[2913]487       
[2931]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);
[2913]491
[2931]492        return Normalize(up);
[2913]493}
494
495
[2932]496void ShadowMap::GetTextureMatrix(Matrix4x4 &m) const
497{
498        m = mTextureMatrix;
499}
500
501 
502unsigned int ShadowMap::GetDepthTexture() const
503{
504        return mFbo->GetDepthTex();
505}
506
507unsigned int ShadowMap::GetShadowColorTexture() const
508{
509        return mFbo->GetColorBuffer(0)->GetTexture();
510       
511}
512
513
514void 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
[2911]549void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView)
550{
[2893]551        const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
552        const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
[2891]553       
[2922]554        //const Vector3 dir = mLight->GetDirection();
555        const Vector3 dir(0, 0, -1);
[2913]556
[2932]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
[2931]563        Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
[2932]564        Matrix4x4 lightView =
565                MyLookAt(mShadowCam->GetPosition(), dir, upVec);
[2931]566
[2932]567        mShadowCam->mViewOrientation = lightView;
[2913]568
[2932]569        //cout << "here45:\n" << lightView << endl;
[2893]570
[2932]571        //mShadowCam->SetDirection(dir);
572        //mShadowCam->GetModelViewMatrix(lightView);
573        //cout << "here46:\n" << lightView << endl;
[2911]574
[2891]575        mFbo->Bind();
[2892]576       
[2891]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
[2893]588        glPolygonOffset(1.0f, 2000.0f);
[2892]589        glEnable(GL_POLYGON_OFFSET_FILL);
[2891]590
591        glShadeModel(GL_FLAT);
592        glEnable(GL_DEPTH_TEST);
593
[2932]594        Matrix4x4 lightProj;
[2891]595
[2916]596        CalcLightProjection(lightProj);
[2894]597
[2913]598        glMatrixMode(GL_PROJECTION);
599        glPushMatrix();
600        glLoadMatrixf((float *)lightProj.x);
[2911]601
[2917]602        mLightProjView = lightView * lightProj;
[2911]603
[2932]604        cout << "here3" << endl;
[2929]605        DEL_PTR(lightPoly);
[2931]606        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
[2932]607        cout << "here4\n" << lightView << endl;
[2929]608
[2911]609        glMatrixMode(GL_MODELVIEW);
[2913]610        glPushMatrix();
611        glLoadIdentity();
[2911]612
[2932]613        mShadowCam->SetupCameraView();
[2911]614
[2932]615
[2913]616        //////////////
617        //-- compute texture matrix
[2925]618
[2894]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,
[2911]622                                                                0.0f, 0.0f, 0.0f, 1.0f);
[2894]623
[2911]624        mTextureMatrix = mLightProjView * biasMatrix;
[2894]625
626
[2911]627
628
[2894]629        /////////////
630        //-- render scene into shadow map
631
[2897]632        renderer->RenderScene();
633
[2891]634       
635        glDisable(GL_POLYGON_OFFSET_FILL);
[2892]636        glMatrixMode(GL_MODELVIEW);
[2891]637        glPopMatrix();
638
639        glMatrixMode(GL_PROJECTION);
640        glPopMatrix();
641
642        glPopAttrib();
[2928]643
644       
645        glEnable(GL_LIGHTING);
646        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
647
[2913]648#if 0
[2911]649        float *data = new float[mSize * mSize];
[2891]650
651        GrabDepthBuffer(data, mFbo->GetDepthTex());
652        ExportDepthBuffer(data, mSize);
653
[2892]654        delete [] data;
[2911]655       
[2892]656        PrintGLerror("shadow map");
[2913]657#endif
[2891]658        FrameBufferObject::Release();
659}
660
661
[2931]662void 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
[2932]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
[2931]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);
[2932]687       
[2931]688        glEnable(GL_DEPTH_TEST);
689
[2932]690        Matrix4x4 lightProj;
[2931]691       
692        CalcLightProjection(lightProj);
693
694        glMatrixMode(GL_PROJECTION);
695        glPushMatrix();
696        glLoadMatrixf((float *)lightProj.x);
697
698        mLightProjView = lightView * lightProj;
699
[2932]700        dummy = mLightProjView;
701
[2931]702        DEL_PTR(lightPoly);
703        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
704
705        glMatrixMode(GL_MODELVIEW);
706        glPushMatrix();
707        glLoadIdentity();
708
[2932]709        mShadowCam->SetupCameraView();
710       
[2931]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));
[2932]730
[2931]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
[2891]762} // namespace
Note: See TracBrowser for help on using the repository browser.