Changeset 2931


Ignore:
Timestamp:
09/11/08 17:31:36 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
7 edited

Legend:

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

    r2928 r2931  
    337337 
    338338 
    339  
    340 } 
    341  
     339Polyhedron *Camera::ComputeFrustum() const 
     340{ 
     341        Vector3 ftl, ftr, fbl, fbr; 
     342        Vector3 ntl, ntr, nbl, nbr; 
     343 
     344        VertexArray sides[6]; 
     345 
     346        ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
     347 
     348        for (int i = 0; i < 6; ++ i) 
     349                sides[i].resize(4); 
     350         
     351        // left, right 
     352        sides[0][0] = ftl; sides[0][1] = fbl; sides[0][2] = nbl; sides[0][3] = ntl; 
     353        sides[1][0] = fbr; sides[1][1] = ftr; sides[1][2] = ntr; sides[1][3] = nbr; 
     354        // bottom, top 
     355        sides[2][0] = fbl; sides[2][1] = fbr; sides[2][2] = nbr; sides[2][3] = nbl; 
     356        sides[3][0] = ftr; sides[3][1] = ftl; sides[3][2] = ntl; sides[3][3] = ntr; 
     357        // near, far 
     358        sides[4][0] = ntr; sides[4][1] = ntl; sides[4][2] = nbl; sides[4][3] = nbr;  
     359        sides[5][0] = ftl; sides[5][1] = ftr; sides[5][2] = fbr; sides[5][3] = fbl;  
     360 
     361 
     362        ////////// 
     363        //-- compute polyhedron 
     364 
     365        PolygonContainer polygons; 
     366 
     367        for (int i = 0; i < 6; ++ i) 
     368        { 
     369                Polygon3 *poly = new Polygon3(sides[i]); 
     370                polygons.push_back(poly); 
     371        } 
     372 
     373        return new Polyhedron(polygons); 
     374} 
     375 
     376 
     377} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h

    r2913 r2931  
    108108        */ 
    109109        void SetDirection(const Vector3 &direction); 
     110        /** Returns frustum as polyhedron. 
     111        */ 
     112        Polyhedron *Camera::ComputeFrustum() const; 
    110113 
    111114protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp

    r2929 r2931  
    1515 
    1616 
    17 /**********************************************************/ 
    18 /*             class Polyhedron Implementation             */ 
    19 /**********************************************************/ 
     17/************************************************************/ 
     18/*              class Polyhedron Implementation             */ 
     19/************************************************************/ 
    2020 
    2121 
     
    5757 
    5858        if (!oldPolyhedron->Valid()) cerr << "******************* not valid!! ************* " << endl; 
    59  
    60         //oldPolyhedron->mBox = sceneBox; 
    6159 
    6260        vector<Plane3>::const_iterator it, it_end = planes.end(); 
     
    7775                } 
    7876 
    79                 // hack: set bounding box temporarily to scene bounding box 
    80                 //newPolyhedron->mBox = sceneBox; 
    81  
    8277                DEL_PTR(oldPolyhedron); 
    8378                oldPolyhedron = newPolyhedron; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp

    r2929 r2931  
    1717{ 
    1818 
     19static CGprogram sCgShadowProgram; 
     20static CGparameter sShadowParam; 
     21 
    1922 
    2023static Polyhedron *polyhedron = NULL; 
     
    3538 
    3639 
    37  
    38 static CGprogram sCgShadowProgram; 
    39 static CGparameter sShadowParam; 
     40static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix, const AxisAlignedBox3 &sceneBox) 
     41{ 
     42        Frustum frustum(lightMatrix); 
     43 
     44        vector<Plane3> clipPlanes; 
     45 
     46        for (int i = 0; i < 6; ++ i) 
     47        { 
     48                //////////// 
     49                //-- normalize the coefficients 
     50 
     51                // the clipping planes look outward the frustum, 
     52                // so distances > 0 mean that a point is outside 
     53                const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal); 
     54 
     55                frustum.mClipPlanes[i].mD *= invLength; 
     56                frustum.mClipPlanes[i].mNormal *= invLength; 
     57 
     58                //clipPlanes.push_back(frustum.mClipPlanes[i]); 
     59        } 
     60 
     61        clipPlanes.push_back(frustum.mClipPlanes[5]); 
     62        clipPlanes.push_back(frustum.mClipPlanes[0]); 
     63        clipPlanes.push_back(frustum.mClipPlanes[1]); 
     64        clipPlanes.push_back(frustum.mClipPlanes[2]); 
     65        clipPlanes.push_back(frustum.mClipPlanes[3]); 
     66        clipPlanes.push_back(frustum.mClipPlanes[4]); 
     67 
     68        return Polyhedron::CreatePolyhedron(clipPlanes, sceneBox); 
     69} 
    4070 
    4171 
     
    182212        //-- first find the free parameter values n, and P (the projection center), and the projection depth 
    183213 
    184         //const float n = 1e6f; 
    185         const float n = 1e3f; 
     214        //const float n = 1e2f; 
     215        const float n = 1e6f; 
    186216        //const float n = ComputeN(bounds_ls) * 100; 
    187217 
     
    431461Polyhedron *ShadowMap::CalcClippedFrustum(const AxisAlignedBox3 &box) const 
    432462{ 
    433         Vector3 ftl, ftr, fbl, fbr; 
    434         Vector3 ntl, ntr, nbl, nbr; 
    435  
    436         VertexArray sides[6]; 
    437  
    438         mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
    439  
    440         for (int i = 0; i < 6; ++ i) 
    441                 sides[i].resize(4); 
    442          
    443         // left, right 
    444         sides[0][0] = ftl; sides[0][1] = fbl; sides[0][2] = nbl; sides[0][3] = ntl; 
    445         sides[1][0] = fbr; sides[1][1] = ftr; sides[1][2] = ntr; sides[1][3] = nbr; 
    446         // bottom, top 
    447         sides[2][0] = fbl; sides[2][1] = fbr; sides[2][2] = nbr; sides[2][3] = nbl; 
    448         sides[3][0] = ftr; sides[3][1] = ftl; sides[3][2] = ntl; sides[3][3] = ntr; 
    449         // near, far 
    450         sides[4][0] = ntr; sides[4][1] = ntl; sides[4][2] = nbl; sides[4][3] = nbr;  
    451         sides[5][0] = ftl; sides[5][1] = ftr; sides[5][2] = fbr; sides[5][3] = fbl;  
    452  
    453         ////////// 
    454         //-- compute polyhedron 
    455  
    456         PolygonContainer polygons; 
    457  
    458         for (int i = 0; i < 6; ++ i) 
    459         { 
    460                 Polygon3 *poly = new Polygon3(sides[i]); 
    461                 polygons.push_back(poly); 
    462         } 
    463  
    464         Polyhedron *p = new Polyhedron(polygons); 
     463        Polyhedron *p = mCamera->ComputeFrustum(); 
     464         
    465465        Polyhedron *clippedPolyhedron = box.CalcIntersection(*p); 
    466          
     466 
    467467        DEL_PTR(p); 
    468468         
    469  
    470469        return clippedPolyhedron; 
     470} 
     471 
     472 
     473//calculates the up vector for the light coordinate frame 
     474static Vector3 CalcUpVec(const Vector3 viewDir, const Vector3 lightDir)  
     475{ 
     476        //we do what gluLookAt does... 
     477        //left is the normalized vector perpendicular to lightDir and viewDir 
     478        //this means left is the normalvector of the yz-plane from the paper 
     479        Vector3 left = CrossProd(lightDir, viewDir); 
     480         
     481        //we now can calculate the rotated(in the yz-plane) viewDir vector 
     482        //and use it as up vector in further transformations 
     483        Vector3 up = CrossProd(left, lightDir); 
     484 
     485        return Normalize(up); 
    471486} 
    472487 
     
    479494        //const Vector3 dir = mLight->GetDirection(); 
    480495        const Vector3 dir(0, 0, -1); 
     496 
     497        Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 
     498        mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec); 
    481499 
    482500        mShadowCam->SetDirection(dir); 
     
    526544        Matrix4x4 lightView, lightProj; 
    527545 
    528         mShadowCam->GetModelViewMatrix(lightView); 
     546        //mShadowCam->GetModelViewMatrix(lightView); 
     547        lightView = mLightMatrix; 
     548 
    529549        CalcLightProjection(lightProj); 
    530550 
     
    535555        mLightProjView = lightView * lightProj; 
    536556 
    537         Frustum frustum(mLightProjView); 
    538         //Frustum frustum(projView); 
    539  
    540557        DEL_PTR(lightPoly); 
    541  
    542         vector<Plane3> clipPlanes; 
    543  
    544         for (int i = 0; i < 6; ++ i) 
    545         { 
    546                 frustum.mClipPlanes[i].mNormal *= -1; 
    547                 frustum.mClipPlanes[i].mD *= -1; 
    548  
    549                 clipPlanes.push_back(frustum.mClipPlanes[i]); 
    550         } 
    551  
    552         DEL_PTR(lightPoly); 
    553         lightPoly = Polyhedron::CreatePolyhedron(clipPlanes, mSceneBox); 
     558        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 
    554559 
    555560        //cout << "new:\n" << lightProj << endl; 
     
    559564        glLoadIdentity(); 
    560565 
    561         mShadowCam->SetupCameraView(); 
    562  
     566        //mShadowCam->SetupCameraView(); 
     567        glLoadMatrixf((float *)mLightMatrix.x); 
    563568 
    564569        ////////////// 
     
    606611        FrameBufferObject::Release(); 
    607612} 
     613 
    608614 
    609615 
     
    661667 
    662668 
     669void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView) 
     670{ 
     671        //const Vector3 dir = mLight->GetDirection(); 
     672        const Vector3 dir(0, 0, -1); 
     673 
     674        mShadowCam->SetDirection(dir); 
     675 
     676        Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 
     677        mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec); 
     678 
     679        // set position so that we can see the whole scene 
     680        Vector3 pos = mSceneBox.Center(); 
     681 
     682        pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f); 
     683        mShadowCam->SetPosition(pos); 
     684 
     685        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     686 
     687        glEnable(GL_LIGHTING); 
     688        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     689 
     690        glPolygonOffset(1.0f, 2000.0f); 
     691        glEnable(GL_POLYGON_OFFSET_FILL); 
     692glDisable(GL_CULL_FACE); 
     693        glEnable(GL_DEPTH_TEST); 
     694 
     695        Matrix4x4 lightView, lightProj; 
     696         
     697        //mShadowCam->GetModelViewMatrix(lightView); 
     698        lightView = mLightMatrix; 
     699        CalcLightProjection(lightProj); 
     700 
     701        glMatrixMode(GL_PROJECTION); 
     702        glPushMatrix(); 
     703        glLoadMatrixf((float *)lightProj.x); 
     704 
     705        mLightProjView = lightView * lightProj; 
     706 
     707        DEL_PTR(lightPoly); 
     708        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 
     709 
     710        //cout << "new:\n" << lightProj << endl; 
     711 
     712        glMatrixMode(GL_MODELVIEW); 
     713        glPushMatrix(); 
     714        glLoadIdentity(); 
     715 
     716        //mShadowCam->SetupCameraView(); 
     717        glLoadMatrixf((float *)mLightMatrix.x); 
     718 
     719 
     720        ///////////// 
     721        //-- render scene into shadow map 
     722 
     723        renderer->RenderScene(); 
     724 
     725         
     726        glDisable(GL_POLYGON_OFFSET_FILL); 
     727 
     728        glPushAttrib(GL_CURRENT_BIT); 
     729        glDisable(GL_LIGHTING); 
     730        glDisable(GL_DEPTH_TEST); 
     731        glDepthMask(GL_FALSE); 
     732        glDisable(GL_CULL_FACE); 
     733 
     734        Polyhedron *hpoly = CreatePolyhedron(projView, mSceneBox); 
     735        //Polyhedron *hpoly = CalcClippedFrustum(mSceneBox); 
     736         
     737        DrawPoly(hpoly, Vector3(1, 1, 1)); 
     738        DEL_PTR(hpoly); 
     739 
     740        glEnable(GL_CULL_FACE); 
     741 
     742        glEnable(GL_DEPTH_TEST); 
     743        glDepthMask(GL_TRUE); 
     744        glPopAttrib(); 
     745 
     746        glMatrixMode(GL_MODELVIEW); 
     747        glPopMatrix(); 
     748 
     749        glMatrixMode(GL_PROJECTION); 
     750        glPopMatrix(); 
     751 
     752 
     753#if 0 
     754        float *data = new float[mSize * mSize]; 
     755 
     756        GrabDepthBuffer(data, mFbo->GetDepthTex()); 
     757        ExportDepthBuffer(data, mSize); 
     758 
     759        delete [] data; 
     760         
     761        PrintGLerror("shadow map"); 
     762#endif 
     763        FrameBufferObject::Release(); 
     764} 
     765 
     766 
     767 
     768 
    663769} // namespace 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2929 r2931  
    5656        Camera *GetShadowCamera() const { return mShadowCam; } 
    5757 
     58        void RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView); 
     59 
    5860 
    5961        static void DrawPolys(); 
     
    6163 
    6264protected: 
     65 
    6366 
    6467        /** Calculates the intersection of the frustum with the box, 
     
    106109 
    107110        Matrix4x4 mLightProjView; 
     111 
     112        Matrix4x4 mLightMatrix; 
    108113}; 
    109114 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp

    r2929 r2931  
    174174 
    175175        glDepthMask(GL_TRUE); 
    176  
    177176        glPopAttrib(); 
    178177} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2930 r2931  
    100100static int winHeight = 768; 
    101101 
     102const float shadowSize = 4096; 
    102103 
    103104static float winAspectRatio = 1.0f; 
     
    179180 
    180181static Matrix4x4 matProjectionView = IdentityMatrix(); 
     182 
     183bool renderLightView = false; 
     184 
    181185 
    182186 
     
    873877         
    874878 
    875         // render without shading 
     879 
     880        if ((!shadowMap || !shadowTraverser) && (showShadowMap || renderLightView)) 
     881        { 
     882                if (!shadowMap) 
     883                        shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera); 
     884 
     885                if (!shadowTraverser) 
     886                        shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera()); 
     887 
     888        } 
     889 
     890        // hack 
     891        int oldRenderType = renderType; 
     892 
     893        if (renderLightView) 
     894                renderType = RenderState::FIXED; 
     895 
     896 
     897        // render with the specified method (forward rendering, forward + depth, deferred) 
    876898        switch (renderType) 
    877899        { 
     
    909931        case RenderState::DEFERRED: 
    910932 
    911                 if (showShadowMap)// && shadowChanged) 
     933                if (showShadowMap && !renderLightView)// && shadowChanged) 
    912934                { 
    913                         if (!shadowMap) 
    914                         { 
    915                                 const float shadowSize = 4096; 
    916                                 //const float shadowSize = 512; 
    917                                 shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera); 
    918                         } 
    919  
    920                         // change CHC state (must be done for each change of camera) 
    921                         BvhNode::SetCurrentState(1); 
    922  
    923                         if (!shadowTraverser) 
    924                                 shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera()); 
    925  
    926935                        shadowChanged = false; 
    927  
    928 #if 0 
    929                         state.SetRenderType(RenderState::DEFERRED); 
    930  
    931                         fbo->Bind(); 
    932  
    933                         glPushAttrib(GL_VIEWPORT_BIT); 
    934                         glViewport(0, 0, texWidth, texHeight); 
    935  
    936                         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    937                         cgGLBindProgram(RenderState::sCgMrtFragmentProgram); 
    938  
    939                         cgGLEnableProfile(RenderState::sCgVertexProfile); 
    940                         cgGLBindProgram(sCgMrtVertexProgram); 
    941 #else 
    942936 
    943937                        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     
    945939 
    946940                        state.SetRenderType(RenderState::DEPTH_PASS); 
    947 #endif 
     941                         
     942                        // change CHC++ set of state variables (must be done for each change of camera because 
     943                        // otherwise the temporal coherency is broken 
     944                        BvhNode::SetCurrentState(1); 
     945 
    948946                        // the scene is rendered withouth any shading    
    949947                        shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView); 
     
    10021000        glLightfv(GL_LIGHT1, GL_POSITION, position1); 
    10031001 
    1004         // actually render the scene geometry using the specified algorithm 
    1005         traverser->RenderScene(); 
     1002 
     1003        if (renderLightView) 
     1004        { 
     1005                // change CHC++ set of state variables (must be done for each change of camera because 
     1006                // otherwise the temporal coherency is broken 
     1007                BvhNode::SetCurrentState(1); 
     1008                shadowMap->RenderShadowView(shadowTraverser, matProjectionView); 
     1009                BvhNode::SetCurrentState(0); 
     1010        } 
     1011        else 
     1012                // actually render the scene geometry using the specified algorithm 
     1013                traverser->RenderScene(); 
    10061014 
    10071015 
     
    10681076        glDisableClientState(GL_VERTEX_ARRAY); 
    10691077        glDisableClientState(GL_NORMAL_ARRAY); 
    1070  
     1078         
     1079        renderType = oldRenderType; 
    10711080 
    10721081 
     
    11141123                traverser = CreateTraverser(camera); 
    11151124 
    1116                 if (shadowTraverser && shadowMap)  
     1125                if (shadowTraverser) 
    11171126                { 
     1127                        // shadow traverser has to be recomputed (this is done on demand) 
    11181128                        DEL_PTR(shadowTraverser); 
    1119                         //shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera()); 
    11201129                } 
    11211130 
     
    12021211        case 'a': 
    12031212        case 'A': 
    1204                         leftKeyPressed = true; 
     1213                leftKeyPressed = true; 
    12051214                break; 
    12061215        case 'd': 
    12071216        case 'D': 
    1208                         rightKeyPressed = true; 
     1217                rightKeyPressed = true; 
    12091218                break; 
    12101219        case 'w': 
    12111220        case 'W': 
    1212                         upKeyPressed = true; 
     1221                upKeyPressed = true; 
    12131222                break; 
    12141223        case 's': 
    12151224        case 'S': 
    1216                         downKeyPressed = true; 
     1225                downKeyPressed = true; 
    12171226                break; 
    12181227        case 'r': 
    12191228        case 'R': 
    1220                 { 
    1221                         useRenderQueue = !useRenderQueue; 
    1222                         traverser->SetUseRenderQueue(useRenderQueue); 
    1223                 } 
     1229                useRenderQueue = !useRenderQueue; 
     1230                traverser->SetUseRenderQueue(useRenderQueue); 
     1231                 
    12241232                break; 
    12251233        case 'b': 
    12261234        case 'B': 
    1227                 { 
    1228                         useTightBounds = !useTightBounds; 
    1229                         traverser->SetUseTightBounds(useTightBounds); 
    1230                 } 
     1235                useTightBounds = !useTightBounds; 
     1236                traverser->SetUseTightBounds(useTightBounds); 
     1237                break; 
     1238        case 'l': 
     1239        case 'L': 
     1240                renderLightView = !renderLightView; 
    12311241                break; 
    12321242        default: 
Note: See TracChangeset for help on using the changeset viewer.