Changeset 2895


Ignore:
Timestamp:
09/02/08 15:29:20 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
7 edited
1 moved

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj

    r2887 r2895  
    216216                        </File> 
    217217                        <File 
    218                                 RelativePath=".\src\DeferredShader.cpp" 
    219                                 > 
    220                         </File> 
    221                         <File 
    222                                 RelativePath=".\src\DeferredShader.h" 
    223                                 > 
    224                         </File> 
    225                         <File 
    226218                                RelativePath=".\src\Environment.cpp" 
    227219                                > 
     
    321313                        <File 
    322314                                RelativePath=".\src\ShadowMapping.h" 
    323                                 > 
    324                         </File> 
    325                         <File 
    326                                 RelativePath=".\src\SsaoShader.cpp" 
    327                                 > 
    328                         </File> 
    329                         <File 
    330                                 RelativePath=".\src\SsaoShader.h" 
    331315                                > 
    332316                        </File> 
     
    544528                                <File 
    545529                                        RelativePath=".\src\Camera.h" 
     530                                        > 
     531                                </File> 
     532                                <File 
     533                                        RelativePath=".\src\DeferredRenderer.cpp" 
    546534                                        > 
    547535                                </File> 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp

    r2894 r2895  
    2424using namespace std; 
    2525 
     26 
     27int BvhNode::sCurrentState = 0; 
    2628 
    2729 
     
    7577mAxis(-1),  
    7678mDepth(0),  
    77 mPlaneMask(0), 
    78 mPreferredPlane(0), 
    7979mLastRenderedFrame(-999), 
    8080mFirst(-1),  
     
    8787mIsVirtualLeaf(false) 
    8888{ 
     89        for (int i = 0; i < NUM_STATES; ++ i) 
     90        { 
     91                mPlaneMask[i] = 0; 
     92                mPreferredPlane[i]= 0; 
     93        } 
    8994} 
    9095 
     
    97102void BvhNode::ResetVisibility() 
    98103{ 
    99         mVisibility.Reset(); 
     104        for (int i = 0; i < NUM_STATES; ++ i) 
     105        { 
     106                mVisibility[i].Reset(); 
     107        } 
     108 
    100109        mLastRenderedFrame = -999; 
    101110} 
     
    301310{ 
    302311        // do the test only if necessary 
    303         if (!(node->mPlaneMask & (1 << i))) 
     312        if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i))) 
    304313                return true; 
    305314                         
     
    310319        { 
    311320                // outside 
    312                 node->mPreferredPlane = i; 
     321                node->mPreferredPlane[BvhNode::sCurrentState] = i; 
    313322                return false; 
    314323        } 
     
    320329        { 
    321330                // completely inside: children don't need to check against this plane no more 
    322                 node->mPlaneMask^= 1 << i; 
     331                node->mPlaneMask[BvhNode::sCurrentState] ^= 1 << i; 
    323332        } 
    324333        else  
     
    336345 
    337346        if (node->GetParent()) 
    338                 node->mPlaneMask = node->GetParent()->mPlaneMask; 
    339  
    340  
    341         //for (int i = 0; i < 6; ++ i) 
    342         //      if (!TestPlane(node, i, bIntersect)) return 0; 
    343          
     347                node->mPlaneMask[BvhNode::sCurrentState] = node->GetParent()->mPlaneMask[BvhNode::sCurrentState]; 
    344348 
    345349        //////// 
    346350        //-- apply frustum culling for the planes [mPreferredPlane - 5] 
    347351 
    348         for (int i = node->mPreferredPlane; i < 6; ++ i) 
     352        for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i) 
    349353                if (!TestPlane(node, i, bIntersect)) return 0; 
    350354         
     355 
    351356        ////////// 
    352357        //-- do the view frustum culling for the planes [0 - m_iPreferredPlane) 
    353358 
    354         for (int i = 0; i < node->mPreferredPlane; ++ i) 
     359        for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i) 
    355360                if (!TestPlane(node, i, bIntersect)) return 0; 
    356361         
     
    362367{ 
    363368        // = 0011 1111 which means that at the beginning, all six planes have to frustum culled 
    364         mRoot->mPlaneMask = 0x3f; 
     369        mRoot->mPlaneMask[BvhNode::sCurrentState] = 0x3f; 
    365370 
    366371        mCamera->CalcFrustum(sFrustum); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h

    r2894 r2895  
    1212namespace CHCDemoEngine 
    1313{ 
     14 
     15// the number of visibility states 
     16#define NUM_STATES 2 
    1417 
    1518//////// 
     
    149152        inline int CountPrimitives() const; 
    150153 
     154        static void SetCurrentState(int _state) { sCurrentState = _state; } 
     155 
    151156 
    152157protected: 
    153158 
    154         ///////////// 
    155          
    156159        /// the depth of this node 
    157160        unsigned char mDepth; 
     
    160163        /// the parent node 
    161164        BvhNode *mParent; 
    162         /// stores the visibility related info 
    163         VisibilityInfo mVisibility; 
     165 
     166 
     167        ////////////// 
     168        //-- these members define the current state 
     169 
     170        /// stores the visibility related info  
     171        VisibilityInfo mVisibility[NUM_STATES]; 
    164172 
    165173        ///////// 
    166174        //-- used for view frustum culling 
    167175 
    168         int mPlaneMask; 
    169         int mPreferredPlane; 
     176        int mPlaneMask[NUM_STATES]; 
     177        int mPreferredPlane[NUM_STATES]; 
     178 
     179        // the current state 
     180        static int sCurrentState; 
     181 
     182 
     183        //////////////////// 
    170184 
    171185 
     
    221235int BvhNode::GetLastVisitedFrame() const  
    222236{  
    223         return mVisibility.mLastVisitedFrame;  
     237        return mVisibility[sCurrentState].mLastVisitedFrame;  
    224238} 
    225239 
     
    227241void BvhNode::SetLastVisitedFrame(const int lastVisited)  
    228242{  
    229         mVisibility.mLastVisitedFrame = lastVisited;  
     243        mVisibility[sCurrentState].mLastVisitedFrame = lastVisited;  
    230244} 
    231245 
     
    233247bool BvhNode::IsVisible() const  
    234248{  
    235         return mVisibility.mIsVisible;  
     249        return mVisibility[sCurrentState].mIsVisible;  
    236250} 
    237251 
     
    239253void BvhNode::SetVisible(bool visible)  
    240254{  
    241         mVisibility.mIsVisible = visible;  
     255        mVisibility[sCurrentState].mIsVisible = visible;  
    242256} 
    243257 
     
    245259void BvhNode::IncTimesTestedInvisible()  
    246260{  
    247         ++ mVisibility.mTimesInvisible;  
     261        ++ mVisibility[sCurrentState].mTimesInvisible;  
    248262} 
    249263 
     
    251265int BvhNode::GetTimesTestedInvisible() const  
    252266{  
    253         return mVisibility.mTimesInvisible;  
     267        return mVisibility[sCurrentState].mTimesInvisible;  
    254268} 
    255269 
     
    257271void BvhNode::SetTimesTestedInvisible(int t)  
    258272{  
    259         mVisibility.mTimesInvisible = t;  
     273        mVisibility[sCurrentState].mTimesInvisible = t;  
    260274} 
    261275 
     
    263277bool BvhNode::IsViewFrustumCulled() const  
    264278{  
    265         return mVisibility.mIsFrustumCulled;  
     279        return mVisibility[sCurrentState].mIsFrustumCulled;  
    266280} 
    267281 
     
    269283void BvhNode::SetViewFrustumCulled(bool frustumCulled)  
    270284{  
    271         mVisibility.mIsFrustumCulled = frustumCulled;  
     285        mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled;  
    272286} 
    273287 
     
    275289bool BvhNode::IsNew() const  
    276290{  
    277         return mVisibility.mIsNew; 
     291        return mVisibility[sCurrentState].mIsNew; 
    278292} 
    279293 
     
    281295void BvhNode::SetIsNew(bool isNew)  
    282296{  
    283         mVisibility.mIsNew = isNew;  
    284 } 
    285  
     297        mVisibility[sCurrentState].mIsNew = isNew;  
     298} 
     299 
     300 
     301void BvhNode::SetAssumedVisibleFrameId(int t)  
     302{  
     303        mVisibility[sCurrentState].mAssumedVisibleFrameId = t;  
     304} 
     305 
     306 
     307int BvhNode::GetAssumedVisibleFrameId() const  
     308{  
     309        return mVisibility[sCurrentState].mAssumedVisibleFrameId; 
     310} 
     311 
     312 
     313int BvhNode::GetLastQueriedFrame() const 
     314{ 
     315        return mVisibility[sCurrentState].mLastQueriedFrame; 
     316} 
     317 
     318 
     319void BvhNode::SetLastQueriedFrame(int lastTested) 
     320{ 
     321        mVisibility[sCurrentState].mLastQueriedFrame = lastTested; 
     322} 
    286323 
    287324int BvhNode::GetLastRenderedFrame() const  
     
    309346 
    310347 
    311 void BvhNode::SetAssumedVisibleFrameId(int t)  
    312 {  
    313         mVisibility.mAssumedVisibleFrameId = t;  
    314 } 
    315  
    316  
    317 int BvhNode::GetAssumedVisibleFrameId() const  
    318 {  
    319         return mVisibility.mAssumedVisibleFrameId; 
    320 } 
    321  
    322  
    323 int BvhNode::GetLastQueriedFrame() const 
    324 { 
    325         return mVisibility.mLastQueriedFrame; 
    326 } 
    327  
    328  
    329 void BvhNode::SetLastQueriedFrame(int lastTested) 
    330 { 
    331         mVisibility.mLastQueriedFrame = lastTested; 
    332 } 
    333348 
    334349 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r2894 r2895  
    88#include "RndGauss.h" 
    99#include "Halton.h" 
     10#include "ShadowMapping.h" 
    1011 
    1112 
     
    2324static CGprogram sCgDeferredProgram = NULL; 
    2425static CGprogram sCgAntiAliasingProgram = NULL; 
     26static CGprogram sCgDeferredShadowProgram = NULL; 
    2527 
    2628static CGparameter sColorsTexCombineParam; 
     
    8385static CGparameter sNormalsTexAntiAliasingParam; 
    8486 
     87 
     88static CGparameter sShadowMapParam; 
     89static CGparameter sPositionsTexShadowParam;   
     90static CGparameter sColorsTexShadowParam;   
     91static CGparameter sNormalsTexShadowParam; 
     92 
     93static CGparameter sShadowMatrixParam; 
     94static CGparameter sMaxDepthShadowParam; 
     95static CGparameter sSampleWidthParam; 
     96 
     97 
     98 
    8599static GLuint noiseTex = 0; 
    86100 
     
    88102static Sample2 samples[NUM_SAMPLES]; 
    89103 
     104static int colorBufferIdx = 0; 
    90105 
    91106static void PrintGLerror(char *msg) 
     
    192207mScaleFactor(scaleFactor), 
    193208mUseTemporalCoherence(true), 
    194 mUseGlobIllum(false), 
    195 mSampling(POISSON), 
     209mRegenerateSamples(true), 
     210mSamplingMethod(POISSON), 
     211mShadingMethod(DEFAULT), 
    196212mFboIndex(0) 
    197213{ 
     
    237253 
    238254        glDeleteTextures(1, &noiseTex); 
    239 } 
    240  
    241  
    242 void SsaoShader::SetUseGlobIllum(bool useGlobIllum) 
    243 { 
    244         mUseGlobIllum = useGlobIllum; 
    245255} 
    246256 
     
    303313                 
    304314                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 
    305  
    306                 // generate samples for ssao kernel 
    307                 //GenerateSamples(mSampling);  
    308  
    309315                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 
    310                 //cgSetArraySize(sSamplesParam, NUM_SAMPLES); 
    311                 //cgCompileProgram(sCgSsaoProgram); 
    312                  
    313                 //cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 
    314316        } 
    315317        else 
     
    344346                sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");   
    345347 
    346                 // generate samples for ssao kernel 
    347                 //GenerateSamples();  
    348                 //cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 
    349  
    350348                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 
    351349        } 
     
    410408                cerr << "antialiasing program failed to load" << endl; 
    411409 
     410        sCgDeferredShadowProgram =  
     411                cgCreateProgramFromFile(context,  
     412                                                                CG_SOURCE, 
     413                                                                "src/shaders/deferred.cg",  
     414                                                                RenderState::sCgFragmentProfile, 
     415                                                                "main_shadow", 
     416                                                                NULL); 
     417 
     418        if (sCgDeferredShadowProgram != NULL) 
     419        { 
     420                cgGLLoadProgram(sCgDeferredShadowProgram); 
     421 
     422                // we need size of texture for scaling 
     423                sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");   
     424                sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");   
     425                sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 
     426 
     427                sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");   
     428                sMaxDepthShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth"); 
     429                sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth"); 
     430                sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix"); 
     431        } 
     432        else 
     433                cerr << "deferred program failed to load" << endl; 
    412434        PrintGLerror("init"); 
    413435} 
     
    416438void SsaoShader::Render(FrameBufferObject *fbo,  
    417439                                                const Matrix4x4 &oldProjViewMatrix, 
    418                                                 float expFactor) 
     440                                                float expFactor, 
     441                                                ShadowMap *shadowMap) 
    419442{ 
    420443         
     
    447470        glOrtho(-offs, offs, -offs, offs, 0, 1); 
    448471 
    449         FirstPass(fbo); 
    450          
    451         if (!mUseGlobIllum) 
    452         { 
     472        if (shadowMap) 
     473                FirstPassShadow(fbo, shadowMap); 
     474        else 
     475                FirstPass(fbo); 
     476         
     477        switch (mShadingMethod) 
     478        { 
     479        case SSAO: 
    453480                ComputeSsao(fbo, expFactor, oldProjViewMatrix); 
    454481                CombineSsao(fbo); 
    455         } 
    456         else 
    457         { 
     482                break; 
     483        case GI: 
    458484                ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix); 
    459485                CombineIllum(fbo); 
     486                break; 
     487        default: // DEFAULT 
     488                // do nothing: standard deferred shading 
     489                break; 
    460490        } 
    461491 
     
    528558         
    529559 
    530         if (mUseTemporalCoherence) 
    531         { 
     560        if (mUseTemporalCoherence || mRegenerateSamples) 
     561        { 
     562                mRegenerateSamples = false; 
    532563                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 
    533                 cgGLSetParameter1f(sExpFactorParam, expFactor); 
    534564 
    535565                // q: should we generate new samples or only rotate the old ones? 
    536566                // in the first case, the sample patterns look nicer, but the kernel 
    537567                // needs longer to converge 
    538                 GenerateSamples(mSampling);  
     568                GenerateSamples(mSamplingMethod);  
    539569                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 
    540570        } 
    541         else 
    542         { 
    543                         cgGLSetParameter1f(sExpFactorParam, 1.0f); 
    544         } 
     571 
     572        cgGLSetParameter1f(sExpFactorParam, mUseTemporalCoherence ? expFactor : 1.0f); 
    545573 
    546574        Vector3 tl, tr, bl, br; 
     
    604632void SsaoShader::AntiAliasing(FrameBufferObject *fbo) 
    605633{ 
    606         GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     634        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 
    607635        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    608636         
     
    650678        fbo->Bind(); 
    651679 
    652         glDrawBuffers(1, mymrt + 3); 
     680        colorBufferIdx = 3; 
     681        glDrawBuffers(1, mymrt + colorBufferIdx); 
    653682 
    654683        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     
    754783 
    755784 
    756         if (mUseTemporalCoherence) 
    757         { 
     785        if (mUseTemporalCoherence || mRegenerateSamples) 
     786        { 
     787                mRegenerateSamples = false; 
    758788                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 
    759789                cgGLSetParameter1f(sExpFactorGiParam, expFactor); 
     
    762792                // in the first case, the sample patterns look nicer, but the kernel 
    763793                // needs longer to converge 
    764                 GenerateSamples(mSampling);  
     794                GenerateSamples(mSamplingMethod);  
    765795                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 
    766796        } 
    767         else 
    768         { 
    769                         cgGLSetParameter1f(sExpFactorGiParam, 1.0f); 
    770         } 
     797 
     798        cgGLSetParameter1f(sExpFactorParam, mUseTemporalCoherence ? expFactor : 1.0f); 
    771799 
    772800        Vector3 tl, tr, bl, br; 
     
    812840 
    813841        // overwrite old color texture 
    814         glDrawBuffers(1, mymrt); 
     842        colorBufferIdx = 0; 
     843        glDrawBuffers(1, mymrt + colorBufferIdx); 
    815844 
    816845        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     
    863892        fbo->Bind(); 
    864893 
    865         // write into old color texture (not needed anymore) 
    866         glDrawBuffers(1, mymrt); 
     894        // overwrite old color texture 
     895        colorBufferIdx = 0; 
     896        glDrawBuffers(1, mymrt + colorBufferIdx); 
    867897 
    868898        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     
    903933 
    904934 
     935void SsaoShader::FirstPassShadow(FrameBufferObject *fbo, ShadowMap *shadowMap) 
     936{ 
     937        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     938 
     939        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     940        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     941        GLuint shadowTex = shadowMap->GetShadowTexture(); 
     942 
     943        Matrix4x4 shadowMatrix; 
     944        shadowMap->GetTextureMatrix(shadowMatrix); 
     945 
     946        fbo->Bind(); 
     947 
     948        colorBufferIdx = 3; 
     949        glDrawBuffers(1, mymrt + colorBufferIdx); 
     950 
     951         
     952        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     953 
     954        cgGLBindProgram(sCgDeferredShadowProgram); 
     955 
     956        cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex); 
     957        cgGLEnableTextureParameter(sColorsTexShadowParam); 
     958 
     959        cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex); 
     960        cgGLEnableTextureParameter(sPositionsTexShadowParam); 
     961 
     962        cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex); 
     963        cgGLEnableTextureParameter(sNormalsTexShadowParam); 
     964         
     965        cgGLSetTextureParameter(sShadowMapParam, shadowTex); 
     966        cgGLEnableTextureParameter(sShadowMapParam); 
     967 
     968        cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor); 
     969        cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize()); 
     970          
     971        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 
     972 
     973        glColor3f(1.0f, 1.0f, 1.0f); 
     974         
     975        glBegin(GL_QUADS); 
     976 
     977        float offs2 = 0.5f; 
     978 
     979        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f); 
     980        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f); 
     981        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f); 
     982        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f); 
     983 
     984        glEnd(); 
     985 
     986        cgGLDisableTextureParameter(sColorsTexShadowParam); 
     987        cgGLDisableTextureParameter(sPositionsTexShadowParam); 
     988        cgGLDisableTextureParameter(sNormalsTexShadowParam); 
     989        cgGLDisableTextureParameter(sShadowMapParam); 
     990 
     991        FrameBufferObject::Release(); 
     992 
     993        PrintGLerror("deferred shading + shadows"); 
     994} 
     995 
     996 
     997void SsaoShader::SetSamplingMethod(SAMPLING_METHOD s)  
     998{  
     999        if (s != mSamplingMethod) 
     1000        { 
     1001                mSamplingMethod = s;  
     1002                mRegenerateSamples = true; 
     1003        } 
     1004} 
    9051005 
    9061006} // namespace 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp

    r2894 r2895  
    4747static CGparameter sShadowMatrixParam; 
    4848static CGparameter sMaxDepthParam; 
    49  
     49static CGparameter sSampleWidthParam; 
    5050 
    5151 
     
    122122                sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");   
    123123                sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");   
    124                 sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");  
     124                sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 
     125 
    125126                sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");   
    126127                sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth"); 
     128                sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth"); 
    127129                sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix"); 
    128130        } 
     
    156158        glOrtho(-offs, offs, -offs, offs, 0, 1); 
    157159 
    158  
    159160        FirstPass(fbo); 
    160161        AntiAliasing(fbo); 
    161162 
    162163        glEnable(GL_LIGHTING); 
    163         glDisable(GL_TEXTURE_2D); 
    164          
     164                 
    165165        glMatrixMode(GL_PROJECTION); 
    166166        glPopMatrix(); 
     
    172172 
    173173        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     174        FrameBufferObject::Release(); 
    174175} 
    175176 
     
    178179                                                        ShadowMap *shadowMap) 
    179180{ 
     181        FrameBufferObject::Release(); 
     182 
    180183        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    181184 
     
    183186        glDisable(GL_TEXTURE_2D); 
    184187        glDisable(GL_LIGHTING); 
    185         glDepthMask(GL_FALSE); 
     188        //glDepthMask(GL_FALSE); 
    186189         
    187190 
     
    204207 
    205208        glEnable(GL_LIGHTING); 
    206         glEnable(GL_TEXTURE_2D); 
    207         glDepthMask(GL_TRUE); 
     209        //glEnable(GL_TEXTURE_2D); 
     210        //glDepthMask(GL_TRUE); 
    208211         
    209212        glMatrixMode(GL_PROJECTION); 
     
    216219 
    217220        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     221        FrameBufferObject::Release(); 
    218222} 
    219223 
     
    258262        cgGLDisableTextureParameter(sPositionsTexParam); 
    259263        cgGLDisableTextureParameter(sNormalsTexParam); 
    260  
    261         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    262264         
    263265        FrameBufferObject::Release(); 
     
    290292        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    291293 
    292         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    293  
    294294        cgGLBindProgram(sCgAntiAliasingProgram); 
    295295 
     
    301301         
    302302        glColor3f(1.0f, 1.0f, 1.0f); 
    303  
    304         float offs2 = 0.5f; 
    305  
     303         
    306304        glBegin(GL_QUADS); 
    307305 
     
    320318        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam); 
    321319 
    322         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    323  
    324320        PrintGLerror("antialiasing"); 
    325321} 
     
    343339        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    344340 
    345         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    346  
    347341        cgGLBindProgram(sCgDeferredShadowProgram); 
    348342 
     
    360354 
    361355        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor); 
    362  
     356        cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize()); 
     357          
    363358        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 
    364359 
     
    381376        cgGLDisableTextureParameter(sShadowMapParam); 
    382377 
    383         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    384          
    385378        FrameBufferObject::Release(); 
    386379 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2894 r2895  
    4646        */ 
    4747        void GetTextureMatrix(Matrix4x4 &m) const; 
    48  
     48        /** Return shadow size. 
     49        */ 
     50        int GetSize() const { return mSize; } 
    4951 
    5052protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2894 r2895  
    3434#include "FrameBufferObject.h" 
    3535#include "SsaoShader.h" 
    36 #include "DeferredShader.h" 
    3736#include "ShadowMapping.h" 
    3837#include "Light.h" 
     
    9594static int winHeight = 768; 
    9695 
    97 //static int winWidth = 512; 
    98 //static int winHeight = 512; 
    99  
    10096 
    10197static float winAspectRatio = 1.0f; 
     
    172168bool useLODs = true; 
    173169 
    174 int samplingMethod = SsaoShader::POISSON; 
     170SsaoShader::SAMPLING_METHOD samplingMethod = SsaoShader::POISSON; 
     171SsaoShader::SHADING_METHOD shadingMethod = SsaoShader::DEFAULT; 
     172 
     173bool showShadowMap = false; 
     174bool shadowChanged = true; 
    175175 
    176176static Matrix4x4 matProjectionView = IdentityMatrix(); 
     
    179179ShadowMap *shadowMap = NULL; 
    180180Light *light = NULL; 
     181 
    181182 
    182183// function forward declarations 
     
    225226 
    226227SsaoShader *ssaoShader = NULL; 
    227 DeferredShader *deferredShader = NULL; 
    228228 
    229229static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 
     
    431431        InitCg(); 
    432432 
    433         DeferredShader::Init(sCgContext); 
    434433        SsaoShader::Init(sCgContext); 
    435434 
     
    442441        sceneQuery = new SceneQuery(bvh->GetBox(), traverser); 
    443442 
    444         Vector3 lightDir = Normalize(Vector3(0, 1, -1)); 
     443        //Vector3 lightDir = Normalize(Vector3(0, 1, -1)); 
     444        Vector3 lightDir = Normalize(-Vector3(0.8f, -1.0f, 0.7f)); 
     445         
    445446        light = new Light(lightDir, RgbaColor(1, 1, 1, 1)); 
    446447 
    447         const float shadowSize = 4096; 
    448         shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera); 
    449          
     448 
    450449        // frame time is restarted every frame 
    451450        frameTimer.Start(); 
     
    867866        } 
    868867         
     868        if (showShadowMap && !shadowMap) 
     869        { 
     870                const float shadowSize = 4096; 
     871                shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera); 
     872        } 
     873 
    869874        // render without shading 
    870875        switch (renderType) 
     
    897902         
    898903                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    899  
    900904                glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    901905 
     
    904908        case RenderState::DEFERRED: 
    905909 
    906                 shadowMap->ComputeShadowMap(light, traverser); 
     910                // change CHC state (must be done for each change of camera) 
     911                BvhNode::SetCurrentState(1); 
     912 
     913                if (showShadowMap && shadowChanged) 
     914                { 
     915                        shadowChanged = false; 
     916 
     917                        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     918                        cgGLDisableProfile(RenderState::sCgVertexProfile); 
     919 
     920                        state.SetRenderType(RenderState::DEPTH_PASS); 
     921 
     922                        // the scene is rendered withouth any shading    
     923                        shadowMap->ComputeShadowMap(light, traverser); 
     924 
     925                        // change back state 
     926                        BvhNode::SetCurrentState(0); 
     927 
     928                        glEnable(GL_LIGHTING); 
     929                        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     930                } 
    907931 
    908932                if (!fbo) InitFBO(); 
     933 
    909934 
    910935                // multisampling does not work with deferred shading 
     
    9891014                cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    9901015 
    991                 if (useSsao) 
    992                 { 
    993                         if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 
    994                         ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor); 
    995  
    996                 } 
    997                 else 
    998                 { 
    999                         if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight, myfar / 10.0f); 
    1000                          
    1001                         deferredShader->Render(fbo, shadowMap); 
    1002                 } 
     1016                if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 
     1017                 
     1018                ssaoShader->SetShadingMethod(shadingMethod); 
     1019                ssaoShader->SetSamplingMethod(samplingMethod); 
     1020                ssaoShader->SetUseTemporalCoherence(useTemporalCoherence); 
     1021 
     1022                ShadowMap *sm = showShadowMap ? shadowMap : NULL; 
     1023                ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor, sm); 
    10031024        } 
    10041025 
     
    11181139                        samplingMethod = SsaoShader::GAUSS; 
    11191140 
    1120                 ssaoShader->SetSamplingMethod(samplingMethod); 
    1121  
     1141                break; 
     1142        case 'Y': 
     1143        case 'y': 
     1144                showShadowMap = !showShadowMap; 
    11221145                break; 
    11231146        case 'g': 
    1124         case 'G': 
    1125                 useGlobIllum = !useGlobIllum; 
    1126                 ssaoShader->SetUseGlobIllum(useGlobIllum); 
    1127                 break; 
    11281147        case 't': 
    11291148        case 'T': 
    11301149                useTemporalCoherence = !useTemporalCoherence; 
    1131                 ssaoShader->SetUseTemporalCoherence(useTemporalCoherence); 
    11321150                break; 
    11331151        case 'o': 
     
    12621280                break; 
    12631281        case GLUT_KEY_F8: 
    1264                 useSsao = !useSsao; 
     1282                shadingMethod = (SsaoShader::SHADING_METHOD)((shadingMethod + 1) % 3); 
     1283                 
    12651284                break; 
    12661285        case GLUT_KEY_F9: 
     
    15621581        DEL_PTR(fbo); 
    15631582        DEL_PTR(ssaoShader); 
    1564         DEL_PTR(deferredShader); 
    15651583 
    15661584        if (sCgMrtVertexProgram) 
     
    17661784} 
    17671785 
    1768  
     1786// render visible object from depth pass 
    17691787void RenderVisibleObjects() 
    17701788{ 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2893 r2895  
    8181                                  uniform sampler2D shadowMap, 
    8282                                  uniform float4x4 shadowMatrix, 
    83                                   uniform float maxDepth 
     83                                  uniform float maxDepth, 
     84                                  uniform float sampleWidth 
    8485                                  ) 
    8586{ 
     
    102103        float4 lightSpacePos = mul(shadowMatrix, position); 
    103104 
    104         float shadowDepth = tex2D(shadowMap, lightSpacePos.xy); 
     105        float shadowDepth[9]; 
     106 
     107        float w = sampleWidth; 
     108 
     109        // pcf sampling using 3 x 3 tab 
     110 
     111        shadowDepth[0] = tex2D(shadowMap, lightSpacePos.xy).x; 
     112         
     113        shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w,  w)).x; 
     114        shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy - float2( w, -w)).x; 
     115        shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, -w)).x; 
     116        shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy - float2(-w,  w)).x; 
     117 
     118        shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy - float2( w,  0)).x; 
     119        shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy - float2( 0,  w)).x; 
     120        shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy - float2(-w,  0)).x; 
     121        shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, -w)).x; 
    105122 
    106123        OUT.color = col; 
    107124         
    108         // hack: prevent shadowing the sky 
    109         if ((amb < 0.9) &&       
     125        float depth = lightSpacePos.z / lightSpacePos.w; 
     126 
     127        float d = 0.0f; 
     128 
     129        for (int i = 0; i < 9; ++ i) 
     130        { 
     131                d += step(shadowDepth[i], depth); 
     132        } 
     133 
     134        d /= 9.0f; 
     135         
     136        /*if ((amb < 0.9) && // hack: prevent shadowing the sky  
    110137                (lightSpacePos.z / lightSpacePos.w > shadowDepth)) 
    111138        { 
    112139                OUT.color *= 0.1f; 
     140        }*/ 
     141         
     142        if (amb < 0.9) // hack: prevent shadowing the sky        
     143        { 
     144                const float x = 0.1f; 
     145                OUT.color *= x + (1.0f - x) * (1.0f - d); 
    113146        } 
    114147         
Note: See TracChangeset for help on using the changeset viewer.