PDA

View Full Version : shadow mapping first try



jide
05-15-2006, 07:33 AM
I tried to do shadow mapping using shaders but I must admit I have pretty failed...
I do it the same way as when not using shaders: 1st pass is for light POV rendering (getting the depth map), 2nd pass is the normal rendering and the 3rd pass is where I apply the shadow map and where I use the shaders.
I only get a dark scene, so letting me know something is wrong somewhere.

Here are the snippets of the VS:

varying vec4 ev_pos;

void main (void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
ev_pos = gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * ev_pos;
}The texture matrix contains the translation, scaling, light proj, light view and the inverse of the modelview matrix.

and the one for the FS:


uniform sampler2DShadow sampler;

void main(void)
{
gl_FragColor = shadow2DProj (sampler, gl_TexCoord[0]);
}You noticed I do not use lights for the moment in the shaders.

zed
05-16-2006, 12:07 AM
someone on the other forumsdoing the saem thing, lighting the scene and then doing shadowing afterwards. this isnt how it works in real life
shadow == abscence of light
thus do
A/ create shadowmap
B/ draw scene if pixel isnt in shadow shade normally else apply ambient (or whatever) to it

eg
fragment shader

float shadow = shadow2DProj (sampler, gl_TexCoord[0]); // shadow is either 0 or 1
gl_FragColor = diffuse_shading * shadow;

also try
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_Vertex;

jide
05-16-2006, 05:39 AM
Thanks for the care. But I guess I bad explained. This is how I do shadowing: I enlight the lit geometry even if my code snippets didn't make it realize it goes this way.

In fact, I realized that shadow2DProj always return a null vec4. And I was pretty sure gl_TexCoord[0] was well initialized. So I have something wrong somewhere.

Using gl_Vertex for calculating the tex coords wouldn't be of help for me just because the vertices aren't set in world space, they have to be transformed by the modelview matrix. This is why the texture matrix contains the invert of the visualisation matrix (not the invert of the modelview as I stippled on my last post).

Here are more codes:


// in the rendering loop, shader has been set to be used before:

glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluLookAt (4,4,4,0,0,0,0,1,0);
glGetFloatv (GL_MODELVIEW_MATRIX, &view_matrix[0][0]);
InvertMatrix (view_matrix, inv_view_matrix);

glLightfv (GL_LIGHT0, GL_POSITION, l_pos);
glLightfv (GL_LIGHT0, GL_AMBIENT, l_amb);
glLightfv (GL_LIGHT0, GL_DIFFUSE, l_dif);
glLightfv (GL_LIGHT0, GL_SPECULAR, white);
glEnable (GL_LIGHT0);
glEnable (GL_LIGHTING);

glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, shadow_map_tex);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LESS);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

glMatrixMode (GL_TEXTURE);
glLoadIdentity();
glTranslatef (.5,.5,.5);
glScalef (.5,.5,.5);
glMultMatrixf (bias_matrix);
glMultMatrixf (light_projection_matrix);
glMultMatrixf (light_view_matrix);
glMultMatrixf (&inv_view_matrix[0][0]);
glMatrixMode (GL_MODELVIEW);

RenderScene();

{...}RenderScene contains all the rendering geometry, including some transformations.

Also, I tested the texture coordinates in the fragment shader with something like this:


gl_FragColor = gl_TexCoord[0]/gl_TexCoord[0]/q;And it appears to be well calculated: red on the right, green on the top and getting blue when going far. But sometimes, specially when it goes out of the light projection, values can be more than 1 or less than 0. I actually think this is not a problem.

I guess I miss some point somewhere but I still haven't been able to find where and what it could be.

Notes: I changed the way to do it since my first post here. In fact I'm now trying to do it in two passes only, so now all what I have is a all black scene.

Komat
05-16-2006, 04:05 PM
What contains the bias_matrix you use in calculation of texture matrix?

jide
05-16-2006, 04:20 PM
GLfloat bias_matrix[] =
{
.9, .0, .0, .0,
.0, 1., .0, .0,
.0, .0, .9, .0,
.0, .0, .0, 1.
};

I don't think this is the source of the problem since that if I do not use the biad matrix, nothing changed and this works well when not using shaders.

jide
05-17-2006, 09:09 AM
Okay, I got it, it was an hidden bad error... the sampler was bad passed to the shader... shame on me.

jide
05-20-2006, 04:34 AM
I have bad results on some areas on the shadow. Still don't understand why. Just have a look at the snaps.

http://dagecko.free.fr/images/snap_shaders_new.jpg

Komat
05-20-2006, 06:03 AM
About which areas you are talking? The holes in the shadow (caused by combination of backface culling and unclosed geometry) or something different?

EDIT: You have linked a incorrect image however the unshadowed line wou marked has the same cause as the missing shadow on the desk. It is caused by lid of the teapot that is not connected with teapot body. The view from camera looks trough this hole and because the part of the body of the teapot that would in real world cover that hole is culled out by backface culling, the shadowmap contains distance of the ground plane or maximal distance depending on if you render the ground plane into shadowmap or not.

jide
05-20-2006, 07:48 AM
Try this one:

http://dagecko.free.fr/images/snap_shaders_shadow_bug.jpg

Komat
05-20-2006, 07:55 AM
I have seen this image already when i have edited my message (I found it when I looked at the images from your lighting thread again).

jide
05-20-2006, 08:21 AM
I didn't understood your edit post like that... After reading it again. Okay, but now why the fixed pipeline does not produce that 'artefact' ? It also culls the backfaces.

I actually think I have a problem with the bias matrix. But I use the same for both: FF or shaders.

Something's definately wrong here.

Komat
05-20-2006, 08:36 AM
Was your light in exactly the same position in both fixed function and shader cases? At least from the screenshots from the lighting thread it does not look so (it is more on the side).

jide
05-20-2006, 11:48 AM
Yes I use the same code and didn't modified the light position neither any lighting parameters or material properties. However the light moves during the rendering: it makes a circle around the teapot.

EDIT: There's also a problem I'd better tell now: the shadow projection is not exactly the same whether it is done using shaders or the fixed pipeline. It looks more 'strange' when done in shaders...

Komat
05-20-2006, 12:42 PM
Do you have screenshots having light with the same position where the one made with shader has the artefact and the one made with fixed function is without the artefact?

Can you more specify what the "more strange" means?

jide
05-21-2006, 06:14 AM
Updated here:
http://dagecko.free.fr/images/

There's a difference about the projection whether it is done in shaders or FP. On the snaps I uploaded there's a little difference, seenable almost only on the frontier between the lid and the teapot. It is wider on the shader's one.
I'll try to put more snaps during the day.