Sign in to follow this  
JSoftware

Deferred Shading and Shadowmap

Recommended Posts

JSoftware    318
Hello there i have a problem with my deferred shading engine. i've decided to implement shadow mapping for each light. only spotlights at the moment. i've found out how to generate the depth map. the geometry data is stored in 3 16floatingpoint textures(pos.x, pos.y, pos.z, normal.x, normal.y, normal.z, diffuse.x, diffuse.y, diffuse.z, spec, none, none). pos is transformed into modelview space when it's packed. how would i go about getting the shadow amount in the lighting program? my idea is to transform pos into lightspace by dp4'ing it with each row of the texture matrix, which is defined by bias*proj*view(?), this would give us a x and an y coord which we try to fetch from the depthmap and the z should contain the depth from the light. is that correct understood? Regards Jeppe

Share this post


Link to post
Share on other sites
blue_knight    194
Do a google search for "Forward Shadow Mapping". I'll look through my docs/code when I get home and post a more thorough explaination then if necessary. You have the basic idea, you concatenate a matrix to your shadow transform that transforms a position from view space into light space. The actual shader cost is the same as normal "Backward" shadowmapping.

Share this post


Link to post
Share on other sites
JSoftware    318
hello blu_knight,

it would certainly be good if you could dig up a few pieces of code! i've searched for anything i possibly could but i weren't able to find anything concrete on how to transform the position into light space. i've had nummerous tries on how to do it, it just won't work..

Regards Jeppe

Share this post


Link to post
Share on other sites
blue_knight    194
OK, its actually very simple assuming you have the shadow projection matrix built as normal using a square shadowmap. This assumes that positions are stored in viewspace in your G-Buffer. Here is what I have:

matInvView = inverse view matrix.
matLightViewProj = the lights view projection matrix used to render the shadowmap.
bias = bias applied to shadow comparison, can be 0.
fTOffs = 0.5 + 0.5/shadow_tex_width;
matTexAdj =
| 0.5 0.0 0.0 0.0 |
| 0.0 -0.5 0.0 0.0 |
| 0.0 0.0 1.0 0.0 |
|fTOff fTOff bias 1.0 |


texMtx = matInvView * matLightViewProj;
texMtx = texMtx * matTexAdj;

Then simply transform your view space position (x,y,z,1) by texMtx and then use that in your projective texture lookup. If you are using hardware shadowmaps then you're done otherwise you'll have to compare the depth of the shadowmap to the depth of your projected texture coordinates.

Share this post


Link to post
Share on other sites
blue_knight    194
So here's some explaination - code doesn't do a lot of good if you don't understand it right? Anyway the matTexAdj converts from projected coordinates in the range of [-1, +1] to [0, 1] to address the texture. The extra 0.5/shadow_tex_width is added to the offset to align the lookup to texel centers. The inverse view matrix is concatenated to get the position from viewspace to worldspace, then it is transformed to lightspace by matLightViewProj. And that's it. If you have ordinary shadowmapping working in a forward renderer, then this is very similar to that. The big difference is that you are transforming the viewspace position to lightspace directly, rather then projecting the light data into worldspace.

Share this post


Link to post
Share on other sites
JSoftware    318
Thank you very much for the explaination! i have no doubts it's correct, i just can't get it working yet. i found out that my inverse routine was wrong but that's corrected now. when i store position in the gbuffer should i then only multiply it with modelview or should i multiply it with mvp? you write viewspace, which i'm really sure is mvp'ed, shouldn't i then multiply the lights mvp with the inverse modelviewprojection instead of just the inverse modelview when i want the position in object space?

Again.. thank you very much

Regards Jeppe

Share this post


Link to post
Share on other sites
blue_knight    194
I store the position in viewspace in the G-Buffer, it makes lighting much easier since I can do it directly in viewspace. You should be using the inverse view matrix, not the inverse modelview matrix. Basically you want to transform the positions from worldspace to lightspace.

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