Ignore:
Timestamp:
09/01/08 18:33:14 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2879 r2892  
    22#include "FrameBufferObject.h" 
    33#include "RenderState.h" 
     4#include "ShadowMapping.h" 
    45 
    56 
     
    2829 
    2930static CGprogram sCgDeferredProgram; 
     31static CGprogram sCgDeferredShadowProgram; 
    3032static CGprogram sCgAntiAliasingProgram; 
    3133 
     
    3739static CGparameter sColorsTexAntiAliasingParam; 
    3840static CGparameter sNormalsTexAntiAliasingParam; 
     41 
     42static CGparameter sShadowMapParam; 
     43static CGparameter sPositionsTexShadowParam;   
     44static CGparameter sColorsTexShadowParam;   
     45static CGparameter sNormalsTexShadowParam; 
     46 
     47static CGparameter sShadowMatrixParam; 
     48 
     49 
    3950 
    4051 
     
    96107                cerr << "antialiasing program failed to load" << endl; 
    97108 
     109        sCgDeferredShadowProgram =  
     110                cgCreateProgramFromFile(context,  
     111                                                                CG_SOURCE, 
     112                                                                "src/shaders/deferred.cg",  
     113                                                                RenderState::sCgFragmentProfile, 
     114                                                                "main_shadow", 
     115                                                                NULL); 
     116 
     117        if (sCgDeferredShadowProgram != NULL) 
     118        { 
     119                cgGLLoadProgram(sCgDeferredShadowProgram); 
     120 
     121                // we need size of texture for scaling 
     122                sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");   
     123                sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");   
     124                sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");  
     125                sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");   
     126 
     127                sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix"); 
     128        } 
     129        else 
     130                cerr << "deferred program failed to load" << endl; 
    98131 
    99132        PrintGLerror("init"); 
     
    103136void DeferredShader::Render(FrameBufferObject *fbo) 
    104137{ 
     138        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     139 
     140        glDisable(GL_ALPHA_TEST); 
     141        glDisable(GL_TEXTURE_2D); 
     142        glDisable(GL_LIGHTING); 
     143 
     144        glPushAttrib(GL_VIEWPORT_BIT); 
     145        glViewport(0, 0, mWidth, mHeight); 
     146 
     147        glMatrixMode(GL_PROJECTION); 
     148        glPushMatrix(); 
     149        glLoadIdentity(); 
     150 
     151        glMatrixMode(GL_MODELVIEW); 
     152        glPushMatrix(); 
     153        glLoadIdentity(); 
     154 
     155        const float offs = 0.5f; 
     156        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     157 
     158 
    105159        FirstPass(fbo); 
    106160        AntiAliasing(fbo); 
     161 
     162        glEnable(GL_LIGHTING); 
     163        glDisable(GL_TEXTURE_2D); 
     164         
     165        glMatrixMode(GL_PROJECTION); 
     166        glPopMatrix(); 
     167 
     168        glMatrixMode(GL_MODELVIEW); 
     169        glPopMatrix(); 
     170 
     171        glPopAttrib(); 
     172 
     173        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     174} 
     175 
     176 
     177void DeferredShader::Render(FrameBufferObject *fbo,  
     178                                                        const Matrix4x4 &matViewing,  
     179                                                        ShadowMapping *shadowMapping) 
     180{ 
     181        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     182 
     183        glDisable(GL_ALPHA_TEST); 
     184        glDisable(GL_TEXTURE_2D); 
     185        glDisable(GL_LIGHTING); 
     186 
     187        glPushAttrib(GL_VIEWPORT_BIT); 
     188        glViewport(0, 0, mWidth, mHeight); 
     189 
     190        glMatrixMode(GL_PROJECTION); 
     191        glPushMatrix(); 
     192        glLoadIdentity(); 
     193 
     194        glMatrixMode(GL_MODELVIEW); 
     195        glPushMatrix(); 
     196        glLoadIdentity(); 
     197 
     198        const float offs = 0.5f; 
     199        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     200 
     201        FirstPassShadow(fbo, matViewing, shadowMapping); 
     202        AntiAliasing(fbo); 
     203 
     204        glEnable(GL_LIGHTING); 
     205        glDisable(GL_TEXTURE_2D); 
     206         
     207        glMatrixMode(GL_PROJECTION); 
     208        glPopMatrix(); 
     209 
     210        glMatrixMode(GL_MODELVIEW); 
     211        glPopMatrix(); 
     212 
     213        glPopAttrib(); 
     214 
     215        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    107216} 
    108217 
     
    117226        glDrawBuffers(1, mymrt + 3); 
    118227 
    119  
    120         glPushAttrib(GL_VIEWPORT_BIT); 
    121         glViewport(0, 0, mWidth, mHeight); 
    122  
    123         glDisable(GL_ALPHA_TEST); 
    124         glDisable(GL_TEXTURE_2D); 
    125         glDisable(GL_LIGHTING); 
    126          
    127         glMatrixMode(GL_PROJECTION); 
    128         glPushMatrix(); 
    129         glLoadIdentity(); 
    130  
    131         glMatrixMode(GL_MODELVIEW); 
    132         glPushMatrix(); 
    133         glLoadIdentity(); 
    134  
    135         const float offs = 0.5f; 
    136          
    137         glOrtho(-offs, offs, -offs, offs, 0, 1); 
    138228        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    139229 
    140230        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    141  
    142231        cgGLBindProgram(sCgDeferredProgram); 
    143232 
     
    170259        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    171260         
    172         glEnable(GL_LIGHTING); 
    173         glDisable(GL_TEXTURE_2D); 
    174          
    175         glMatrixMode(GL_PROJECTION); 
    176         glPopMatrix(); 
    177  
    178         glMatrixMode(GL_MODELVIEW); 
    179         glPopMatrix(); 
    180  
    181         glPopAttrib(); 
    182  
    183261        FrameBufferObject::Release(); 
    184262 
     
    207285        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    208286 
    209         glPushAttrib(GL_VIEWPORT_BIT); 
    210         glViewport(0, 0, mWidth, mHeight); 
    211  
    212         glDisable(GL_ALPHA_TEST); 
    213         glDisable(GL_TEXTURE_2D); 
    214         glDisable(GL_LIGHTING); 
    215          
    216         glMatrixMode(GL_PROJECTION); 
    217         glPushMatrix(); 
    218         glLoadIdentity(); 
    219  
    220         glMatrixMode(GL_MODELVIEW); 
    221         glPushMatrix(); 
    222         glLoadIdentity(); 
    223  
    224         const float offs = 0.5f; 
    225          
    226         glOrtho(-offs, offs, -offs, offs, 0, 1); 
     287         
    227288        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    228289 
     
    258319 
    259320        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    260          
    261         glEnable(GL_LIGHTING); 
    262         glDisable(GL_TEXTURE_2D); 
    263          
    264         glMatrixMode(GL_PROJECTION); 
    265         glPopMatrix(); 
    266  
    267         glMatrixMode(GL_MODELVIEW); 
    268         glPopMatrix(); 
    269  
    270         glPopAttrib(); 
     321 
     322        PrintGLerror("antialiasing"); 
     323} 
     324 
     325 
     326void DeferredShader::FirstPassShadow(FrameBufferObject *fbo,  
     327                                                                         const Matrix4x4 &matViewing,  
     328                                                                         ShadowMapping *shadowMapping) 
     329{ 
     330        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     331        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     332        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     333        GLuint shadowMap = shadowMapping->mFbo->GetDepthTex(); 
     334 
     335        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f, 
     336                                                                0.0f, 0.5f, 0.0f, 0.0f, 
     337                                                                0.0f, 0.0f, 0.5f, 0.0f, 
     338                                                                0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1] 
     339 
     340        Matrix4x4 inverseView = matViewing; 
     341        inverseView.Invert(); 
     342 
     343        Matrix4x4 lightProjView = shadowMapping->mLightViewMatrix * shadowMapping->mLightProjectionMatrix; 
     344 
     345        //cout << "matview:\n" << matViewing << endl; 
     346        //cout << "proj:\n" <<  shadowMapping->mLightProjectionMatrix << endl; 
     347        //cout << "view:\n" <<  shadowMapping->mLightViewMatrix << endl; 
     348 
     349        // compute combined matrix that transforms pixels into light texture space 
     350        /*Matrix4x4 shadowMatrix = biasMatrix *  
     351                                                         shadowMapping->mLightProjectionMatrix *  
     352                                                         shadowMapping->mLightViewMatrix * 
     353                                                         inverseView; 
     354*/ 
     355        Matrix4x4 shadowMatrix = //inverseView *  
     356                                                         lightProjView *  
     357                                                         biasMatrix; 
     358/* 
     359Matrix4x4 shadowMatrix = biasMatrix *  
     360                                                         lightProjView;  
     361                                                         //inverseView; 
     362*/ 
     363                                                         cout << "combined:\n" <<  shadowMatrix << endl; 
     364 
     365        fbo->Bind(); 
     366        glDrawBuffers(1, mymrt + 3); 
     367 
     368         
     369        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     370 
     371        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     372 
     373        cgGLBindProgram(sCgDeferredShadowProgram); 
     374 
     375        cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex); 
     376        cgGLEnableTextureParameter(sColorsTexShadowParam); 
     377 
     378        cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex); 
     379        cgGLEnableTextureParameter(sPositionsTexShadowParam); 
     380 
     381        cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex); 
     382        cgGLEnableTextureParameter(sNormalsTexShadowParam); 
     383         
     384        cgGLSetTextureParameter(sShadowMapParam, shadowMap); 
     385        cgGLEnableTextureParameter(sShadowMapParam); 
     386 
     387        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 
     388 
     389        glColor3f(1.0f, 1.0f, 1.0f); 
     390         
     391        glBegin(GL_QUADS); 
     392 
     393        float offs2 = 0.5f; 
     394 
     395        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f); 
     396        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f); 
     397        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f); 
     398        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f); 
     399 
     400        glEnd(); 
     401 
     402        cgGLDisableTextureParameter(sColorsTexShadowParam); 
     403        cgGLDisableTextureParameter(sPositionsTexShadowParam); 
     404        cgGLDisableTextureParameter(sNormalsTexShadowParam); 
     405        cgGLDisableTextureParameter(sShadowMapParam); 
     406 
     407        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     408         
     409        FrameBufferObject::Release(); 
    271410 
    272411        PrintGLerror("deferred shading"); 
     
    274413 
    275414 
     415 
    276416} // namespace 
Note: See TracChangeset for help on using the changeset viewer.