Changeset 3085 for GTP


Ignore:
Timestamp:
10/31/08 17:35:42 (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/DeferredRenderer.cpp

    r3084 r3085  
    7171 
    7272 
     73/** Helper method that computes the view vectors in the corners of the current view frustum. 
     74*/ 
     75static void ComputeViewVectors(PerspectiveCamera *cam, Vector3 &bl, Vector3 &br, Vector3 &tl, Vector3 &tr) 
     76{ 
     77        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; 
     78 
     79        cam->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
     80 
     81        bl = Normalize(nbl - fbl); 
     82        br = Normalize(nbr - fbr); 
     83        tl = Normalize(ntl - ftl); 
     84        tr = Normalize(ntr - ftr); 
     85} 
     86 
    7387 
    7488static float GaussianDistribution(float x, float y, float rho) 
     
    127141void DeferredRenderer::DrawQuad(ShaderProgram *p) 
    128142{ 
    129         Vector3 tl, tr, bl, br; 
    130         ComputeViewVectors(tl, tr, bl, br); 
    131  
    132143        p->Bind(); 
     144 
     145        Vector3 bl = mCornersView[0]; 
     146        Vector3 br = mCornersView[1]; 
     147        Vector3 tl = mCornersView[2]; 
     148        Vector3 tr = mCornersView[3]; 
    133149 
    134150        // note: slightly larger texture could hide ambient occlusion error on border but costs resolution 
     
    258274        CreateNoiseTex2D(mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight()); 
    259275 
     276        mProjViewMatrix = IdentityMatrix(); 
     277        mOldProjViewMatrix = IdentityMatrix(); 
     278 
     279        for (int i = 0; i < 4; ++ i) 
     280        { 
     281                mCornersView[i] = mOldCornersView[i] = Vector3::UNIT_X(); 
     282        } 
     283 
     284        mEyePos = mOldEyePos = Vector3::ZERO(); 
     285 
    260286        InitCg(); 
    261287} 
     
    308334        sCgSsaoProgram->AddParameter("oldModelViewProj", i ++); 
    309335 
     336        sCgSsaoProgram->AddParameter("oldEyePos", i ++); 
     337        sCgSsaoProgram->AddParameter("oldbl", i ++); 
     338        sCgSsaoProgram->AddParameter("oldbr", i ++); 
     339        sCgSsaoProgram->AddParameter("oldtl", i ++); 
     340        sCgSsaoProgram->AddParameter("oldtr", i ++); 
     341         
    310342        i = 0; 
    311343        sCgGiProgram->AddParameter("colors", i ++); 
     
    379411 
    380412void DeferredRenderer::Render(FrameBufferObject *fbo,  
    381                                                           const Matrix4x4 &oldProjViewMatrix, 
    382                                                           const Matrix4x4 &projViewMatrix, 
    383413                                                          float tempCohFactor, 
    384414                                                          DirectionalLight *light, 
     
    387417                                                          ) 
    388418{ 
     419        InitFrame(); 
     420 
    389421        // switch roles of old and new fbo 
    390422        // the algorihm uses two input fbos, where the one 
     
    429461                DownSample(fbo, 1, mDownSampleFbo, 1); 
    430462 
    431                 ComputeSsao(fbo, tempCohFactor, projViewMatrix, oldProjViewMatrix); 
     463                ComputeSsao(fbo, tempCohFactor); 
    432464                CombineSsao(fbo); 
    433465                break; 
     
    437469                DownSample(fbo, 1, mDownSampleFbo, 1); 
    438470 
    439                 ComputeGlobIllum(fbo, tempCohFactor, projViewMatrix, oldProjViewMatrix); 
     471                ComputeGlobIllum(fbo, tempCohFactor); 
    440472                CombineIllum(fbo); 
    441473                break; 
     
    472504 
    473505void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo,  
    474                                                                    float tempCohFactor, 
    475                                                                    const Matrix4x4 &projViewMatrix, 
    476                                                                    const Matrix4x4 &oldProjViewMatrix 
     506                                                                   float tempCohFactor 
     507                                                                    
    477508                                                                   ) 
    478509{ 
     
    499530        sCgSsaoProgram->SetTexture(3, noiseTex); 
    500531 
    501         const Vector3 pos = mCamera->GetPosition(); 
    502         sCgSsaoProgram->SetValue3f(4, pos.x, pos.y, pos.z); 
     532        sCgSsaoProgram->SetValue3f(4, mEyePos.x, mEyePos.y, mEyePos.z); 
    503533 
    504534        sCgSsaoProgram->SetValue1f(5, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0); 
     
    514544                sCgSsaoProgram->SetArray2f(6, (float *)samples2, NUM_SAMPLES); 
    515545        } 
    516  
    517         Vector3 tl, tr, bl, br; 
    518         ComputeViewVectors(tl, tr, bl, br); 
     546         
     547        Vector3 bl = mCornersView[0]; 
     548        Vector3 br = mCornersView[1]; 
     549        Vector3 tl = mCornersView[2]; 
     550        Vector3 tr = mCornersView[3]; 
    519551 
    520552        sCgSsaoProgram->SetValue3f(7, bl.x, bl.y, bl.z); 
     
    526558        //cout << "old projview:\n" << oldProjViewMatrix << endl; 
    527559 
    528         sCgSsaoProgram->SetMatrix(11, projViewMatrix); 
    529         sCgSsaoProgram->SetMatrix(12, oldProjViewMatrix); 
     560        sCgSsaoProgram->SetMatrix(11, mProjViewMatrix); 
     561        sCgSsaoProgram->SetMatrix(12, mOldProjViewMatrix); 
     562 
     563        bl = mOldCornersView[0]; 
     564        br = mOldCornersView[1]; 
     565        tl = mOldCornersView[2]; 
     566        tr = mOldCornersView[3]; 
     567 
     568        sCgSsaoProgram->SetValue3f(13, mOldEyePos.x, mOldEyePos.y, mOldEyePos.z); 
     569        sCgSsaoProgram->SetValue3f(14, bl.x, bl.y, bl.z); 
     570        sCgSsaoProgram->SetValue3f(15, br.x, br.y, br.z); 
     571        sCgSsaoProgram->SetValue3f(16, tl.x, tl.y, tl.z); 
     572        sCgSsaoProgram->SetValue3f(17, tr.x, tr.y, tr.z); 
    530573 
    531574 
     
    535578 
    536579        PrintGLerror("ssao first pass"); 
    537 } 
    538  
    539  
    540 void DeferredRenderer::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br) 
    541 { 
    542         Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; 
    543  
    544         mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
    545  
    546         bl = Normalize(nbl - fbl); 
    547         br = Normalize(nbr - fbr); 
    548         tl = Normalize(ntl - ftl); 
    549         tr = Normalize(ntr - ftr); 
    550580} 
    551581 
     
    622652 
    623653void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,  
    624                                                                                 float tempCohFactor, 
    625                                                                                 const Matrix4x4 &projViewMatrix, 
    626                                                                                 const Matrix4x4 &oldProjViewMatrix) 
     654                                                                                float tempCohFactor) 
    627655{ 
    628656#if 0 
     
    652680        sCgGiProgram->SetTexture(4, oldIllumTex); 
    653681 
    654         const Vector3 pos = mCamera->GetPosition(); 
    655         sCgGiProgram->SetValue3f(5, pos.x, pos.y, pos.z); 
     682        sCgGiProgram->SetValue3f(5, mEyePos.x, mEyePos.y, mEyePos.z); 
    656683 
    657684 
     
    671698        } 
    672699 
    673  
    674         Vector3 tl, tr, bl, br; 
    675         ComputeViewVectors(tl, tr, bl, br); 
    676          
     700        Vector3 bl = mCornersView[0]; 
     701        Vector3 br = mCornersView[1]; 
     702        Vector3 tl = mCornersView[2]; 
     703        Vector3 tr = mCornersView[3]; 
     704 
    677705        sCgGiProgram->SetValue3f(8, bl.x, bl.y, bl.z); 
    678706        sCgGiProgram->SetValue3f(9, br.x, br.y, br.z); 
     
    680708        sCgGiProgram->SetValue3f(11, tr.x, tr.y, tr.z); 
    681709 
    682         sCgGiProgram->SetMatrix(12, oldProjViewMatrix); 
    683         sCgGiProgram->SetMatrix(13, projViewMatrix); 
     710        sCgGiProgram->SetMatrix(12, mOldProjViewMatrix); 
     711        sCgGiProgram->SetMatrix(13, mProjViewMatrix); 
    684712 
    685713 
     
    792820        sCgDeferredShadowProgram->SetValue3f(6, lightDir.x, lightDir.y, lightDir.z); 
    793821 
    794         const Vector3 pos = mCamera->GetPosition(); 
    795         sCgDeferredShadowProgram->SetValue3f(7, pos.x, pos.y, pos.z); 
     822        sCgDeferredShadowProgram->SetValue3f(7, mEyePos.x, mEyePos.y, mEyePos.z); 
    796823 
    797824        DrawQuad(sCgDeferredShadowProgram); 
     
    972999 
    9731000 
     1001void DeferredRenderer::InitFrame() 
     1002{ 
     1003        mOldProjViewMatrix = mProjViewMatrix; 
     1004 
     1005        Matrix4x4 matViewing, matProjection; 
     1006 
     1007        mCamera->GetModelViewMatrix(matViewing); 
     1008        mCamera->GetProjectionMatrix(matProjection); 
     1009 
     1010        mProjViewMatrix = matViewing * matProjection; 
     1011 
     1012        for (int i = 0; i < 4; ++ i) 
     1013        { 
     1014                mOldCornersView[i] = mCornersView[i]; 
     1015        } 
     1016 
     1017        ComputeViewVectors(mCamera, mCornersView[0], mCornersView[1], mCornersView[2], mCornersView[3]); 
     1018 
     1019        mOldEyePos = mEyePos; 
     1020        mEyePos = mCamera->GetPosition(); 
     1021} 
     1022 
    9741023 
    9751024} // namespace 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h

    r3068 r3085  
    33 
    44#include "common.h" 
    5 #include "glInterface.h" 
    6 #include "ShaderProgram.h" 
    7  
    8 #include <Cg/cg.h> 
    9 #include <Cg/cgGL.h> 
    10  
     5#include "Matrix4x4.h" 
     6#include "Vector3.h" 
    117 
    128namespace CHCDemoEngine  
     
    4238 
    4339        void Render(FrameBufferObject *fbo,  
    44                         const Matrix4x4 &oldProjViewMatrix,  
    45                                 const Matrix4x4 &projViewMatrix,  
    4640                                float tempCohFactor,  
    4741                                DirectionalLight *light, 
     
    6963protected: 
    7064 
    71         void ComputeSsao(FrameBufferObject *fbo,  
    72                                          float tempCohFactor,  
    73                                          const Matrix4x4 &projViewMatrix, 
    74                                          const Matrix4x4 &oldProjViewMatrix 
    75                                          ); 
     65        void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor); 
    7666 
    77         void ComputeGlobIllum(FrameBufferObject *fbo, 
    78                                   float tempCohFactor, 
    79                                                   const Matrix4x4 &projViewMatrix, 
    80                                                   const Matrix4x4 &oldProjViewMatrix); 
     67        void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor); 
    8168 
    8269        void FirstPass(FrameBufferObject *fbo, DirectionalLight *light); 
     
    9784 
    9885        void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light); 
    99         /** Helper method that computes the view vectors in the corners of the current view frustum. 
    100         */ 
    101         void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 
    102  
    10386        /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the 
    10487                resolution of fbo. 
     
    11396        */ 
    11497        void InitCg(); 
     98        /** Is called once per render call and sets the most important parameters. 
     99        */ 
     100        void InitFrame(); 
    115101 
    116102 
     
    139125 
    140126        static ShaderContainer sShaders; 
     127 
     128        Matrix4x4 mProjViewMatrix; 
     129        Matrix4x4 mOldProjViewMatrix; 
     130 
     131        Vector3 mCornersView[4]; 
     132        Vector3 mOldCornersView[4]; 
     133 
     134        Vector3 mEyePos; 
     135        Vector3 mOldEyePos; 
    141136}; 
    142137 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp

    r3071 r3085  
    77#include "Camera.h" 
    88 
    9 #include "glInterface.h" 
    10 #include <Cg/cg.h> 
    11 #include <Cg/cgGL.h> 
    129 
    1310 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h

    r3071 r3085  
    22#define __SCENEENTITY_H 
    33 
    4 #include "glInterface.h" 
    5 #include <Cg/cg.h> 
    6 #include <Cg/cgGL.h> 
    74#include "common.h" 
    85#include "AxisAlignedBox3.h" 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h

    r3038 r3085  
    22#define _SKYPREETHAM_H__ 
    33 
    4 #include "glInterface.h" 
    54#include "common.h" 
    65#include "ShaderProgram.h" 
    76 
    8 #include <Cg/cg.h> 
    9 #include <Cg/cgGL.h> 
    107 
    118 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3080 r3085  
    10651065 
    10661066                ShadowMap *sm = showShadowMap ? shadowMap : NULL; 
    1067                 ssaoShader->Render(fbo, oldViewProjMat, viewProjMat, ssaoTempCohFactor, light, useHDR, sm); 
     1067                ssaoShader->Render(fbo, ssaoTempCohFactor, light, useHDR, sm); 
    10681068        } 
    10691069 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h

    r3084 r3085  
    4242#define MAX_LOD_LEVEL 10 
    4343 
    44 #define MIN_DEPTH_DIFF 1e-6f 
    45 #define PRECISION_SCALE 1e-3f 
     44#define MIN_DEPTH_DIFF 1e-3f 
     45#define PRECISION_SCALE 1e-1f 
    4646 
    4747// burnout 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3084 r3085  
    5050 
    5151// reconstruct world space position 
    52 inline float3 ReconstructSamplePos(uniform sampler2D colors, 
     52inline float3 ReconstructSamplePos(uniform sampler2D tex, 
    5353                                                                   float2 texcoord,  
    5454                                                                   float3 bl, float3 br, float3 tl, float3 tr) 
    5555{ 
    56         const float eyeSpaceDepth = tex2D(colors, texcoord).w; 
     56        const float eyeSpaceDepth = tex2D(tex, texcoord).w; 
    5757        float3 viewVec = Interpol(texcoord, bl, br, tl, tr); 
    5858        float3 samplePos = -viewVec * eyeSpaceDepth; 
     
    8181                                                 uniform float3 tr,  
    8282                                                 float2 texcoord0, 
    83                                                  float3 eyePos 
     83                                                 float3 eyePos, 
     84                                                 float eyeSpaceDepth, 
     85                                                 float3 oldEyePos, 
     86                                                 uniform float3 oldbl, 
     87                                                 uniform float3 oldbr, 
     88                                                 uniform float3 oldtl, 
     89                                                 uniform float3 oldtr 
    8490                                                 ) 
    8591{ 
     
    9096 
    9197        // reproject into old frame and calculate projected depth 
    92         const float3 dummyPos = ReconstructSamplePos(colors, texcoord0, bl, br, tl, tr) + eyePos; 
    93         float4 backProjPos = mul(oldModelViewProj, float4(dummyPos, 1.0f)); 
    94         //float4 backProjPos = mul(oldModelViewProj, worldPos); 
    95          
     98        float4 backProjPos = mul(oldModelViewProj, worldPos); 
    9699        backProjPos /= backProjPos.w; 
    97          
    98         // the current depth projected into the old frame 
    99         const float backProjDepth = backProjPos.z * PRECISION_SCALE; 
    100100        // fit from unit cube into 0 .. 1 
    101         const float2 tex = backProjPos.xy * 0.5f + 0.5f; 
     101        const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f; 
     102 
    102103        // retrieve the sample from the last frame 
    103         float4 oldCol = tex2D(oldTex, tex); 
    104  
    105         const float oldDepth = oldCol.z; 
    106         const float depthDif = backProjDepth - oldDepth; 
     104        float4 oldCol = tex2D(oldTex, oldTexCoords); 
     105        const float oldEyeSpaceDepth = oldCol.z; 
     106 
     107        float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr); 
     108        //float3 oldSamplePos = -viewVec * eyeSpaceDepth + oldEyePos; 
     109        float3 oldSamplePos = oldEyePos - viewVec * oldCol.z; 
     110 
     111        //const float oldDepth = oldCol.z; 
     112        const float depthDif = length(oldSamplePos - worldPos.xyz); 
    107113 
    108114        //const float oldNumSamples = oldCol.y; 
     
    110116        float newWeight; 
    111117 
    112         bool isValid = true; 
     118        /*bool isValid = true; 
    113119 
    114120        for (int i = 0; i < NUM_SAMPLES; ++ i)  
     
    134140 
    135141                if (abs(dDiff) > 1e-5f) isValid = false; 
    136         } 
     142        }*/ 
    137143 
    138144        // the number of valid samples in this frame 
     
    140146 
    141147        if ((temporalCoherence > 1e-6f) 
    142                 && (tex.x >= 0.0f) && (tex.x < 1.0f) 
    143                 && (tex.y >= 0.0f) && (tex.y < 1.0f) 
     148                && (oldTexCoords.x >= 0.0f) && (oldTexCoords.x < 1.0f) 
     149                && (oldTexCoords.y >= 0.0f) && (oldTexCoords.y < 1.0f) 
    144150                && (abs(depthDif) <= MIN_DEPTH_DIFF)  
    145151                // if visibility changed in the surrounding area we have to recompute 
    146152                //&& (oldNumSamples > 0.8f * newNumSamples) 
    147                 && isValid 
     153        //      && isValid 
    148154                ) 
    149155        { 
     
    162168        //illum_col.y = isValid / 16.0f; 
    163169        illum_col.y = newWeight; 
    164         illum_col.z = currentDepth; 
     170        illum_col.z = eyeSpaceDepth; 
    165171 
    166172        return illum_col; 
     
    252258                   uniform float2 samples[NUM_SAMPLES], 
    253259                   uniform sampler2D oldTex, 
    254                    const uniform float4x4 modelViewProj, 
    255                    const uniform float4x4 oldModelViewProj, 
     260                   uniform float4x4 modelViewProj, 
     261                   uniform float4x4 oldModelViewProj, 
    256262                   uniform float temporalCoherence, 
    257263                   uniform float3 eyePos, 
     
    259265                   uniform float3 br, 
    260266                   uniform float3 tl, 
    261                    uniform float3 tr 
     267                   uniform float3 tr, 
     268                   uniform float3 oldEyePos, 
     269                   uniform float3 oldbl, 
     270                   uniform float3 oldbr, 
     271                   uniform float3 oldtl, 
     272                   uniform float3 oldtr 
    262273                   ) 
    263274{ 
    264275        pixel OUT; 
    265276 
    266         const float3 normal =  
    267                 normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz); 
     277        const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz); 
    268278 
    269279        // reconstruct position from the eye space depth 
     
    292302         
    293303        OUT.illum_col = temporalSmoothing(projPos, worldPos, currentDepth, oldTex, oldModelViewProj, temporalCoherence, ao, 
    294                                               samples, colors, noiseTex, w, bl, br, tl, tr, IN.texCoord, eyePos); 
     304                                              samples, colors, noiseTex, w, bl, br, tl, tr, IN.texCoord, eyePos, eyeDepth, 
     305                                                                          oldEyePos,oldbl, oldbr, oldtl, oldtr); 
    295306 
    296307        return OUT; 
Note: See TracChangeset for help on using the changeset viewer.