Ignore:
Timestamp:
08/26/08 16:27:11 (16 years ago)
Author:
mattausch
Message:

changed ssao to multipass algorithm

File:
1 edited

Legend:

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

    r2867 r2868  
    1919 
    2020static CGprogram sCgSsaoProgram = NULL; 
     21static CGprogram sCgDeferredProgram2 = NULL; 
     22static CGprogram sCgAntiAliasingProgram = NULL; 
     23 
     24static CGparameter sColorsTexDeferredParam; 
     25static CGparameter sPositionsTexDeferredParam; 
     26static CGparameter sNormalsTexDeferredParam; 
    2127 
    2228static CGparameter sColorsTexParam; 
    2329static CGparameter sPositionsTexParam; 
    2430static CGparameter sNormalsTexParam; 
     31 
     32 
    2533static CGparameter sOldModelViewProjMatrixParam; 
    2634static CGparameter sMaxDepthParam; 
     
    3038static CGparameter sNoiseMultiplierParam; 
    3139static CGparameter sExpFactorParam; 
    32 static CGprogram sCgAntiAliasingProgram; 
    3340 
    3441static CGparameter sColorsTexAntiAliasingParam; 
     
    8188        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    8289         
     90        mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     91        // the diffuse color buffer 
     92        mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     93         
    8394 
    8495        // create noise texture for ssao 
     
    101112void SsaoShader::Init(CGcontext context) 
    102113{        
    103         /////////////// 
    104  
    105         sCgSsaoProgram =  
     114        sCgDeferredProgram2 =  
    106115                cgCreateProgramFromFile(context,  
    107116                                                                CG_SOURCE, 
    108117                                                                "src/shaders/deferred.cg",  
    109118                                                                RenderState::sCgFragmentProfile, 
    110                                                                 "main_ssao", 
     119                                                                "main2", 
     120                                                                NULL); 
     121 
     122        if (sCgDeferredProgram2 != NULL) 
     123        { 
     124                cgGLLoadProgram(sCgDeferredProgram2); 
     125 
     126                // we need size of texture for scaling 
     127                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "positions");   
     128                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "colors");   
     129                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "normals");  
     130        } 
     131        else 
     132                cerr << "deferred program failed to load" << endl; 
     133 
     134 
     135        /////////////// 
     136 
     137        sCgSsaoProgram =  
     138                cgCreateProgramFromFile(context,  
     139                                                                CG_SOURCE, 
     140                                                                "src/shaders/ssao.cg",  
     141                                                                RenderState::sCgFragmentProfile, 
     142                                                                "main", 
    111143                                                                NULL); 
    112144 
     
    121153                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture"); 
    122154                sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 
     155                 
    123156                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj"); 
    124157                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth"); 
     
    162195                                                float expFactor) 
    163196{ 
    164         cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 
    165  
    166         glPushAttrib(GL_VIEWPORT_BIT); 
    167         glViewport(0, 0, mWidth, mHeight); 
    168  
    169         glDrawBuffers(1, mymrt); 
    170  
    171         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    172  
    173         glDisable(GL_ALPHA_TEST); 
    174         glDisable(GL_TEXTURE_2D); 
    175         glDisable(GL_LIGHTING); 
    176  
    177         glMatrixMode(GL_PROJECTION); 
    178         glPushMatrix(); 
    179         glLoadIdentity(); 
    180  
    181         glMatrixMode(GL_MODELVIEW); 
    182         glPushMatrix(); 
    183         glLoadIdentity(); 
    184  
    185         const float offs = 0.5f; 
    186         glOrtho(-offs, offs, -offs, offs, 0, 1); 
    187  
     197         
    188198        // switch roles of old and new fbo 
    189199        // the algorihm uses two input fbos, where the one 
     
    192202        swap(mNewFbo, mOldFbo);  
    193203 
     204        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 
     205 
     206        glPushAttrib(GL_VIEWPORT_BIT); 
     207        glViewport(0, 0, mWidth, mHeight); 
     208 
     209        FrameBufferObject::Release(); 
     210 
     211        glDrawBuffers(1, mymrt); 
     212 
     213        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     214 
     215        glDisable(GL_ALPHA_TEST); 
     216        glDisable(GL_TEXTURE_2D); 
     217        glDisable(GL_LIGHTING); 
     218 
     219        glMatrixMode(GL_PROJECTION); 
     220        glPushMatrix(); 
     221        glLoadIdentity(); 
     222 
     223        glMatrixMode(GL_MODELVIEW); 
     224        glPushMatrix(); 
     225        glLoadIdentity(); 
     226 
     227        const float offs = 0.5f; 
     228        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     229 
     230        FirstPass(fbo); 
    194231        ComputeSsao(fbo, expFactor); 
    195         // the second pass just renders the combined solution 
    196         //DisplayTexture(); 
    197232        AntiAliasing(fbo); 
    198233 
     
    214249void SsaoShader::ComputeSsao(FrameBufferObject *fbo, float expFactor) 
    215250{ 
    216         GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     251        GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 
    217252        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    218253        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     
    228263        // read the second buffer, write to the first buffer 
    229264        mNewFbo->Bind(); 
     265        glDrawBuffers(1, mymrt); 
     266 
    230267        GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 
    231268 
    232         glDrawBuffers(1, mymrt); 
    233  
    234269        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    235270 
     271        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    236272        cgGLBindProgram(sCgSsaoProgram); 
    237273 
     
    385421 
    386422        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    387  
    388423        cgGLBindProgram(sCgAntiAliasingProgram); 
    389  
     424         
    390425        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex); 
    391426        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam); 
     
    393428        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex); 
    394429        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam); 
    395          
     430 
    396431        glColor3f(1.0f, 1.0f, 1.0f); 
    397432 
     
    418453 
    419454 
     455void SsaoShader::FirstPass(FrameBufferObject *fbo) 
     456{ 
     457        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     458        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     459        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     460 
     461        mFbo3->Bind(); 
     462        //mNewFbo->Bind(); 
     463 
     464        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     465         
     466        glDrawBuffers(1, mymrt); 
     467 
     468        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     469 
     470        cgGLBindProgram(sCgDeferredProgram2); 
     471 
     472        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex); 
     473        cgGLEnableTextureParameter(sColorsTexDeferredParam); 
     474 
     475        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex); 
     476        cgGLEnableTextureParameter(sPositionsTexDeferredParam); 
     477 
     478        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex); 
     479        cgGLEnableTextureParameter(sNormalsTexDeferredParam); 
     480         
     481        glColor3f(1.0f, 1.0f, 1.0f); 
     482 
     483        const float offs = 0.5f; 
     484 
     485        glBegin(GL_QUADS); 
     486 
     487        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 
     488        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 
     489        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f); 
     490        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f); 
     491 
     492        glEnd(); 
     493 
     494        cgGLDisableTextureParameter(sColorsTexDeferredParam); 
     495        cgGLDisableTextureParameter(sPositionsTexDeferredParam); 
     496        cgGLDisableTextureParameter(sNormalsTexDeferredParam); 
     497 
     498        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     499 
     500        FrameBufferObject::Release(); 
     501 
     502        PrintGLerror("deferred shading"); 
     503} 
     504 
    420505} // namespace 
Note: See TracChangeset for help on using the changeset viewer.