Changeset 2932


Ignore:
Timestamp:
09/12/08 03:09:20 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
8 edited

Legend:

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

    r2931 r2932  
    1111using namespace std; 
    1212 
    13 // our coordinate system has the positive z axis pointing up 
     13// our coordinate system is left-handed 
     14// we look into positive y and have the positive z axis pointing up 
    1415static const Vector3 baseDir = -Vector3::UNIT_Y(); 
    1516 
     
    8889        mat = mViewOrientation; 
    8990 
    90         Vector3 pos = mViewOrientation * -mPosition; 
    91          
    92         mat.x[3][0] = pos.x; 
    93         mat.x[3][1] = pos.y; 
    94         mat.x[3][2] = pos.z; 
    95  
    96         //glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x); 
     91        // note: left handed system => we go into positive z 
     92        mat.x[3][0] = -DotProd(GetRightVector(), mPosition); 
     93        mat.x[3][1] = -DotProd(GetUpVector(), mPosition); 
     94        mat.x[3][2] = DotProd(GetDirection(), mPosition); 
    9795} 
    9896 
     
    127125void Camera::SetupCameraView() 
    128126{ 
    129         Matrix4x4 viewOrientation = mViewOrientation; 
    130  
    131         Vector3 pos = mViewOrientation * -mPosition; 
    132  
    133         viewOrientation.x[3][0] = pos.x; 
    134         viewOrientation.x[3][1] = pos.y; 
    135         viewOrientation.x[3][2] = pos.z; 
    136  
    137         glLoadMatrixf((float *)viewOrientation.x); 
     127        Matrix4x4 m; 
     128        GetModelViewMatrix(m); 
     129 
     130        glLoadMatrixf((float *)m.x); 
    138131} 
    139132 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h

    r2931 r2932  
    4040class Camera 
    4141{ 
     42        friend class ShadowMap; 
    4243public: 
    4344         
     
    4647        Camera(int width, int height, float fieldOfView = 90.f); 
    4748 
    48  
     49        /** Sets the current camera position. 
     50        */ 
    4951        void SetPosition(const Vector3 &pos); 
    5052 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r2930 r2932  
    293293                                                                CG_SOURCE, 
    294294#ifdef USE_3D_SSAO 
    295                                                                 "src/shaders/ssao3d.cg",  
    296  
     295                                                                "src/shaders/ssao3d.cg", 
    297296#else 
    298297                                                                "src/shaders/ssao.cg",  
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp

    r2924 r2932  
    731731} 
    732732 
    733 } 
     733 
     734Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up)  
     735{ 
     736        const Vector3 nDir = Normalize(dir); 
     737        Vector3 nUp = Normalize(up); 
     738 
     739        Vector3 nRight = Normalize(CrossProd(nDir, nUp)); 
     740        nUp = Normalize(CrossProd(nRight, nDir)); 
     741 
     742        Matrix4x4 m(nRight, nUp, -nDir); 
     743 
     744        /*m.x[3][0] = -DotProd(nRight, position); 
     745        m.x[3][1] = -DotProd(nUp, position); 
     746        m.x[3][2] = -DotProd(nDir, position);*/ 
     747 
     748        Vector3 position = m * -pos; 
     749         
     750        // note: left handet system => we go into positive z 
     751        m.x[3][0] = position.x; 
     752        m.x[3][1] = position.y; 
     753        m.x[3][2] = position.z; 
     754 
     755        return m; 
     756} 
     757 
     758} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h

    r2919 r2932  
    160160Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); 
    161161Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); 
     162Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); 
    162163 
    163164 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp

    r2931 r2932  
    2323static Polyhedron *polyhedron = NULL; 
    2424static Polyhedron *lightPoly = NULL; 
     25static Matrix4x4 dummy = IdentityMatrix(); 
    2526 
    2627 
     
    183184        DrawPoly(lightPoly, Vector3(1, 0, 1)); 
    184185        DrawPoly(polyhedron, Vector3(0, 1, 0)); 
     186 
     187        Vector3 pt = Vector3::ZERO(); 
     188 
     189        pt = dummy * pt; 
     190 
     191        glPointSize(10.0f); 
     192 
     193        glBegin(GL_POINTS); 
     194        glVertex3f(pt.x, pt.y, pt.z); 
     195        glEnd(); 
    185196} 
    186197 
     
    212223        //-- first find the free parameter values n, and P (the projection center), and the projection depth 
    213224 
    214         //const float n = 1e2f; 
    215         const float n = 1e6f; 
     225        const float n = 1e2f; 
     226        //const float n = 1e6f; 
    216227        //const float n = ComputeN(bounds_ls) * 100; 
    217228 
     
    357368        cout << "dummy: " << Normalize(dummyVec2 - dummyVec) << endl; 
    358369        //project the view direction into the shadow map plane 
    359         projDir.y = 0.0; 
     370        projDir.y = .0f; 
    360371 
    361372        return Normalize(projDir); 
     
    401412 
    402413        //switch to the lightspace used in the article 
    403         lightProj = lightProj * transform2LispSM; 
     414        //lightProj = lightProj * transform2LispSM; 
    404415 
    405416        const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints); 
     
    412423        //calculate a frame matrix that uses the projViewDir[lightspace] as up vector 
    413424        //look(from position, into the direction of the projected direction, with unchanged up-vector) 
    414         const Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y()); 
     425        const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y()); 
    415426 
    416427        cout << "frame\n " << frame << endl; 
    417         lightProj = lightProj * frame; 
     428        //lightProj = lightProj * frame; 
    418429 
    419430        cout << "here9\n" << lightProj << endl; 
     
    421432        const Matrix4x4 matLispSM =  
    422433                CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints); 
    423  
    424         Vector3 mydummy = projViewDir;//Vector3::UNIT_Z(); 
    425         //cout << "transformed unit z vector: " << Normalize(lightView * lightProj * mydummy) << endl; 
    426         cout << "transformed unit z vector: " << Normalize(frame * mydummy) << endl; 
    427434 
    428435        lightProj = lightProj * matLispSM; 
     
    436443        transformToGL.x[3][3] =  1.0f; 
    437444 
    438         lightProj = lightProj * transformToGL; 
     445        //lightProj = lightProj * transformToGL; 
    439446        //cout << "here4 \n" << lightProj << endl; 
    440447 
     
    485492        return Normalize(up); 
    486493} 
    487  
    488  
    489 void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView) 
    490 { 
    491         const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f); 
    492         const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f); 
    493          
    494         //const Vector3 dir = mLight->GetDirection(); 
    495         const Vector3 dir(0, 0, -1); 
    496  
    497         Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 
    498         mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec); 
    499  
    500         mShadowCam->SetDirection(dir); 
    501          
    502         //cout << "lightdir: " << mShadowCam->GetDirection() << endl; 
    503  
    504         // set position so that we can see the whole scene 
    505         Vector3 pos = mSceneBox.Center(); 
    506  
    507         //Matrix4x4 camView; 
    508         //mCamera->GetModelViewMatrix(camView); 
    509  
    510         pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f); 
    511         mShadowCam->SetPosition(pos); 
    512  
    513         mFbo->Bind(); 
    514          
    515         glDrawBuffers(1, mrt); 
    516  
    517         glPushAttrib(GL_VIEWPORT_BIT); 
    518         glViewport(0, 0, mSize, mSize); 
    519  
    520         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    521  
    522         //glEnable(GL_LIGHTING); 
    523         //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    524         glDisable(GL_LIGHTING); 
    525         glDisable(GL_TEXTURE_2D); 
    526         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    527  
    528         glPolygonOffset(1.0f, 2000.0f); 
    529         glEnable(GL_POLYGON_OFFSET_FILL); 
    530  
    531         glShadeModel(GL_FLAT); 
    532         glEnable(GL_DEPTH_TEST); 
    533  
    534         // setup projection 
    535         /*glMatrixMode(GL_PROJECTION); 
    536         glLoadIdentity(); 
    537         glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal()));  
    538  
    539         Matrix4x4 dummyMat; 
    540         glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat.x); 
    541         cout << "old:\n" << dummyMat << endl; 
    542         */ 
    543  
    544         Matrix4x4 lightView, lightProj; 
    545  
    546         //mShadowCam->GetModelViewMatrix(lightView); 
    547         lightView = mLightMatrix; 
    548  
    549         CalcLightProjection(lightProj); 
    550  
    551         glMatrixMode(GL_PROJECTION); 
    552         glPushMatrix(); 
    553         glLoadMatrixf((float *)lightProj.x); 
    554  
    555         mLightProjView = lightView * lightProj; 
    556  
    557         DEL_PTR(lightPoly); 
    558         lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 
    559  
    560         //cout << "new:\n" << lightProj << endl; 
    561  
    562         glMatrixMode(GL_MODELVIEW); 
    563         glPushMatrix(); 
    564         glLoadIdentity(); 
    565  
    566         //mShadowCam->SetupCameraView(); 
    567         glLoadMatrixf((float *)mLightMatrix.x); 
    568  
    569         ////////////// 
    570         //-- compute texture matrix 
    571  
    572         static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f, 
    573                                                                 0.0f, 0.5f, 0.0f, 0.5f, 
    574                                                                 0.0f, 0.0f, 0.5f, 0.5f, 
    575                                                                 0.0f, 0.0f, 0.0f, 1.0f); 
    576  
    577         mTextureMatrix = mLightProjView * biasMatrix;  
    578  
    579  
    580  
    581  
    582         ///////////// 
    583         //-- render scene into shadow map 
    584  
    585         renderer->RenderScene(); 
    586  
    587          
    588         glDisable(GL_POLYGON_OFFSET_FILL); 
    589         glMatrixMode(GL_MODELVIEW); 
    590         glPopMatrix(); 
    591  
    592         glMatrixMode(GL_PROJECTION); 
    593         glPopMatrix(); 
    594  
    595         glPopAttrib(); 
    596  
    597          
    598         glEnable(GL_LIGHTING); 
    599         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    600  
    601 #if 0 
    602         float *data = new float[mSize * mSize]; 
    603  
    604         GrabDepthBuffer(data, mFbo->GetDepthTex()); 
    605         ExportDepthBuffer(data, mSize); 
    606  
    607         delete [] data; 
    608          
    609         PrintGLerror("shadow map"); 
    610 #endif 
    611         FrameBufferObject::Release(); 
    612 } 
    613  
    614494 
    615495 
     
    667547 
    668548 
    669 void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView) 
    670 { 
     549void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView) 
     550{ 
     551        const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f); 
     552        const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f); 
     553         
    671554        //const Vector3 dir = mLight->GetDirection(); 
    672555        const Vector3 dir(0, 0, -1); 
    673556 
    674         mShadowCam->SetDirection(dir); 
     557        // set position so that we can see the whole scene 
     558        //Vector3 pos = mSceneBox.Center(); 
     559        //pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f); 
     560        //mShadowCam->SetPosition(pos); 
     561        mShadowCam->SetPosition(mCamera->GetPosition()); 
    675562 
    676563        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); 
     564        Matrix4x4 lightView =  
     565                MyLookAt(mShadowCam->GetPosition(), dir, upVec); 
     566 
     567        mShadowCam->mViewOrientation = lightView; 
     568 
     569        //cout << "here45:\n" << lightView << endl; 
     570 
     571        //mShadowCam->SetDirection(dir); 
     572        //mShadowCam->GetModelViewMatrix(lightView); 
     573        //cout << "here46:\n" << lightView << endl; 
     574 
     575        mFbo->Bind(); 
     576         
     577        glDrawBuffers(1, mrt); 
     578 
     579        glPushAttrib(GL_VIEWPORT_BIT); 
     580        glViewport(0, 0, mSize, mSize); 
    684581 
    685582        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    686583 
    687         glEnable(GL_LIGHTING); 
    688         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     584        glDisable(GL_LIGHTING); 
     585        glDisable(GL_TEXTURE_2D); 
     586        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    689587 
    690588        glPolygonOffset(1.0f, 2000.0f); 
    691589        glEnable(GL_POLYGON_OFFSET_FILL); 
    692 glDisable(GL_CULL_FACE); 
     590 
     591        glShadeModel(GL_FLAT); 
    693592        glEnable(GL_DEPTH_TEST); 
    694593 
    695         Matrix4x4 lightView, lightProj; 
    696          
    697         //mShadowCam->GetModelViewMatrix(lightView); 
    698         lightView = mLightMatrix; 
     594        Matrix4x4 lightProj; 
     595 
    699596        CalcLightProjection(lightProj); 
    700597 
     
    705602        mLightProjView = lightView * lightProj; 
    706603 
     604        cout << "here3" << endl; 
    707605        DEL_PTR(lightPoly); 
    708606        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 
    709  
    710         //cout << "new:\n" << lightProj << endl; 
     607        cout << "here4\n" << lightView << endl; 
    711608 
    712609        glMatrixMode(GL_MODELVIEW); 
     
    714611        glLoadIdentity(); 
    715612 
    716         //mShadowCam->SetupCameraView(); 
    717         glLoadMatrixf((float *)mLightMatrix.x); 
    718  
     613        mShadowCam->SetupCameraView(); 
     614 
     615 
     616        ////////////// 
     617        //-- compute texture matrix 
     618 
     619        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f, 
     620                                                                0.0f, 0.5f, 0.0f, 0.5f, 
     621                                                                0.0f, 0.0f, 0.5f, 0.5f, 
     622                                                                0.0f, 0.0f, 0.0f, 1.0f); 
     623 
     624        mTextureMatrix = mLightProjView * biasMatrix;  
     625 
     626 
     627 
     628 
     629        ///////////// 
     630        //-- render scene into shadow map 
     631 
     632        renderer->RenderScene(); 
     633 
     634         
     635        glDisable(GL_POLYGON_OFFSET_FILL); 
     636        glMatrixMode(GL_MODELVIEW); 
     637        glPopMatrix(); 
     638 
     639        glMatrixMode(GL_PROJECTION); 
     640        glPopMatrix(); 
     641 
     642        glPopAttrib(); 
     643 
     644         
     645        glEnable(GL_LIGHTING); 
     646        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     647 
     648#if 0 
     649        float *data = new float[mSize * mSize]; 
     650 
     651        GrabDepthBuffer(data, mFbo->GetDepthTex()); 
     652        ExportDepthBuffer(data, mSize); 
     653 
     654        delete [] data; 
     655         
     656        PrintGLerror("shadow map"); 
     657#endif 
     658        FrameBufferObject::Release(); 
     659} 
     660 
     661 
     662void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView) 
     663{ 
     664        //const Vector3 dir = mLight->GetDirection(); 
     665        const Vector3 dir(0, 0, -1); 
     666 
     667        mShadowCam->SetDirection(dir); 
     668 
     669        // set position so that we can see the whole scene 
     670        Vector3 pos = mSceneBox.Center(); 
     671        pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f); 
     672 
     673        mShadowCam->SetPosition(mCamera->GetPosition()); 
     674 
     675        Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 
     676        Matrix4x4 lightView = MyLookAt(mShadowCam->GetPosition(), dir, upVec); 
     677 
     678        mShadowCam->mViewOrientation = lightView; 
     679 
     680        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     681 
     682        glEnable(GL_LIGHTING); 
     683        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     684 
     685        glPolygonOffset(1.0f, 2000.0f); 
     686        glEnable(GL_POLYGON_OFFSET_FILL); 
     687         
     688        glEnable(GL_DEPTH_TEST); 
     689 
     690        Matrix4x4 lightProj; 
     691         
     692        CalcLightProjection(lightProj); 
     693 
     694        glMatrixMode(GL_PROJECTION); 
     695        glPushMatrix(); 
     696        glLoadMatrixf((float *)lightProj.x); 
     697 
     698        mLightProjView = lightView * lightProj; 
     699 
     700        dummy = mLightProjView; 
     701 
     702        DEL_PTR(lightPoly); 
     703        lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 
     704 
     705        glMatrixMode(GL_MODELVIEW); 
     706        glPushMatrix(); 
     707        glLoadIdentity(); 
     708 
     709        mShadowCam->SetupCameraView(); 
     710         
    719711 
    720712        ///////////// 
     
    736728         
    737729        DrawPoly(hpoly, Vector3(1, 1, 1)); 
     730 
    738731        DEL_PTR(hpoly); 
    739732 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2931 r2932  
    109109 
    110110        Matrix4x4 mLightProjView; 
    111  
    112         Matrix4x4 mLightMatrix; 
    113111}; 
    114112 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2931 r2932  
    15691569 
    15701570        //const float offs = box.Size().x * 0.3f; 
    1571         const float offs = box.Size().x * 0.4f; 
     1571        const float offs = box.Size().x * 0.6f; 
    15721572         
    15731573        Vector3 vizpos = Vector3(box.Min().x, box.Min().y  - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50); 
Note: See TracChangeset for help on using the changeset viewer.