Ignore:
Timestamp:
08/25/08 17:34:34 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2861 r2865  
    2424 
    2525 
     26 
    2627static CGprogram sCgDeferredProgram; 
     28static CGprogram sCgAntiAliasingProgram; 
     29 
     30 
    2731static CGparameter sColorsTexParam; 
    2832static CGparameter sPositionsTexParam; 
    2933static CGparameter sNormalsTexParam; 
    3034 
     35static CGparameter sColorsTexAntiAliasingParam; 
     36static CGparameter sNormalsTexAntiAliasingParam; 
     37 
    3138 
    3239DeferredShader::DeferredShader(int w, int h): 
    3340mWidth(w), mHeight(h) 
    34 {} 
     41{ 
     42        mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     43        // the diffuse color buffer 
     44        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 
     45} 
    3546 
    3647 
     
    6576                cerr << "deferred program failed to load" << endl; 
    6677 
     78        sCgAntiAliasingProgram =  
     79                cgCreateProgramFromFile(context,  
     80                                                                CG_SOURCE, 
     81                                                                "src/shaders/antialiasing.cg",  
     82                                                                RenderState::sCgFragmentProfile, 
     83                                                                "main", 
     84                                                                NULL); 
     85 
     86        if (sCgAntiAliasingProgram != NULL) 
     87        { 
     88                cgGLLoadProgram(sCgAntiAliasingProgram); 
     89 
     90                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");   
     91                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals"); 
     92        } 
     93        else 
     94                cerr << "antialiasing program failed to load" << endl; 
     95 
    6796 
    6897        PrintGLerror("init"); 
     
    72101void DeferredShader::Render(FrameBufferObject *fbo) 
    73102{ 
    74         FrameBufferObject::Release(); 
    75  
     103        FirstPass(fbo); 
     104        AntiAliasing(fbo); 
     105} 
     106 
     107 
     108void DeferredShader::FirstPass(FrameBufferObject *fbo) 
     109{ 
    76110        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    77111        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    78112        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     113 
     114        // read the second buffer, write to the first buffer 
     115        mFbo->Bind(); 
    79116 
    80117        glPushAttrib(GL_VIEWPORT_BIT); 
     
    141178        glPopAttrib(); 
    142179 
     180        FrameBufferObject::Release(); 
     181 
    143182        PrintGLerror("deferred shading"); 
    144183} 
    145184 
    146185 
     186static void SetVertex(float x, float y, float x_offs, float y_offs) 
     187{ 
     188        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center 
     189        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top 
     190        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom 
     191        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top 
     192        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom 
     193 
     194        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right 
     195        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom 
     196 
     197        glVertex3f(x - 0.5f, y - 0.5f, -0.5f); 
     198} 
     199 
     200 
     201void DeferredShader::AntiAliasing(FrameBufferObject *fbo) 
     202{ 
     203        GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture(); 
     204        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     205 
     206        glPushAttrib(GL_VIEWPORT_BIT); 
     207        glViewport(0, 0, mWidth, mHeight); 
     208 
     209        glDisable(GL_ALPHA_TEST); 
     210        glDisable(GL_TEXTURE_2D); 
     211        glDisable(GL_LIGHTING); 
     212         
     213        glMatrixMode(GL_PROJECTION); 
     214        glPushMatrix(); 
     215        glLoadIdentity(); 
     216 
     217        glMatrixMode(GL_MODELVIEW); 
     218        glPushMatrix(); 
     219        glLoadIdentity(); 
     220 
     221        const float offs = 0.5f; 
     222         
     223        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     224        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     225 
     226        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     227 
     228        cgGLBindProgram(sCgAntiAliasingProgram); 
     229 
     230        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex); 
     231        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam); 
     232 
     233        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex); 
     234        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam); 
     235         
     236        glColor3f(1.0f, 1.0f, 1.0f); 
     237 
     238        float offs2 = 0.5f; 
     239 
     240        glBegin(GL_QUADS); 
     241 
     242        // the neighbouring texels 
     243        float x_offs = 1.0f / mWidth; 
     244        float y_offs = 1.0f / mHeight; 
     245 
     246        SetVertex(0, 0, x_offs, y_offs); 
     247        SetVertex(1, 0, x_offs, y_offs); 
     248        SetVertex(1, 1, x_offs, y_offs); 
     249        SetVertex(0, 1, x_offs, y_offs); 
     250 
     251        glEnd(); 
     252 
     253        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam); 
     254        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam); 
     255 
     256        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     257         
     258        glEnable(GL_LIGHTING); 
     259        glDisable(GL_TEXTURE_2D); 
     260         
     261        glMatrixMode(GL_PROJECTION); 
     262        glPopMatrix(); 
     263 
     264        glMatrixMode(GL_MODELVIEW); 
     265        glPopMatrix(); 
     266 
     267        glPopAttrib(); 
     268 
     269        PrintGLerror("deferred shading"); 
     270} 
     271 
     272 
    147273} // namespace 
Note: See TracChangeset for help on using the changeset viewer.