Ignore:
Timestamp:
09/02/08 09:07:45 (16 years ago)
Author:
mattausch
Message:

shadow mapping almost working (but ulgy!!)

File:
1 edited

Legend:

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

    r2893 r2894  
    7979 
    8080 
    81 ShadowMapping::ShadowMapping(const AxisAlignedBox3 &sceneBox, int size): 
    82 mSceneBox(sceneBox), mSize(size) 
     81ShadowMap::ShadowMap(int size, const AxisAlignedBox3 &sceneBox, Camera *cam): 
     82mSceneBox(sceneBox), mSize(size), mCamera(cam) 
    8383{ 
    8484        mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true); 
     
    9191 
    9292 
    93 ShadowMapping::~ShadowMapping() 
    94 { 
    95         //if (sCgShadowProgram) cgDestroyProgram(sCgShadowProgram); 
     93ShadowMap::~ShadowMap() 
     94{ 
    9695        DEL_PTR(mFbo); 
    9796} 
    9897 
    9998 
    100 void ShadowMapping::Init(CGcontext context) 
    101 {/* 
    102         sCgShadowProgram =  
    103                 cgCreateProgramFromFile(context,  
    104                                                                 CG_SOURCE, 
    105                                                                 "src/shaders/shadow.cg",  
    106                                                                 RenderState::sCgFragmentProfile, 
    107                                                                 "main", 
    108                                                                 NULL); 
    109  
    110         if (sCgShadowProgram != NULL) 
    111         { 
    112                 cgGLLoadProgram(sCgShadowProgram); 
    113                 sShadowParam = cgGetNamedParameter(sCgShadowProgram, "shadowMap"); 
    114         } 
    115         else 
    116                 cerr << "shadow program failed to load" << endl; 
    117 */ 
    118         PrintGLerror("init"); 
    119 } 
    120  
    121  
    122 void ShadowMapping::ComputeShadowMap(Light *light, RenderTraverser *traverser) 
     99 
     100void ShadowMap::ComputeShadowMap(Light *light, RenderTraverser *traverser) 
    123101{ 
    124102        mLight = light; 
     
    133111 
    134112        pos -= light->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f); 
    135         //pos.z = mSceneBox.Max().z; 
    136         //mShadowCam->SetPosition(pos - Magnitude(mSceneBox.Diagonal() * light->GetDirection())); 
    137113        mShadowCam->SetPosition(pos); 
    138114 
     
    160136        glLoadIdentity(); 
    161137         
     138        // setup projection 
    162139        glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal()));  
    163140         
     
    168145        glLoadIdentity(); 
    169146 
    170  
     147        // setup shadow camera 
    171148        mShadowCam->SetupCameraView(); 
    172         mShadowCam->GetModelViewMatrix(mLightViewMatrix); 
    173  
    174         mShadowCam->GetProjectionMatrix(mLightProjectionMatrix); 
    175         //glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mLightProjectionMatrix.x); 
     149 
     150        ////////////// 
     151        //-- compute texture matrix 
     152 
     153        Matrix4x4 lightView, lightProj; 
     154 
     155        mShadowCam->GetModelViewMatrix(lightView); 
     156        mShadowCam->GetProjectionMatrix(lightProj); 
     157 
     158        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f, 
     159                                                                0.0f, 0.5f, 0.0f, 0.5f, 
     160                                                                0.0f, 0.0f, 0.5f, 0.5f, 
     161                                                                0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1] 
     162 
     163        Matrix4x4 lightProjView = lightView * lightProj; 
     164        mTextureMatrix = lightProjView * biasMatrix;  
     165 
     166 
     167        ///////////// 
     168        //-- render scene into shadow map 
    176169 
    177170        traverser->RenderScene(); 
     
    192185 
    193186        delete [] data; 
    194 */ 
     187        */ 
    195188        PrintGLerror("shadow map"); 
    196189 
     
    198191} 
    199192 
    200 /* 
    201 void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam) 
    202 { 
    203         Matrix4x4 cam_proj; 
    204         Matrix4x4 cam_inverse_modelview; 
    205  
    206         const float bias[16] =  
    207         {0.5f, 0.0f, 0.0f, 0.0f,  
    208         0.0f, 0.5f, 0.0f, 0.0f, 
    209         0.0f, 0.0f, 0.5f, 0.0f, 
    210         0.5f, 0.5f, 0.5f, 1.0f  }; 
    211  
    212  
    213         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    214  
    215         // update the camera, so that the user can have a free look 
    216         glMatrixMode(GL_MODELVIEW); 
    217         glLoadIdentity(); 
    218          
    219         glMatrixMode(GL_PROJECTION); 
    220         glLoadIdentity(); 
    221          
    222         cam->SetupCameraView(); 
    223  
    224         // store the inverse of the resulting modelview matrix for the shadow computation 
    225         glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x); 
    226         cam_inverse_modelview.Invert(); 
    227  
    228         // bind shadow map 
    229         glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, mFbo->GetDepthTex()); 
    230  
    231         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); 
    232         //glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_COMPARE_MODE, GL_NONE); 
    233  
    234         PrintGLerror("texture"); 
    235  
    236         //float light_m[16]; 
    237          
    238         // compute a matrix that transforms from camera eye space to light clip space 
    239         // and pass it to the shader through the OpenGL texture matrices, since we 
    240         // don't use them now 
    241         glEnable(GL_TEXTURE_2D); 
    242         glMatrixMode(GL_TEXTURE); 
    243  
    244         glLoadMatrixf(bias); 
    245         glMultMatrixf((float *)mShadowMatrix.x); 
    246          
    247         // multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview 
    248         // so that we can transform a pixel as seen from the camera 
    249         glMultMatrixf((float *)cam_inverse_modelview.x); 
    250          
    251                  
    252         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    253         cgGLBindProgram(sCgShadowProgram); 
    254          
    255         cgGLSetTextureParameter(sShadowParam, mFbo->GetDepthTex()); 
    256         cgGLEnableTextureParameter(sShadowParam); 
    257          
    258         // finally, draw the scene  
    259         traverser->RenderScene(); 
    260  
    261         cgGLDisableTextureParameter(sShadowParam); 
    262  
    263         PrintGLerror("shadow"); 
    264 } 
    265 */ 
    266  
    267  
    268 //Called to draw scene 
    269 void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam) 
    270 { 
    271 #if 0 
    272         Matrix4x4 cam_inverse_modelview; 
    273  
    274         //Calculate texture matrix for projection 
    275         //This matrix takes us from eye space to the light's clip space 
    276         //It is postmultiplied by the inverse of the current view matrix when specifying texgen 
    277         static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f, 
    278                                                                 0.0f, 0.5f, 0.0f, 0.0f, 
    279                                                                 0.0f, 0.0f, 0.5f, 0.0f, 
    280                                                                 0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1] 
    281  
    282         /*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    283  
    284         // update the camera, so that the user can have a free look 
    285         glMatrixMode(GL_MODELVIEW); 
    286         glLoadIdentity(); 
    287          
    288         glMatrixMode(GL_PROJECTION); 
    289         glLoadIdentity(); 
    290          
    291         cam->SetupCameraView(); 
    292 */ 
    293         // store the inverse of the resulting modelview matrix for the shadow computation 
    294         glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x); 
    295         cam_inverse_modelview.Invert(); 
    296  
    297         // bind shadow map 
    298         glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, mFbo->GetDepthTex()); 
    299          
    300         // compute a matrix that transforms from camera eye space to light clip space 
    301         // and pass it to the shader through the OpenGL texture matrices, since we 
    302         // don't use them now 
    303         glEnable(GL_TEXTURE_2D); 
    304         glMatrixMode(GL_TEXTURE); 
    305  
    306         glLoadMatrixf((float *)biasMatrix.x); 
    307         glMultMatrixf((float *)mLightViewMatrix.x); 
    308          
    309         // multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview 
    310         // so that we can transform a pixel as seen from the camera 
    311         glMultMatrixf((float *)cam_inverse_modelview.x); 
    312  
    313         RgbaColor white(1, 1, 1, 0); 
    314         RgbaColor dark(0.2, 0.2, 0.2, 0); 
    315  
    316         glDisable(GL_LIGHTING); 
    317  
    318         /*glDisable(GL_LIGHT0); 
    319         glDisable(GL_LIGHT1); 
    320  
    321         //Use dim light to represent shadowed areas 
    322         glLightfv(GL_LIGHT1, GL_POSITION, mLight->GetDirection()); 
    323         glLightfv(GL_LIGHT1, GL_AMBIENT, (float *)&dark.r); 
    324         glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r); 
    325         //glLightfv(GL_LIGHT1, GL_SPECULAR, black); 
    326         glEnable(GL_LIGHT1); 
    327         glEnable(GL_LIGHTING);*/ 
    328  
    329         PrintGLerror("firstpass"); 
    330  
    331         // finally, draw the scene  
    332         traverser->RenderScene(); 
    333  
    334  
    335         glEnable(GL_LIGHTING); 
    336  
    337  
    338         //3rd pass 
    339         //Draw with bright light 
    340         //glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r); 
    341         //glLightfv(GL_LIGHT1, GL_SPECULAR, (float *)&white.r); 
    342  
    343         Matrix4x4 textureMatrix = biasMatrix * mLightProjectionMatrix * mLightViewMatrix; 
    344  
    345         Matrix4x4 texMatT = Transpose(textureMatrix); 
    346  
    347         //Set up texture coordinate generation. 
    348         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); 
    349         glTexGenfv(GL_S, GL_EYE_PLANE, texMatT.x[0]); 
    350         glEnable(GL_TEXTURE_GEN_S); 
    351  
    352         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); 
    353         glTexGenfv(GL_T, GL_EYE_PLANE, texMatT.x[1]); 
    354         glEnable(GL_TEXTURE_GEN_T); 
    355  
    356         glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); 
    357         glTexGenfv(GL_R, GL_EYE_PLANE, texMatT.x[2]); 
    358         glEnable(GL_TEXTURE_GEN_R); 
    359  
    360         glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); 
    361         glTexGenfv(GL_Q, GL_EYE_PLANE, texMatT.x[3]); 
    362         glEnable(GL_TEXTURE_GEN_Q); 
    363  
    364         //Bind & enable shadow map texture 
    365         glBindTexture(GL_TEXTURE_2D, mFbo->GetDepthTex()); 
    366         glEnable(GL_TEXTURE_2D); 
    367  
    368         //Enable shadow comparison 
    369         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE); 
    370  
    371         //Shadow comparison should be true (ie not in shadow) if r<=texture 
    372         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); 
    373  
    374         //Shadow comparison should generate an INTENSITY result 
    375         glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY); 
    376  
    377         //Set alpha test to discard false comparisons 
    378         glAlphaFunc(GL_GEQUAL, 0.99f); 
    379         glEnable(GL_ALPHA_TEST); 
    380  
    381         PrintGLerror("texture"); 
    382  
    383         traverser->RenderScene(); 
    384  
    385         //Disable textures and texgen 
    386         glDisable(GL_TEXTURE_2D); 
    387  
    388         glDisable(GL_TEXTURE_GEN_S); 
    389         glDisable(GL_TEXTURE_GEN_T); 
    390         glDisable(GL_TEXTURE_GEN_R); 
    391         glDisable(GL_TEXTURE_GEN_Q); 
    392  
    393         //Restore other states 
    394         glDisable(GL_LIGHTING); 
    395         glDisable(GL_ALPHA_TEST); 
    396 #endif 
     193 
     194void ShadowMap::GetTextureMatrix(Matrix4x4 &m) const 
     195{ 
     196        m = mTextureMatrix; 
     197} 
     198 
     199  
     200unsigned int ShadowMap::GetShadowTexture() const 
     201{ 
     202        return mFbo->GetDepthTex(); 
    397203} 
    398204 
Note: See TracChangeset for help on using the changeset viewer.