Changeset 2915


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

Legend:

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

    r2913 r2915  
    676676 
    677677        m.x[0][0] = -2.0f / (box.Max()[0] - box.Min()[0]); 
     678        m.x[0][1] = .0f; 
     679        m.x[0][2] = .0f; 
     680        m.x[0][3] = .0f; 
     681 
    678682        m.x[1][0] = .0f; 
     683        m.x[1][1] = -2.0f / (box.Max()[1] - box.Min()[1]); 
     684        m.x[1][2] = .0f; 
     685        m.x[1][3] = .0f; 
     686 
    679687        m.x[2][0] = .0f; 
     688        m.x[2][1] = .0f; 
     689        m.x[2][2] = -2.0f / (box.Max()[2] - box.Min()[2]); 
     690        m.x[2][3] = .0f; 
     691 
    680692        m.x[3][0] = (box.Max()[0] + box.Min()[0]) / (box.Max()[0] - box.Min()[0]); 
    681  
     693        m.x[3][1] = (box.Max()[1] + box.Min()[1]) / (box.Max()[1] - box.Min()[1]); 
     694        m.x[3][2] = (box.Max()[2] + box.Min()[2]) / (box.Max()[2] - box.Min()[2]); 
     695        m.x[3][3] = 1.0f; 
     696 
     697        return m; 
     698} 
     699 
     700 
     701//output is initialized with the same result as glFrustum 
     702Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far) 
     703{ 
     704        Matrix4x4 m; 
     705 
     706        const float xDif = 1.0f / (right - left); 
     707        const float yDif = 1.0f / (top - bottom); 
     708        const float zDif = 1.0f / (near - far); 
     709 
     710        m.x[0][0] = 2.0f * near * xDif; 
    682711        m.x[0][1] = .0f; 
    683         m.x[1][1] = -2.0f / (box.Max()[1] - box.Min()[1]); 
    684         m.x[2][1] = .0f; 
    685         m.x[3][1] = (box.Max()[1] + box.Min()[1]) / (box.Max()[1] - box.Min()[1]); 
    686  
    687712        m.x[0][2] = .0f; 
     713        m.x[0][3] = .0f; 
     714 
     715        m.x[1][0] = .0f; 
     716        m.x[1][1] = 2.0f * near * yDif; 
    688717        m.x[1][2] = .0f; 
    689         m.x[2][2] = -2.0f / (box.Max()[2] - box.Min()[2]); 
    690         m.x[3][2] = (box.Max()[2] + box.Min()[2]) / (box.Max()[2] - box.Min()[2]); 
    691  
    692         m.x[0][3] = .0f; 
    693718        m.x[1][3] = .0f; 
    694         m.x[2][3] = .0f; 
    695         m.x[3][3] = 1.0f; 
    696  
    697         return m; 
    698 } 
    699  
    700  
    701 } 
     719 
     720        m.x[2][0] = (right + left) * xDif; 
     721        m.x[2][1] = (top + bottom)* yDif; 
     722        m.x[2][2] = (far + near) * zDif; 
     723        m.x[2][3] = -1.0f; 
     724 
     725        m.x[3][0] = .0f; 
     726        m.x[3][1] = .0f; 
     727        m.x[3][2] = 2.0f * near * far * zDif; 
     728        m.x[3][3] = .0f; 
     729} 
     730 
     731 
     732 
     733} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h

    r2913 r2915  
    111111        friend Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box); 
    112112 
     113        //output is initialized with the same result as glFrustum 
     114        friend Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); 
     115 
    113116        // Overloaded output operator. 
    114117        friend std::ostream& operator<< (std::ostream &s, const Matrix4x4 &M); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp

    r2914 r2915  
    144144        VertexArray::const_iterator it, it_end = vertices.end(); 
    145145 
    146         //cout << "=================" << endl; 
    147  
    148146        for (it = vertices.begin(); it != it_end; ++ it) 
    149147        { 
     
    155153                v -= Magnitude(mSceneBox.Diagonal()) * lightDir; 
    156154 
    157                 //cout << "pt: " << v << endl; 
    158155                SimpleRay ray(v, lightDir); 
    159156 
     
    163160                { 
    164161                        Vector3 newpt = ray.Extrap(tNear); 
    165                         Vector3 newpt2 = ray.Extrap(tFar); 
    166  
    167                         frustumPoints.push_back(newpt); 
    168  
    169                         //if (newpt.z < 220) cout << "ipt: " << newpt << " " << *it << endl; 
     162                        frustumPoints.push_back(newpt);                  
    170163                } 
    171164        } 
     
    173166 
    174167 
    175 bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj) 
    176 { 
    177         DEL_PTR(polyhedron); 
     168bool ShadowMap::CalcLightProjectionLispSM(Matrix4x4 &lightProj) 
     169{ 
     170        Matrix4x4 lispMtx = IdentityMatrix(); 
     171 
     172        const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection()); 
     173        const float sinGamma = sqrt(1.0f - dotProd * dotProd); 
    178174 
    179175 
     
    181177        //-- First step: calc frustum clipped by scene box 
    182178 
     179        DEL_PTR(polyhedron); 
    183180        polyhedron = CalcClippedFrustum(mSceneBox); 
    184181 
     
    200197        Matrix4x4 lightView; 
    201198        mShadowCam->GetModelViewMatrix(lightView); 
    202  
    203         //Matrix4x4 inverseCamView; 
    204         //mCamera->GetModelViewMatrix(inverseCamView); 
    205         //Invert(inverseCamView); 
    206  
    207         //Matrix4x4 m = inverseCamView * lightView; 
    208199 
    209200        VertexArray::const_iterator it, it_end = frustumPoints.end(); 
     
    212203        { 
    213204                Vector3 pt = *it; 
    214  
    215                 //pt = inverseCamView * pt; 
    216205                pt = lightView * pt; 
    217                 //v = m * v; 
    218206 
    219207                extremalPoints.Include(pt); 
    220                 //cout << "pt: " << pt << endl; 
     208        } 
     209 
     210 
     211        /////////////// 
     212        //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix 
     213 
     214        // use lispsm formulas 
     215        const float fac = 1.0f / sinGamma; 
     216         
     217        const float z_n = fac * mCamera->GetNear(); 
     218 
     219        // light space y size 
     220        const float d = fabs(extremalPoints.Max()[1] - extremalPoints.Min()[1]);  
     221 
     222        const float z_f = z_n + d * sinGamma; 
     223 
     224        // this is the famous n parameter 
     225        const float n = (z_n + sqrt(z_f * z_n)) /sinGamma; 
     226        const float f = n + d; 
     227 
     228        const Vector3 nearPt = mShadowCam->GetNear() * mShadowCam->GetDirection() + mShadowCam->GetPosition(); 
     229 
     230        //get the coordinates of the near camera point in light space 
     231        const Vector3 lsNear = lightView * nearPt; 
     232 
     233        //c start has the x and y coordinate of e, the z coord of B.min() 
     234        const Vector3 startPt = Vector3(lsNear.x, lsNear.y, extremalPoints.Max().z); 
     235 
     236        // the new projection center 
     237        Vector3 projCenter = startPt - Vector3(0, 0, 1) * n; 
     238 
     239        //construct a translation that moves to the projection center 
     240        const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter); 
     241 
     242        lightProj = IdentityMatrix(); 
     243 
     244        //one possibility for a simple perspective transformation matrix 
     245        //with the two parameters n(near) and f(far) in y direction 
     246         
     247        // a = (f+n)/(f-n); b = -2*f*n/(f-n); 
     248        // [ 1 0 0 0] 
     249        // [ 0 a 0 b] 
     250        // [ 0 0 1 0] 
     251        // [ 0 1 0 0] 
     252 
     253        lightProj.x[1][1] = (f + n) / (f - n);            
     254        lightProj.x[1][3] = 1.0f; 
     255 
     256        lightProj.x[3][1] = -2.0f * f * n / (f - n);             
     257        lightProj.x[3][3] = 0.0f;                                
     258 
     259        cout << "here4\n" << lightProj << endl; 
     260 
     261        // transform into OpenGL right handed system 
     262        Matrix4x4 scale = ScaleMatrix(1.0f, 1.0f, -1.0f); 
     263 
     264        lightProj = projectionCenter * scale * lightProj; 
     265 
     266        Vector3 pmax = extremalPoints.Max(); 
     267        Vector3 pmin = extremalPoints.Min(); 
     268 
     269        cout << "min: " << lightProj * pmin << endl; 
     270        cout << "max: " << lightProj * pmax << endl; 
     271         
     272 
     273        return true; 
     274} 
     275 
     276 
     277bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj) 
     278{ 
     279        DEL_PTR(polyhedron); 
     280 
     281 
     282        /////////////////// 
     283        //-- First step: calc frustum clipped by scene box 
     284 
     285        polyhedron = CalcClippedFrustum(mSceneBox); 
     286 
     287        if (!polyhedron) return false; // something is wrong 
     288 
     289        // include the part of the light volume that "sees" the frustum 
     290        // we only require frustum vertices 
     291 
     292        VertexArray frustumPoints; 
     293        IncludeLightVolume(*polyhedron, frustumPoints, mShadowCam->GetDirection(), mSceneBox); 
     294 
     295 
     296        /////////////// 
     297        //-- transform points from world view to light view and calculate extremal points 
     298 
     299        AxisAlignedBox3 extremalPoints; 
     300        extremalPoints.Initialize(); 
     301 
     302        Matrix4x4 lightView; 
     303        mShadowCam->GetModelViewMatrix(lightView); 
     304 
     305        VertexArray::const_iterator it, it_end = frustumPoints.end(); 
     306                 
     307        for (it = frustumPoints.begin(); it != it_end; ++ it) 
     308        { 
     309                Vector3 pt = *it; 
     310                pt = lightView * pt; 
     311 
     312                extremalPoints.Include(pt); 
    221313        } 
    222314 
    223315        // focus projection matrix on the extremal points 
    224316        lightProj = GetFittingProjectionMatrix(extremalPoints); 
    225  
    226         /*Vector3 pmax = extremalPoints.Max(); 
    227         Vector3 pmin = extremalPoints.Min(); 
    228  
    229         cout << "min: " << lightProj * pmin << endl; 
    230         cout << "max: " << lightProj * pmax << endl; 
    231          
    232         for (it = frustumPoints.begin(); it != it_end; ++ it) 
    233         { 
    234                 Vector3 pt = *it; 
    235                  
    236                 pt = lightView * pt; 
    237                 pt = lightProj * pt; 
    238  
    239                 cout << "pt: " << pt << endl; 
    240         }*/ 
    241317 
    242318        return true; 
     
    283359 
    284360        Polyhedron *p = new Polyhedron(polygons); 
    285         //return p; 
    286          
    287361        Polyhedron *clippedPolyhedron = box.CalcIntersection(*p); 
    288362         
     
    348422        mShadowCam->GetModelViewMatrix(lightView); 
    349423 
    350         CalcLightProjection(lightProj); 
     424        //CalcLightProjection(lightProj); 
     425        CalcLightProjectionLispSM(lightProj); 
    351426 
    352427        glMatrixMode(GL_PROJECTION); 
     
    354429        glLoadMatrixf((float *)lightProj.x); 
    355430 
    356         //cout << "new:\n" << lightProj << endl; 
     431        cout << "new:\n" << lightProj << endl; 
    357432 
    358433        glMatrixMode(GL_MODELVIEW); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2913 r2915  
    6363        */ 
    6464        Polyhedron *CalcClippedFrustum(const AxisAlignedBox3 &box) const; 
    65  
    66  
     65         
     66        bool CalcLightProjectionLispSM(Matrix4x4 &lightProj); 
    6767        bool CalcLightProjection(Matrix4x4 &lightProj); 
    6868 
Note: See TracChangeset for help on using the changeset viewer.