Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Figuring out shadow mapping

This topic is 2907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have been using the D3D samples provided by Microsoft to figure out how to implement shadow mapping into my game engine(specifically the ShadowMap example). With out worrying about my shader code yet, I just want to post what I have going so far on the shadow map rendering, and let me know if it looks proper. I have commented most lines to the best of my understanding as to what that line does. I am still fairly new to advanced d3d stuff, and I want to make sure I am understanding what is going on, instead of blindly copying code.


_d3dDevice->GetRenderTarget(0, &oldRT); //Save the current render target to revert after building the shadow map.

_d3dDevice->GetDepthStencilSurface(&oldDS); //Save the current depth stencil to revert after building the shadow map.

_d3dDevice->CreateTexture(512, 512, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &shadowMap, 0);//Create the texture to hold the shadow map.
shadowMap->GetSurfaceLevel(0, &shadowSurf); //Get the surface of the texture to render to.

_d3dDevice->CreateDepthStencilSurface(512, 512, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, true, &shadowDepth, 0); //Create the depth stencil surface to hold the shadow depth information.

_d3dDevice->SetRenderTarget(0, shadowSurf); //Set my render target to my shadow texture
_d3dDevice->SetDepthStencilSurface(shadowDepth); //Set the depth buffer to my shadow depth surface.

/////////////////Hard coded light matrices for testing//////////////

//Get the view matrix for the light
D3DXMATRIX lightView;
D3DXMatrixLookAtLH(&lightView, &D3DXVECTOR3(0.0f, 50.0f, 0.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

//Get the projection matrix of the light
D3DXMATRIX lightProj;
D3DXMatrixPerspectiveFovLH(&lightProj, D3DXToRadian(45.0f), 1, 1.0f, 100.0f);


//This loop is my normal draw process, with a few things omitted and changed for shadow rendering purposes.

//render the shadow map
for(int i = 0; i < _bucket.Size(); i++)
D3DXMatrixTranslation(&_bucket.GetObjectIndependents()->matTranslate, _bucket.GetDrawLocation()->x, _bucket.GetDrawLocation()->y, _bucket.GetDrawLocation()->z);

//Usually my cameras view and cameras projection
D3DXMATRIX lightWorldViewProj = (_bucket.GetObjectIndependents()->matTranslate * lightView * lightProj);

//Set the indexed stream

//Draw the item
_bucket.Draw(_d3dDevice, lightWorldViewProj, true);

//After rendering shadow map, revert the render target and depth buffer

_d3dDevice->SetRenderTarget(0, oldRT);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!