Ignore:
Timestamp:
08/27/08 15:09:04 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2871 r2873  
    1515 
    1616// number of ssao samples 
    17 #define NUM_SAMPLES 16 
     17#define NUM_SAMPLES 10 
    1818 
    1919 
    2020static CGprogram sCgSsaoProgram = NULL; 
    21 static CGprogram sCgDeferredProgram2 = NULL; 
     21static CGprogram sCgGiProgram = NULL; 
     22 
     23static CGprogram sCgDeferredProgram = NULL; 
    2224static CGprogram sCgAntiAliasingProgram = NULL; 
    2325static CGprogram sCgCombineProgram = NULL; 
     
    3032static CGparameter sNormalsTexDeferredParam; 
    3133 
     34 
     35///////////////////////////////////////7 
     36 
     37 
    3238static CGparameter sColorsTexParam; 
    3339static CGparameter sPositionsTexParam; 
    3440static CGparameter sNormalsTexParam; 
    35  
    3641 
    3742static CGparameter sOldModelViewProjMatrixParam; 
     
    4348static CGparameter sExpFactorParam; 
    4449 
     50 
     51/////////////////////////////////////// 
     52 
     53 
     54static CGparameter sColorsTexGiParam; 
     55static CGparameter sPositionsTexGiParam; 
     56static CGparameter sNormalsTexGiParam; 
     57 
     58 
     59static CGparameter sOldModelViewProjMatrixGiParam; 
     60static CGparameter sMaxDepthGiParam; 
     61static CGparameter sSamplesGiParam;  
     62static CGparameter sOldSsaoTexGiParam; 
     63static CGparameter sOldIllumTexGiParam; 
     64static CGparameter sNoiseTexGiParam; 
     65static CGparameter sNoiseMultiplierGiParam; 
     66static CGparameter sExpFactorGiParam; 
     67 
     68 
     69//////////// 
     70 
    4571static CGparameter sColorsTexAntiAliasingParam; 
    4672static CGparameter sNormalsTexAntiAliasingParam; 
     
    88114        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    89115        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     116        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    90117         
    91118        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     
    93120        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    94121        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     122        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    95123         
    96124        mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     
    98126        mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    99127                 
    100         mFbo4 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
    101         // the diffuse color buffer 
    102         mFbo4->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    103128 
    104129        // create noise texture for ssao 
     
    121146void SsaoShader::Init(CGcontext context) 
    122147{        
    123         sCgDeferredProgram2 =  
     148        sCgDeferredProgram =  
    124149                cgCreateProgramFromFile(context,  
    125150                                                                CG_SOURCE, 
    126151                                                                "src/shaders/deferred.cg",  
    127152                                                                RenderState::sCgFragmentProfile, 
    128                                                                 "main2", 
     153                                                                "main", 
    129154                                                                NULL); 
    130155 
    131         if (sCgDeferredProgram2 != NULL) 
    132         { 
    133                 cgGLLoadProgram(sCgDeferredProgram2); 
     156        if (sCgDeferredProgram != NULL) 
     157        { 
     158                cgGLLoadProgram(sCgDeferredProgram); 
    134159 
    135160                // we need size of texture for scaling 
    136                 sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "positions");   
    137                 sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "colors");   
    138                 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "normals");  
     161                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");   
     162                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");   
     163                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");  
    139164        } 
    140165        else 
    141166                cerr << "deferred program failed to load" << endl; 
    142167 
    143  
    144         /*sCgCombineProgram =  
    145                 cgCreateProgramFromFile(context,  
    146                                                                 CG_SOURCE, 
    147                                                                 "src/shaders/ssao.cg",  
    148                                                                 RenderState::sCgFragmentProfile, 
    149                                                                 "combined", 
    150                                                                 NULL); 
    151  
    152         if (sCgCombineProgram != NULL) 
    153         { 
    154                 cgGLLoadProgram(sCgCombineProgram); 
    155  
    156                 // we need size of texture for scaling 
    157                 sColorsTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "colors");   
    158                 sSsaoTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "ssaoTex");  
    159         } 
    160         else 
    161                 cerr << "combined program failed to load" << endl; 
    162 */ 
    163168 
    164169        /////////////// 
     
    189194                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 
    190195                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");   
     196                 
    191197 
    192198                // generate samples for ssao kernel 
     
    196202        else 
    197203                cerr << "ssao program failed to load" << endl; 
     204 
     205        sCgGiProgram =  
     206                cgCreateProgramFromFile(context,  
     207                                                                CG_SOURCE, 
     208                                                                "src/shaders/globillum.cg",  
     209                                                                RenderState::sCgFragmentProfile, 
     210                                                                "main", 
     211                                                                NULL); 
     212 
     213        if (sCgGiProgram != NULL) 
     214        { 
     215                cgGLLoadProgram(sCgGiProgram); 
     216 
     217                // we need size of texture for scaling 
     218                sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");   
     219                sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");   
     220                sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");   
     221                sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture"); 
     222                sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier"); 
     223                 
     224                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj"); 
     225                sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth"); 
     226                sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor"); 
     227 
     228                sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples"); 
     229                 
     230                sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");   
     231                sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");   
     232 
     233                // generate samples for ssao kernel 
     234                GenerateSamples();  
     235                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 
     236        } 
     237        else 
     238                cerr << "globillum program failed to load" << endl; 
    198239 
    199240        sCgAntiAliasingProgram =  
     
    258299 
    259300        FirstPass(fbo); 
     301         
    260302        ComputeSsao(fbo, expFactor); 
     303        //ComputeGlobIllum(fbo, expFactor); 
     304 
    261305        //Combine(fbo); 
    262306        AntiAliasing(fbo); 
     
    323367 
    324368 
    325         //GenerateSamples();  
     369        // q: should we generate new samples or only rotate the old ones? 
     370        // in the first case, the sample patterns look nicer, but the kernel 
     371        // needs longer to converge 
     372        GenerateSamples();  
    326373        cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 
    327374 
     
    499546        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    500547 
    501         cgGLBindProgram(sCgDeferredProgram2); 
     548        cgGLBindProgram(sCgDeferredProgram); 
    502549 
    503550        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex); 
     
    535582 
    536583 
    537 void SsaoShader::Combine(FrameBufferObject *fbo) 
     584void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo, float expFactor) 
    538585{ 
    539586        GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 
    540         GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture(); 
    541  
    542         mFbo4->Bind(); 
    543         //mNewFbo->Bind(); 
     587        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     588        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     589 
     590        if (1) 
     591        { 
     592                // generate mip map levels for position texture 
     593                glBindTexture(GL_TEXTURE_2D, positionsTex); 
     594                glGenerateMipmapEXT(GL_TEXTURE_2D); 
     595 
     596                // generate mip map levels for position texture 
     597                glBindTexture(GL_TEXTURE_2D, colorsTex); 
     598                glGenerateMipmapEXT(GL_TEXTURE_2D); 
     599        } 
     600 
     601 
     602        // read the second buffer, write to the first buffer 
     603        mNewFbo->Bind(); 
     604        glDrawBuffers(3, mymrt); 
     605 
     606        GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 
     607        GLuint oldIllumTex = mOldFbo->GetColorBuffer(2)->GetTexture(); 
    544608 
    545609        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    546          
    547         glDrawBuffers(1, mymrt); 
    548610 
    549611        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    550  
    551         cgGLBindProgram(sCgCombineProgram); 
    552  
    553         cgGLSetTextureParameter(sColorsTexCombineParam, colorsTex); 
    554         cgGLEnableTextureParameter(sColorsTexCombineParam); 
    555  
    556         cgGLSetTextureParameter(sSsaoTexCombineParam, ssaoTex); 
    557         cgGLEnableTextureParameter(sSsaoTexCombineParam); 
     612        cgGLBindProgram(sCgGiProgram); 
     613 
     614        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex); 
     615        cgGLEnableTextureParameter(sPositionsTexGiParam); 
     616 
     617        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex); 
     618        cgGLEnableTextureParameter(sColorsTexGiParam); 
     619 
     620        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex); 
     621        cgGLEnableTextureParameter(sNormalsTexGiParam); 
     622 
     623        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex); 
     624        cgGLEnableTextureParameter(sNoiseTexGiParam); 
     625 
     626        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex); 
     627        cgGLEnableTextureParameter(sOldSsaoTexGiParam); 
     628 
     629        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex); 
     630        cgGLEnableTextureParameter(sOldIllumTexGiParam); 
     631 
     632        cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 
     633         
     634        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor); 
     635        cgGLSetParameter1f(sExpFactorGiParam, expFactor); 
     636 
     637 
     638        // q: should we generate new samples or only rotate the old ones? 
     639        // in the first case, the sample patterns look nicer, but the kernel 
     640        // needs longer to converge 
     641        GenerateSamples();  
     642        cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 
     643 
     644        Vector3 tl, tr, bl, br; 
     645        ComputeViewVectors(tl, tr, bl, br); 
    558646 
    559647        glColor3f(1.0f, 1.0f, 1.0f); 
    560648 
    561         const float offs = 0.5f; 
    562  
    563649        glBegin(GL_QUADS); 
    564650 
    565         glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 
    566         glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 
    567         glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f); 
    568         glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f); 
     651        // note: slightly larger texture hides ambient occlusion error on border but costs resolution 
     652        //const float new_offs = 0.55f; 
     653        const float new_offs = 0.5f; 
     654         
     655        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f); 
     656        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f); 
     657        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f); 
     658        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f); 
    569659 
    570660        glEnd(); 
    571661 
    572         cgGLDisableTextureParameter(sColorsTexCombineParam); 
    573         cgGLDisableTextureParameter(sSsaoTexCombineParam); 
    574  
    575         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     662        cgGLDisableTextureParameter(sColorsTexGiParam); 
     663        cgGLDisableTextureParameter(sPositionsTexGiParam); 
     664        cgGLDisableTextureParameter(sNormalsTexGiParam); 
     665        cgGLDisableTextureParameter(sNoiseTexGiParam); 
     666        cgGLDisableTextureParameter(sOldSsaoTexGiParam); 
     667        cgGLDisableTextureParameter(sOldIllumTexGiParam); 
    576668 
    577669        FrameBufferObject::Release(); 
    578670 
    579         PrintGLerror("deferred shading"); 
     671        PrintGLerror("globillum first pass"); 
    580672} 
    581673 
Note: See TracChangeset for help on using the changeset viewer.