Ignore:
Timestamp:
09/07/08 13:44:28 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2899 r2911  
    44#include "RenderTraverser.h" 
    55#include "Light.h" 
     6#include "Polygon3.h" 
     7#include "Polyhedron.h" 
     8 
    69#include <IL/il.h> 
    710#include <assert.h> 
    811 
     12 
    913using namespace std; 
    1014 
     
    1216namespace CHCDemoEngine 
    1317{ 
     18 
     19 
     20static Polyhedron *polyhedron = NULL; 
    1421 
    1522 
     
    97104 
    98105 
    99 void ShadowMap::ComputeShadowMap(RenderTraverser *renderer) 
     106void ShadowMap::DrawPolys() 
     107{ 
     108        if (!polyhedron) return; 
     109 
     110        cout << "here3" << endl; 
     111 
     112        for (size_t i = 0; i < polyhedron->NumPolygons(); ++ i) 
     113        { 
     114                float r = (float)i / polyhedron->NumPolygons(); 
     115                float g = 1; 
     116                float b = 1; 
     117 
     118                glColor3f(r, g, b); 
     119 
     120                glBegin(GL_LINE_LOOP); 
     121 
     122                Polygon3 *poly = polyhedron->GetPolygons()[i]; 
     123 
     124                for (size_t j = 0; j < poly->mVertices.size(); ++ j) 
     125                { 
     126                        Vector3 v = poly->mVertices[j]; 
     127                        glVertex3d(v.x, v.y, v.z); 
     128                } 
     129 
     130                glEnd(); 
     131        } 
     132} 
     133 
     134 
     135void ShadowMap::CalcFocussedFrustum(const Matrix4x4 &camProjView,  
     136                                                                        const Matrix4x4 &lightProjView,  
     137                                                                        Matrix4x4 &focussed) 
     138{ 
     139        /////////////////// 
     140        //-- First step: calc clipped frustum 
     141 
     142        DEL_PTR(polyhedron); 
     143        polyhedron = mCamera->CalcClippedFrustum(mSceneBox); 
     144 
     145        if (polyhedron) 
     146        { 
     147                // second step: focus light frustum on clipped frustum 
     148                Frustum lightFrustum(lightProjView); 
     149 
     150                for (int i = 0; i < 6; ++ i) 
     151                { 
     152                        // the clipping planes look outward the frustum, 
     153                        // so distances > 0 mean that a point is outside 
     154                        const float invLength = 1.0f / Magnitude(lightFrustum.mClipPlanes[i].mNormal); 
     155 
     156                        lightFrustum.mClipPlanes[i].mD *= invLength; 
     157                        lightFrustum.mClipPlanes[i].mNormal *= invLength; 
     158                } 
     159 
     160                //lightFrustum.EnclosePolyhedron(*polyhedron); 
     161                lightFrustum.ExtractTransformation(focussed); 
     162        } 
     163} 
     164 
     165 
     166void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView) 
    100167{ 
    101168        const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f); 
     
    107174        Vector3 pos = mSceneBox.Center(); 
    108175 
     176        Matrix4x4 camView; 
     177        mCamera->GetModelViewMatrix(camView); 
     178 
    109179        pos -= mLight->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f); 
    110         mShadowCam->SetPosition(pos); 
     180        //mShadowCam->SetPosition(pos); 
    111181 
    112182        mFbo->Bind(); 
     
    153223        mShadowCam->GetProjectionMatrix(lightProj); 
    154224 
     225        Matrix4x4 oldLightProjView = lightView * lightProj; 
     226        Matrix4x4 newLightProj; 
     227 
     228        CalcFocussedFrustum(projView, lightProj, newLightProj); 
     229        //CalcFocussedFrustum(projView, oldLightProjView, newLightProj); 
     230 
     231        Matrix4x4 dummyMat; 
     232        glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat.x); 
     233 
     234 
     235        cout << "old:\n" << dummyMat << endl; 
     236 
     237 
     238        glMatrixMode(GL_PROJECTION); 
     239        glLoadMatrixf((float *)newLightProj.x); 
     240         
     241        glMatrixMode(GL_MODELVIEW); 
     242        //glLoadIdentity(); 
     243 
     244        Matrix4x4 dummyMat3; 
     245        glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat3.x); 
     246 
     247        cout << "new:\n" << newLightProj << endl; 
     248 
     249        lightView = camView; 
     250        mLightProjView = lightView * newLightProj; 
     251        //mLightProjView = newLightProj; 
     252 
     253        //cout << "old: \n" << oldLightProjView << " new: \n " << mLightProjView << endl; 
     254         
     255 
    155256        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f, 
    156257                                                                0.0f, 0.5f, 0.0f, 0.5f, 
    157258                                                                0.0f, 0.0f, 0.5f, 0.5f, 
    158                                                                 0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1] 
    159  
    160         Matrix4x4 lightProjView = lightView * lightProj; 
    161         mTextureMatrix = lightProjView * biasMatrix;  
     259                                                                0.0f, 0.0f, 0.0f, 1.0f); 
     260 
     261        mTextureMatrix = mLightProjView * biasMatrix;  
     262        //mTextureMatrix = oldLightProjView * biasMatrix;  
     263 
     264 
    162265 
    163266 
     
    176279 
    177280        glPopAttrib(); 
    178          
    179         /*float *data = new float[mSize * mSize]; 
     281        /* 
     282        float *data = new float[mSize * mSize]; 
    180283 
    181284        GrabDepthBuffer(data, mFbo->GetDepthTex()); 
     
    183286 
    184287        delete [] data; 
    185         */ 
     288         
    186289        PrintGLerror("shadow map"); 
    187  
     290*/ 
    188291        FrameBufferObject::Release(); 
    189292} 
Note: See TracChangeset for help on using the changeset viewer.