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

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

implemented sun color

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