Shadow Mapping

Started by
7 comments, last by PhillipHamlyn 10 years, 7 months ago

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.

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

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.

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.

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

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

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

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

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.

This topic is closed to new replies.

Advertisement