• 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
Endemoniada

Simple Shadow Mapping

11 posts in this topic

Hi guys, I'm trying to get simple shadow mapping working before I move on to percentage-closer and/or variance methods. I can't seem to get it to work so first I want to make sure I'm making the map correctly.

 

Here is the pertinent code:

d3d_device->CreateDepthStencilSurface(512,512,D3DFMT_D16,D3DMULTISAMPLE_NONE,0,TRUE,&shadow_depth_surface,NULL);
d3d_device->CreateTexture(512,512,1,D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&shadow_texture,NULL);
 
D3DXVECTOR3 eye(0.0f, 8.0f, 0.0f);
D3DXVECTOR3 look(8.0f, 0.0f, 8.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
 
D3DXMatrixLookAtLH(&shadow_view_matrix,&eye,&look,&up);
 
D3DXMatrixPerspectiveFovLH(&shadow_projection_matrix, pi/2.0f, 1.0f, 1.0f, 100.0f);
 
D3DXMatrixMultiply(&shadow_world_view_projection,&shadow_view_matrix,&shadow_projection_matrix);

Here is the shader:

VS_OUTPUT vertexShader(VS_INPUT input)
{
 VS_OUTPUT output;

 output.position=mul(input.position,WorldViewProjection);
 output.depth=output.position.zw;

 return output;
}
 
float4 pixelShader(VS_OUTPUT input) : COLOR
{
 float d=input.depth.x/input.depth.y;

 return float4(d,d,d,1);
}

The shadow map seems alright but it's mostly white. Am I using the correct formats when I create the depth surface and texture ? The near clipping value in the projection matrix is very sensitive and makes a huge difference in how the map looks, it's not very satisfying.

 

I don't think my problem is with the map generation but I want to make sure it's correct before asking about the rest of the algorithm.

 

Thanks a lot.

Edited by Endemoniada
0

Share this post


Link to post
Share on other sites

Shader looks fine, depth-stencil format is ok-ish, but you might play with higher precision (D24X8 or even D32). I'm concerned about your render target format though. 8 bit is definitively too little. Usually you use a full float (but single channel) format for the depth map, i.e. R32F.

 

Not sure why it's all white. This can happen if your scene ends up mostly at the far plane - which in turn  may indicates that your shadow  view-projection is wrong.

 

As always: Running such things through PIX or similar really helps. One can inspect both depth and render-target after each draw call (which in this case should look practically the same)

1

Share this post


Link to post
Share on other sites

I feel like I'm getting closer but it's still not right. I changed my texture creation format to D3DFMT_R32F.

 

Using the same code above here is the part that renders the scene with the shadow:

struct VS_OUTPUT
{
 float4 position : POSITION;
 float4 lpos : TEXCOORD1;
...
};
 
VS_OUTPUT vertexShader(VS_INPUT input)
{
 VS_OUTPUT output;
 output.position=mul(input.position,WorldViewProjection);
 output.lpos=mul(input.position,LightWorldViewProjection);
 ....
}
 
float4 pixelShader(VS_OUTPUT input) : COLOR
{
 ...
 float I=lighting model result

 input.lpos.xyz/=input.lpos.w;
 
 input.lpos.x=input.lpos.x/2+0.5;
 input.lpos.y=input.lpos.y/-2+0.5;

 float depth=tex2D(Sampler0,input.lpos.xy).r;

 if(depth < input.lpos.z)
  I*=0.1f;

 return float4(color.rgb*I,1);
}

I think my shader is alright and the problem is how I'm setting it all up but I don't really know. I'm also not sure if I should be doing the depth test int the light's eye space or it's clip space. Thanks for helping.

Edited by Endemoniada
0

Share this post


Link to post
Share on other sites

You're welcome. Shader looks fine, yes.

 

Clip space ? Hmm, not sure if this is possible - or even makes sense. Haven't seen such an approach yet.

 

I think you either have a wrong setup (read: transformations) or you experience now the "fun" stuff with shadow mapping, i.e. acne or self-shadowing. Screenshots might help.

1

Share this post


Link to post
Share on other sites

Yay, I got it to work.

 

I want to make them softer now, what do you guys recommend for a topdown/isometric view ? The shadows will be pretty dim so they don't need to be perfect, I just don't want any obvious eye sores.

 

Thanks.

0

Share this post


Link to post
Share on other sites
You've already mentioned approaches in your original post. PCF is the next thing to do. Try a 4-tap first, then increase the kernel (and taps). You might also want to play with the shadow map resolution. Also, the 4-tap might be available through special formats by the hardware directly (so-called hardware PCF. Search the forum and for NVidia papers).
 
Since even whole books are written about this topic, one probably can't tell you what approach is best for your use case wink.png. Edited by unbird
0

Share this post


Link to post
Share on other sites

Hi, I'm trying to do a simple 3x3 averaging and I can't understand why it doesn't work:

 

 
pixel shader:
 
//
input.lpos.xyz/=input.lpos.w;
input.lpos.x=(input.lpos.x*0.5f)+0.5f;
input.lpos.y=(-input.lpos.y*0.5f)+0.5f;
 
// the following works ok
 
float depth=tex2D(Sampler0,input.lpos.xy).r;
 
if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 
// but this doesn't
 
float2 offset;
float size=1024.0f; // shadow map size in pixels
float depth=0.0f;
 
for(int x=-1;x<=1;x++){
 for(int y=-1;y<=1;y++){
  offset=float2(x,y)/size;
  depth+=tex2D(Sampler0,input.lpos.xy+offset).r;
 }
}
 
depth=depth/9.0f;

if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 
0

Share this post


Link to post
Share on other sites

Hi, I'm trying to do a simple 3x3 averaging and I can't understand why it doesn't work:

 

 
pixel shader:
 
// the position transformed by the light matrix
input.lpos.xyz/=input.lpos.w;
input.lpos.x=(input.lpos.x*0.5f)+0.5f;
input.lpos.y=(-input.lpos.y*0.5f)+0.5f;
 
// the following works ok
 
float depth=tex2D(Sampler0,input.lpos.xy).r;
 
if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 
// but this doesn't
 
float2 offset;
float size=1024.0f; // shadow map size in pixels
float depth=0.0f;
 
for(int x=-1;x<=1;x++){
 for(int y=-1;y<=1;y++){
  offset=float2(x,y)/size;
  depth+=tex2D(Sampler0,input.lpos.xy+offset).r;
 }
}
 
depth=depth/9.0f;

if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 

 

I get areas that shouldn't be in shadow shadowed (the parts perpendicular to the light ray) and the shadow edge looks the same, it's not smoothed even slightly, plus a few other problems.

 

This stuff is complicated.

 

Thanks.

 

0

Share this post


Link to post
Share on other sites
Even more if I link to a source which makes you skip a step, sorry about that. The GPU Gems article seems to already use hardware PCF. Look closely: Your original shader has a depth comparison, your latest just adds values from the depth map without comparing (which is wrong). You can't use that source if you haven't setup hardware PCF explicitly.
 
Here's a better example. Not that bare, maybe you wanna look at a the original paper or grab F.Luna's book's source code (chapter 22)

PS: And yes, shadows are a pain. Edited by unbird
0

Share this post


Link to post
Share on other sites

Hi, I see what you mean about the depth comparison. In any case, I pretty much copied and pasted the Frank Luna code (which seems identical to the DirectXSDK sample) and the shadows seem alright but I'm getting Moire patterns, the code is the same so it's weird.

 

Also, in his code he does this to clear the buffer before making the shadow map:

 

gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);

 

Notice the color is 0, I don't see how that can possibly work, shouldn't it be 0xffffffff ? In fact, that's why my original setup wasn't working, I was clearing it to zero and not one.

 

I wonder why my code isn't working, it's exactly like his and the DXSDK sample. Here is how it looks (finally a screen):

 

5pip.jpg

Edited by Endemoniada
0

Share this post


Link to post
Share on other sites
Looks like self-shadowing (though weird you don't get the same effect on the floor). Use a higher bias for the depth comparison (SHADOW_EPSILON in Luna's code) or only render the backfaces to your shadow map by flipping the cull mode.
 
To high an epsilon will result in so-called peter-panning (objects look like they hover above ground). It's hard to get straight forward shadow mapping right - if possible at all.

Edit:

Notice the color is 0, I don't see how that can possibly work, shouldn't it be 0xffffffff ? In fact, that's why my original setup wasn't working, I was clearing it to zero and not one.


You're right, it should be. Depends, though. The SDK sample e.g. is "indoors" so I guess every pixel gets drawn anyway (you wouldn't even need a target clear). Edited by unbird
1

Share this post


Link to post
Share on other sites

Thanks a lot unbird. I realized SSM has lots of inherent problems so I think it's time to switch over to variance or more probably exponential shadow mapping. Exponential seems like the way to go but it's hard to find much about it. I'll probabky start a new post once I get something up and running. I'm learning a lot and that's a good thing :)

 

Thanks again.

0

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