Sign in to follow this  
Kurt-olsson

Adding Primitives to Shadow Mapping

Recommended Posts

Hi!

I am using this tutorial to try to understand how to use shadow-mapping
http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/soft-edged-shadows-r2193

I have downloaded the source code and got it to work. I understand in a small way how the shadowmap is rendered and using the effect file to calculate the shadows.

Now i am trying to add Primitives like triangles/cubes and sprites to my scene but can´t get it to work, i get the Triangle to show, but not in World-Space with the other models. I am completely stuck and have no ide where that code would be?

Where should i render my primitives in this code and how can i get them in world-space?

I have made lots of demo with just triangles / cubes and sprites with blending etc, it is just that the shadow-mapping code confuses me.

Any help is greatfully accepted. =)

My UpdateFrame function:
[source lang="cpp"]HRESULT FrameMove()
{
//
// Camera space matrices
//

// Computee the world matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
// Compute the view matrix
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &g_vEyePos, &g_vEyeAim, &g_vUp );
// Compute the projection matrix
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian(60.0f), (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, 1.0f, 1024.0f );
// Compute the world-view-projection matrix
D3DXMATRIX matWorldViewProj = matWorld * matView * matProj;


// World inverse matrix
D3DXMATRIX matWorldIT;
D3DXMatrixInverse( &matWorldIT, NULL, &matWorld );
D3DXMatrixTranspose( &matWorldIT, &matWorldIT );

//
// Light space matrices
//

// View matrix
D3DXVECTOR3 vLightPos = D3DXVECTOR3( 40.0f, 10.0f, -50.0f );
D3DXVECTOR3 vLightAim = D3DXVECTOR3( 0.0f, 10.0f, 0.0f );

D3DXMatrixLookAtLH( &matView, &vLightPos, &vLightAim, &g_vUp );

// Compute the projection matrix
D3DXMatrixOrthoLH( &matProj, 45.0f, 45.0f, 1.0f, 1024.0f );

// Compute the light-view-projection matrix
D3DXMATRIX matLightViewProj = matWorld * matView * matProj;

// Compute the texture matrix
float fTexOffs = 0.5 + (0.5 / (float)SHADOW_MAP_SIZE);
D3DXMATRIX matTexAdj( 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
fTexOffs, fTexOffs, 0.0f, 1.0f );

D3DXMATRIX matTexture = matLightViewProj * matTexAdj;

// Setup the effect variables
g_pEffect->SetMatrix( "g_matWorldViewProj", &matWorldViewProj );
g_pEffect->SetMatrix( "g_matLightViewProj", &matLightViewProj );
g_pEffect->SetMatrix( "g_matWorld", &matWorld );
g_pEffect->SetMatrix( "g_matWorldIT", &matWorldIT );
g_pEffect->SetMatrix( "g_matTexture", &matTexture );

g_pEffect->SetVector( "g_vLightPos", (D3DXVECTOR4*)&vLightPos );
g_pEffect->SetVector( "g_vEyePos", (D3DXVECTOR4*)&g_vEyePos );
g_pEffect->SetVector( "g_vLightColor", &D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 0.5f ) );

g_pEffect->SetBool("g_bFiltered", g_bFiltered);

return S_OK;
}[/source]
I think the scene is set, i can rotate and walk around when manipulatin the View but my primitives are not placing correctly.

This is the Render Scene:

[source lang="cpp"]HRESULT Render()
{



// Clear the viewport
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );

// Begin rendering the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{



g_pd3dDevice->SetFVF(CUSTOMFVF);
g_pd3dDevice->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);


// Grab the old render target and depth buffer
g_pd3dDevice->GetRenderTarget( 0, &g_pOldColorRT );
g_pd3dDevice->GetDepthStencilSurface( &g_pOldDepthRT );

// Render the scene depth to the shadow map
g_pd3dDevice->SetRenderTarget( 0, g_pShadowSurf );
g_pd3dDevice->SetDepthStencilSurface( g_pShadowDepth );

// Clear the viewport
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFFFFFFFF, 1.0f, 0 );

// Set the technique
g_pEffect->SetTechnique( "techShadow" );

// Render the effect
UINT uPasses = 0;
g_pEffect->Begin( &uPasses, 0 );

for( UINT uPass = 0; uPass < uPasses; uPass++ )
{
// Set the current pass
g_pEffect->BeginPass( uPass );


// Draw the floor
g_pScene->DrawSubset(0);

// Draw the statue
g_pScene->DrawSubset(1);
g_pScene->DrawSubset(2);

// End the current pass
g_pEffect->EndPass();
}

// End the effect
g_pEffect->End();

if( g_bSoftShadows )
{
// Render the shadowed scene into the screen map
g_pd3dDevice->SetRenderTarget( 0, g_pScreenSurf );
g_pd3dDevice->SetDepthStencilSurface( g_pNewDepthRT );

// Clear the viewport
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );

// Set the technique
g_pEffect->SetTechnique( "techUnlit" );

// Set the textures
g_pEffect->SetTexture( "tShadowMap", g_pShadowMap );

// Render the effect
uPasses = 0;
g_pEffect->Begin( &uPasses, 0 );

for(int uPass = 0; uPass < uPasses; uPass++ )
{
// Set the current pass
g_pEffect->BeginPass( uPass );

// Draw the floor
g_pScene->DrawSubset(0);

// Draw the statue
g_pScene->DrawSubset(1);
g_pScene->DrawSubset(2);

// End the current pass
g_pEffect->EndPass();
}

// End the effect
g_pEffect->End();

// Blur the screen map horizontally
g_pd3dDevice->SetRenderTarget( 0, g_pBlurSurf[0] );
g_pd3dDevice->SetDepthStencilSurface( g_pNewDepthRT );

// Clear the viewport
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );

// Set the technique
g_pEffect->SetTechnique( "techBlurH" );

// Compute the Gaussian offsets
GetGaussianOffsets(TRUE, D3DXVECTOR2(1.0f / (FLOAT)SHADOW_MAP_SIZE, 1.0f / (FLOAT)SHADOW_MAP_SIZE),
g_vSampleOffsets, g_fSampleWeights);
g_pEffect->SetValue("g_vSampleOffsets", g_vSampleOffsets, 15 * sizeof(D3DXVECTOR2));
g_pEffect->SetValue("g_fSampleWeights", g_fSampleWeights, 15 * sizeof(FLOAT));

// Set the textures
g_pEffect->SetTexture( "tScreenMap", g_pScreenMap );

// Render the effect
uPasses = 0;
g_pEffect->Begin( &uPasses, 0 );

for(int uPass = 0; uPass < uPasses; uPass++ )
{
// Set the current pass
g_pEffect->BeginPass( uPass );

// Draw the quad
g_pd3dDevice->SetStreamSource( 0, g_pQuadVB, 0, sizeof(QuadVertex) );
g_pd3dDevice->SetFVF( FVF_QUADVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );




// End the current pass
g_pEffect->EndPass();
}

// End the effect
g_pEffect->End();

// Blur the screen map vertically
g_pd3dDevice->SetRenderTarget( 0, g_pBlurSurf[1] );
g_pd3dDevice->SetDepthStencilSurface( g_pNewDepthRT );

// Clear the viewport
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );

// Set the technique
g_pEffect->SetTechnique( "techBlurV" );

// Compute the Gaussian offsets
GetGaussianOffsets(FALSE, D3DXVECTOR2(1.0f / (FLOAT)SHADOW_MAP_SIZE, 1.0f / (FLOAT)SHADOW_MAP_SIZE),
g_vSampleOffsets, g_fSampleWeights);
g_pEffect->SetValue("g_vSampleOffsets", g_vSampleOffsets, 15 * sizeof(D3DXVECTOR2));
g_pEffect->SetValue("g_fSampleWeights", g_fSampleWeights, 15 * sizeof(FLOAT));

// Set the textures
g_pEffect->SetTexture( "tBlurHMap", g_pBlurMap[0] );

// Render the effect
uPasses = 0;
g_pEffect->Begin( &uPasses, 0 );

for(int uPass = 0; uPass < uPasses; uPass++ )
{
// Set the current pass
g_pEffect->BeginPass( uPass );

// Draw the quad
g_pd3dDevice->SetStreamSource( 0, g_pQuadVB, 0, sizeof(QuadVertex) );
g_pd3dDevice->SetFVF( FVF_QUADVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );


// End the current pass
g_pEffect->EndPass();
}

// End the effect
g_pEffect->End();

// Restore the render target and depth buffer
g_pd3dDevice->SetRenderTarget( 0, g_pOldColorRT );
g_pd3dDevice->SetDepthStencilSurface( g_pOldDepthRT );
SAFE_RELEASE( g_pOldColorRT );
SAFE_RELEASE( g_pOldDepthRT );

// Finally, render the shadowed scene
g_pEffect->SetTechnique( "techScene" );

// Set the textures
g_pEffect->SetTexture( "tBlurVMap", g_pBlurMap[1] );
g_pEffect->SetTexture( "tSpotMap", g_pSpotMap );

// Render the effect
uPasses = 0;
g_pEffect->Begin( &uPasses, 0 );

// Do we need to render the scene in wireframe mode
if( g_bWireframe )
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );

for( int uPass = 0; uPass < uPasses; uPass++ )
{
// Set the current pass
g_pEffect->BeginPass( uPass );

// Draw the floor
g_pEffect->SetTexture( "tColorMap", g_pColorMap_Floor );
g_pEffect->CommitChanges();
g_pScene->DrawSubset(0);

// Draw the statue
g_pEffect->SetTexture( "tColorMap", g_pColorMap_Statue );
g_pEffect->CommitChanges();
g_pScene->DrawSubset(1);
g_pScene->DrawSubset(2);

// End the current pass
g_pEffect->EndPass();
}

// End the effect
g_pEffect->End();

// Restore the render state
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
}

else
{
// Restore the render target and depth buffer
g_pd3dDevice->SetRenderTarget( 0, g_pOldColorRT );
g_pd3dDevice->SetDepthStencilSurface( g_pOldDepthRT );
SAFE_RELEASE( g_pOldColorRT );
SAFE_RELEASE( g_pOldDepthRT );

// Otherwise, render the scene with hard shadows
g_pEffect->SetTechnique( "techSceneHard" );

// Set the textures
g_pEffect->SetTexture( "tShadowMap", g_pShadowMap );
g_pEffect->SetTexture( "tSpotMap", g_pSpotMap );

// Render the effect
uPasses = 0;
g_pEffect->Begin( &uPasses, 0 );

// Do we need to render the scene in wireframe mode
if( g_bWireframe )
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );

for(int uPass = 0; uPass < uPasses; uPass++ )
{
// Set the current pass
g_pEffect->BeginPass( uPass );

// Draw the floor
g_pEffect->SetTexture( "tColorMap", g_pColorMap_Floor );
g_pEffect->CommitChanges();
g_pScene->DrawSubset(0);

// Draw the statue
g_pEffect->SetTexture( "tColorMap", g_pColorMap_Statue );
g_pEffect->CommitChanges();
g_pScene->DrawSubset(1);
g_pScene->DrawSubset(2);

// End the current pass
g_pEffect->EndPass();
}

// End the effect
g_pEffect->End();

// Restore the render state
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
}



// End rendering the scene and present it
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

return S_OK;
}[/source]

Share this post


Link to post
Share on other sites
So shadow mapping is done generally in such a way that it is decoupled from your geometry. Meaning, it doesn't matter how you draw your geometry, as long as it's drawn into the shadow map.

So your flow normally goes like this:
BeginFrame

Bind your shadow camera
Bind your shadow map

Draw your occluders, (scene)

Bind your normal camera
Bind your render target

Draw your scene
Compare your object's depth in shadow camera view space vs the depth in the shadow map
Shade if your depth is greater than the depth in the map
Draw your object with applied shading
End Frame

This is a gross over simplification, but that's the goal.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this