Changeset 2917


Ignore:
Timestamp:
09/08/08 16:51:14 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp

    r2914 r2917  
    1111using namespace std; 
    1212 
    13 // this is the start vector.  
    14 // warning: our coordinate system has the negative z axis pointing up 
    15 // which is different from the system used in opengl!! 
    16 static Vector3 startVector = Vector3(0, 1, 0); 
     13// our coordinate system has the positive z axis pointing up 
     14static Vector3 baseDir = Vector3(0, 1, 0); 
    1715 
    1816 
     
    2725 
    2826        mPitch = mYaw = 0; 
    29         Precompute(startVector); 
     27        Precompute(baseDir); 
    3028 
    3129        CalculateFromPitchAndYaw(); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp

    r2916 r2917  
    655655        Matrix4x4 m; 
    656656 
    657         m.x[0][0] = -2.0f / (box.Max()[0] - box.Min()[0]); 
    658         m.x[0][1] = .0f; 
    659         m.x[0][2] = .0f; 
    660         m.x[0][3] = .0f; 
    661  
    662         m.x[1][0] = .0f; 
    663         m.x[1][1] = -2.0f / (box.Max()[1] - box.Min()[1]); 
    664         m.x[1][2] = .0f; 
    665         m.x[1][3] = .0f; 
    666  
    667         m.x[2][0] = .0f; 
    668         m.x[2][1] = .0f; 
    669         m.x[2][2] = -2.0f / (box.Max()[2] - box.Min()[2]); 
    670         m.x[2][3] = .0f; 
    671  
    672         m.x[3][0] = (box.Max()[0] + box.Min()[0]) / (box.Max()[0] - box.Min()[0]); 
    673         m.x[3][1] = (box.Max()[1] + box.Min()[1]) / (box.Max()[1] - box.Min()[1]); 
    674         m.x[3][2] = (box.Max()[2] + box.Min()[2]) / (box.Max()[2] - box.Min()[2]); 
    675         m.x[3][3] = 1.0f; 
    676  
    677         return m; 
    678 } 
    679  
    680 /* 
    681 Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box) 
    682 { 
    683         Matrix4x4 m; 
    684  
    685657        m.x[0][0] = 2.0f / (box.Max()[0] - box.Min()[0]); 
    686658        m.x[0][1] = .0f; 
     
    704676 
    705677        return m; 
    706 }*/ 
     678} 
     679 
    707680 
    708681//output is initialized with the same result as glFrustum 
     
    734707        m.x[3][2] = 2.0f * near * far * zDif; 
    735708        m.x[3][3] = .0f; 
    736 } 
    737  
    738  
    739  
    740 } 
     709 
     710        return m; 
     711} 
     712 
     713 
     714Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up)  
     715{ 
     716        const Vector3 nDir = Normalize(dir); 
     717         
     718        Vector3 nUp = Normalize(up); 
     719 
     720        Vector3 nRight = Normalize(CrossProd(nDir, nUp)); 
     721        nUp = Normalize(CrossProd(nRight, nDir)); 
     722 
     723        Matrix4x4 m; 
     724        Matrix4x4(nRight, nUp, -nDir); 
     725 
     726        m.x[3][0] = -DotProd(nRight, pos); 
     727        m.x[3][1] = -DotProd(nUp, pos); 
     728        m.x[3][2] = DotProd(nDir, pos); 
     729 
     730        return m; 
     731} 
     732 
     733} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h

    r2916 r2917  
    114114        friend Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); 
    115115 
     116        // look from position into given direction with given up vector 
     117        Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); 
     118 
    116119        // Overloaded output operator. 
    117120        friend std::ostream& operator<< (std::ostream &s, const Matrix4x4 &M); 
     
    156159Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box); 
    157160Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); 
    158  
     161Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); 
    159162} 
    160163 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp

    r2916 r2917  
    180180 
    181181 
    182 Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView) 
    183 {/* 
    184         Matrix4x4 lispMtx = IdentityMatrix(); 
    185  
    186         const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection()); 
    187         const float sinGamma = sqrt(1.0f - dotProd * dotProd); 
     182Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView, const AxisAlignedBox3 &extremalPoints) 
     183{ 
     184        Matrix4x4 lispMtx; 
     185 
     186        /////////////// 
     187        //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix 
     188 
     189        const float n = 1e20f;//ComputeN(); 
     190 
     191        cout << "n: " << n << endl; 
     192 
     193        const Vector3 nearPt = mShadowCam->GetNear() * mShadowCam->GetDirection() + mShadowCam->GetPosition(); 
     194 
     195        //get the coordinates of the near camera point in light space 
     196        const Vector3 lsNear = lightProjView * nearPt; 
     197 
     198        //c start has the x and y coordinate of e, the z coord of B.min() 
     199        const Vector3 startPt = Vector3(lsNear.x, lsNear.y, extremalPoints.Min().y); 
     200 
     201        // the new projection center 
     202        const Vector3 unit_y = Vector3(0, 1, 0); 
     203        Vector3 projCenter = startPt - unit_y * n; 
     204 
     205        //construct a translation that moves to the projection center 
     206        const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter); 
     207 
     208        // light space y size 
     209        const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);  
     210 
     211        lispMtx = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d); 
     212 
     213        cout << "lispsm\n" << lispMtx << endl; 
     214 
     215        lispMtx *= projectionCenter; 
     216 
     217        // transform into OpenGL right handed system 
     218        Matrix4x4 scale = ScaleMatrix(1.0f, 1.0f, -1.0f); 
     219 
     220        lispMtx *= scale; 
     221         
     222        return IdentityMatrix(); 
     223} 
     224 
     225 
     226bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj) 
     227{ 
     228        DEL_PTR(polyhedron); 
    188229 
    189230 
     
    191232        //-- First step: calc frustum clipped by scene box 
    192233 
    193         DEL_PTR(polyhedron); 
    194234        polyhedron = CalcClippedFrustum(mSceneBox); 
    195235 
     
    222262        } 
    223263 
    224  
    225         /////////////// 
    226         //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix 
    227  
    228         const float n = 1e20f;//ComputeN(); 
    229  
    230         cout << "n: " << n << endl; 
    231  
    232         const Vector3 nearPt = mShadowCam->GetNear() * mShadowCam->GetDirection() + mShadowCam->GetPosition(); 
    233  
    234         //get the coordinates of the near camera point in light space 
    235         const Vector3 lsNear = lightView * nearPt; 
    236  
    237         //c start has the x and y coordinate of e, the z coord of B.min() 
    238         const Vector3 startPt = Vector3(lsNear.x, lsNear.y, extremalPoints.Min().y); 
    239  
    240         // the new projection center 
    241         const Vector3 unit_y = Vector3(0, 1, 0); 
    242         Vector3 projCenter = startPt - unit_y * n; 
    243  
    244         //construct a translation that moves to the projection center 
    245         const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter); 
    246  
    247         // light space y size 
    248         const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);  
    249  
    250         lightProj = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d); 
    251  
    252         cout << "here4\n" << lightProj << endl; 
    253  
    254         // transform into OpenGL right handed system 
    255         Matrix4x4 scale = ScaleMatrix(1.0f, 1.0f, -1.0f); 
    256  
    257         lightProj = projectionCenter * scale * lightProj; 
    258  
    259         Vector3 pmax = extremalPoints.Max(); 
    260         Vector3 pmin = extremalPoints.Min(); 
    261  
    262         cout << "min: " << lightProj * pmin << endl; 
    263         cout << "max: " << lightProj * pmax << endl; 
    264          
    265 */ 
    266         return IdentityMatrix(); 
    267 } 
    268  
    269  
    270 bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj) 
    271 { 
    272         DEL_PTR(polyhedron); 
    273  
    274  
    275         /////////////////// 
    276         //-- First step: calc frustum clipped by scene box 
    277  
    278         polyhedron = CalcClippedFrustum(mSceneBox); 
    279  
    280         if (!polyhedron) return false; // something is wrong 
    281  
    282         // include the part of the light volume that "sees" the frustum 
    283         // we only require frustum vertices 
    284  
    285         VertexArray frustumPoints; 
    286         IncludeLightVolume(*polyhedron, frustumPoints, mShadowCam->GetDirection(), mSceneBox); 
    287  
    288  
    289         /////////////// 
    290         //-- transform points from world view to light view and calculate extremal points 
    291  
    292         AxisAlignedBox3 extremalPoints; 
    293         extremalPoints.Initialize(); 
    294  
    295         Matrix4x4 lightView; 
    296         mShadowCam->GetModelViewMatrix(lightView); 
    297  
    298         VertexArray::const_iterator it, it_end = frustumPoints.end(); 
    299                  
    300         for (it = frustumPoints.begin(); it != it_end; ++ it) 
    301         { 
    302                 Vector3 pt = *it; 
    303                 pt = lightView * pt; 
    304  
    305                 extremalPoints.Include(pt); 
    306         } 
    307  
    308         //Matrix4x4 m = CalcLispSMTransform(lightProj); 
    309  
    310         // focus projection matrix on the extremal points 
    311         lightProj = GetFittingProjectionMatrix(extremalPoints); 
    312  
    313         //Matrix4x4 scale = ScaleMatrix(1, 1, -1); lightProj *= scale; 
     264        lightProj = IdentityMatrix(); // we use directional lights 
     265 
     266        Matrix4x4 shadowView; 
     267        mShadowCam->GetModelViewMatrix(shadowView); 
     268 
     269        //do Light Space Perspective shadow mapping 
     270        //rotate the lightspace so that the proj light view always points upwards 
     271        //calculate a frame matrix that uses the projViewDir[light-space] as up vector 
     272        //look(from position, into the direction of the projected direction, with unchanged up-vector) 
     273        //lightProj = Math::look<REAL>(M4(),V3::ZERO(),projViewDir,V3::UNIT_Y())*lightProj 
     274 
     275        const Matrix4x4 matLispSM =  
     276                CalcLispSMTransform(shadowView * lightProj, extremalPoints); 
     277 
     278        lightProj *= matLispSM; 
     279 
     280        // focus projection matrix on the extremal points => scale to unit cube 
     281        lightProj *= GetFittingProjectionMatrix(extremalPoints); 
     282 
     283        // we have to flip the signs in order tp opengl style projection matrix 
     284        Matrix4x4 scale = ScaleMatrix(-1, -1, -1);  
     285        lightProj *= scale; 
    314286         
    315287        return true; 
     
    339311        sides[5][0] = ftl; sides[5][1] = ftr; sides[5][2] = fbr; sides[5][3] = fbl;  
    340312 
    341         //sides[4][0] = ntl; sides[4][1] = ntr; sides[4][2] = nbr; sides[4][3] = nbl;  
    342         //sides[5][0] = ftr; sides[5][1] = ftl; sides[5][2] = fbl; sides[5][3] = fbr; 
    343  
    344  
    345  
    346313        ////////// 
    347314        //-- compute polyhedron 
     
    424391        glLoadMatrixf((float *)lightProj.x); 
    425392 
    426         cout << "new:\n" << lightProj << endl; 
     393        mLightProjView = lightView * lightProj; 
     394 
     395        //cout << "new:\n" << lightProj << endl; 
    427396 
    428397        glMatrixMode(GL_MODELVIEW); 
     
    431400 
    432401        mShadowCam->SetupCameraView(); 
    433  
    434         mLightProjView = lightView * lightProj; 
    435402 
    436403 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2916 r2917  
    6565         
    6666        bool CalcLightProjection(Matrix4x4 &lightProj); 
    67         Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView); 
     67        Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView, const AxisAlignedBox3 &bounds); 
    6868 
    6969        void IncludeLightVolume(const Polyhedron &polyhedron,  
Note: See TracChangeset for help on using the changeset viewer.