Hello, while continuing my work on my deferred shader engine I descided to implement a shadowmapping feature.
Here is the process i do for each light that casts shadows: 1. render the scene from light point of view to create depth-shadowmap 2. store modelview and projection matrix 3. draw light to lightmap using g-buffer and shadowmap 3.1 build transformation matrix that goes from cameraspace to light-view-clipping space by using this matrix: transformation-matrix = light_projection_matrix * light_modelview_matrix * camera_modelview_matrix_inverse 3.2 In the shader: read fragment position from gbuffer, calculate shadowmap_texture_coordinate with: shadowmap_texture_coordinate = transformation-matrix * gbuffer_pos 3.3 do the is-in-shadow check stuff
I have several problems: 1. The shadowmap_texture_coordinate does not seem to have an orthographic projection. I fixed this by applying this to the shadowmap_texture_coordinate (sc): sc.xy = sc.xy / sc.z This is not correct in any way I think but I have no idea what else could cause the projection-bug.
2. When I read from the shadowmap I get values in [0,1]. How do I transform these to compare them with the distance of a fragment from the light-position since those are given in engine-units.
I hope someone will understand what I am doing sometimes I'm not sure my self
Wow thanks that worked!
Now one step further: Is there a more efficient way of rendering a cubemap than rendering all 6 directions seperately ? I found something that uses a geometry shader to render to all 6 faces of the cube at one time but that looks to complicated for me.
It is possible to use a spherical mapping. That is, drawing only once, but map the sphere onto a quadratic texture. The advantage is that you only need to do one drawing. The advantage is that you don't get the same resolution in the shadow map everywhere. It will have lowest resolution at the equator, and a singularity at the poles. This may be acceptable, depending on your requirements. Search for "spherical shadowmaps" and you will find a lot.
See also for an illustration and idea how to improve on the singularities at the poles.
ok, I guess I will be fine with my cube map for now ... with a little bit of optimization it will only draw visible objects for each side of the cube and I share one FBO for the shadow map. Thanks for your help!