• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Kurt-olsson

Adding Primitives to Shadow Mapping

2 posts in this topic

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]
0

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.
1

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  
Followers 0