Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Shadow Mapping

This topic is 2114 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

I'm currently trying to implement shadows

 

I have read some articles saying that I have to generate a depth texture to use it for generating shadows

 

Does that means I have to render the scene twice?

Do I have to use the depth texture to manipulate the final scene pixel color?

 

BTW, I'm currently having depth texture used for soft particles.

Share this post


Link to post
Share on other sites
Advertisement

 

Does that means I have to render the scene twice?

 

Yes, unless you can do some tricky geometry shader stuff, but I don't think the performance would be worth it, so yes.

 

 

Do I have to use the depth texture to manipulate the final scene pixel color?

 

Yes, so what you do in a nutshell is that you supply the shaders with this depth texture and the light properties plus the view (+projection, cant remember), and then reconstruct the shadow map on top of the current pixel.

 

 

 I'm currently having depth texture used for soft particles.

 

That wont be useful as the soft particle map is rendered with another camera location and lookat.

 

Hope this helps happy.png

-MIGI0027

Edited by Migi0027

Share this post


Link to post
Share on other sites

Simply said you first render the scene from the light's view (as if it was a camera and not a light, for a spotlight the camera has perspective projection, for a directional light it has ortho projection, for point lights it gets quite complicated smile.png ) and store just distances of individual pixels from the light (so you get depth texture - called shadow map).

Then you render the scene normally from your standard camera, but you use special shaders which for each pixel calculate its distance from the light (this is quite simple) and compare this with the pixel-light distance value from the shadow map (this requires some mathematical transformations done in the shader).

If the first distance is larger, then there clearly is some other object (pixel) between the currently rendered pixel and the light and thus the currently rendered pixel IS in shadow and should be rendered darker. If the first distance is smaller, than we are closer to the light than anything else in this particular direction and the pixel isn't in shadow.

Share this post


Link to post
Share on other sites
That wont be useful as the soft particle map is rendered with another camera location and lookat.

 

 

I'm creating the depth map for soft particles using the same view and projection matrix.

Share this post


Link to post
Share on other sites

 

That wont be useful as the soft particle map is rendered with another camera location and lookat.

 

 

I'm creating the depth map for soft particles using the same view and projection matrix.

 

 

Exactly, and that is the problem of using that map here, when rendering the shadow map, you have to render it, as an example, far up in the sky with a lookat to 0,0,0, then if you debug that shadow map, you see that it is looking down to the player and shows the depth of all objects (should), and then you can use that depth texture to calculate the shadow intensity of the object later from this map by re projecting it.

 

I hope this helps smile.png

-MIGI0027

Edited by Migi0027

Share this post


Link to post
Share on other sites

The tricky part is understanding the transformation between pixel you are coloring and the shadow map UV.

Share this post


Link to post
Share on other sites

@Migi0027: Got it! Thanks, what is the appropriate value for the camera look at when it look at the player from above (for creating shadow map)?

 

@Tispe: Any good example for using the depth texture for coloring pixels (to create the final scene with shadow)?

 

BTW, I'm talking about soft shadow.

Share this post


Link to post
Share on other sites

This should help you (Code wise):

 

http://rastertek.com/dx11tut40.html (It may look ugly, but It's some damn good resources)

http://rastertek.com/dx11tut42.html

 

Well, when talking about the lookat, I don't think in a position, instead I have e.g. this function: (I don't actually have it, it just came out of my mind tongue.png )

void C3DEngine::SetDirectionalVector(D3DXVECTOR3 const &v)
{
    Camera.SetView(
       v * 10, // Position
       v * 9   // Lookat
    );

    ...
}

 Yeah, the code may not be the best, and it's probably not the best way to archive it, but in this manner, you don't actually provide a lookat, just the directional vector, which I find much easier.

 

Hope this helps.

-MIGI0027

Edited by Migi0027

Share this post


Link to post
Share on other sites

Simply said you first render the scene from the light's view (as if it was a camera and not a light, for a spotlight the camera has perspective projection, for a directional light it has ortho projection, for point lights it gets quite complicated smile.png ) and store just distances of individual pixels from the light (so you get depth texture - called shadow map).

Then you render the scene normally from your standard camera, but you use special shaders which for each pixel calculate its distance from the light (this is quite simple) and compare this with the pixel-light distance value from the shadow map (this requires some mathematical transformations done in the shader).

If the first distance is larger, then there clearly is some other object (pixel) between the currently rendered pixel and the light and thus the currently rendered pixel IS in shadow and should be rendered darker. If the first distance is smaller, than we are closer to the light than anything else in this particular direction and the pixel isn't in shadow.

 

Tom KQT - I implemented shadow mapping, as a hobby programmer, and I wish I'd had your explanation to hand before I started. It took me a long time to reach the understanding you put in this single reply. Good summary.

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!