PDA

View Full Version : shadowmaps and selfshadowing



Bruno
09-04-2006, 03:07 AM
Hi,

I'm tryng to get shadowmaps working in my application, but i'm not sure if i'm doing this right.

What i'm doing right now, is i render to a texture (rgba) the object that i want, and then i project that texture to the ground, all good here, however if i project the same texture on the object itself, won't work.
So, instead of rendering to a rgba texture, i'm rendering to a depth texture..,
This are the steps i'm making :

The FBO setup

glGenFramebuffersEXT( 1, &g_BIG_SHADOWframeBuffer );
glGenRenderbuffersEXT( 1, &g_BIG_SHADOWdepthRenderBuffer );
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, g_BIG_SHADOWdepthRenderBuffer );
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 2048, 2048 );
depth texture setup

glGenTextures(1, &Local_Shadow);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Local_Shadow);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 2048, 2048, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER_ARB );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER_ARB );
Then, render the objects to the texture :


glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_BIG_SHADOWframeBuffer );
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, Local_Shadow, 0 );
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, g_BIG_SHADOWdepthRenderBuffer );
glViewport( 0, 0, 2048, 2048 );
render objects();

glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
glViewport( 0, 0, g_window->init.width, g_window->init.height );

Right now in theory i should have the depth texture.
Now, i will render the objects normally, and then in a 2nd pass with this settings and texture projection enabled :

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

I should get shadow projections, only in the areas where the Z-Buffer is lower or equal by comparing the framebuffer z values with the ones in the depth map, correct ?
However, this is not working, i must be doing something wrong, for a starts my depth map appears all white, no ideia why, it should be grayscale even with a lack of precision.

If anyone can point me in the right direction, i would apreciate.,
thanks,
Bruno

Komat
09-04-2006, 04:58 AM
You are rendering the texture incorrectly. The depth texture must be bound to the GL_DEPTH_ATTACHMENT_EXT point, not the color one.

To the color attachment you may bind a RGBA texture, RGBA render buffer or disable it entirely (like in code bellow)


// Bind the depth texture.

glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, Local_Shadow, 0 ) ;


// Disable the color attachment.

glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0 ) ;


// Disable color writes, we disabled the color attachment.

glDrawBuffer( GL_NONE ) ;
glReadBuffer( GL_NONE ) ;

Bruno
09-05-2006, 05:18 AM
Thanks for the tip, that was indeed one of the problems as now i can see the depth texture, however it seems that this :

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

Doens't work, as i get my objects all white., any ideia what i could be doing wrong ?
Been messing with this for hours now, and i can't see anything wrong here

plasmonster
09-05-2006, 06:48 AM
Why not just start with a working demo? Perhaps that would be easier than trying to debug a complex process like shadow mapping, snippet by snippet (there's just so many things that can go wrong).

k_szczech
09-05-2006, 07:17 AM
any ideia what i could be doing wrong ?Lots of things, but most likely:
-texture S and T coordinates are not calculated properly
-texture R coordinate in in the wrong range
-you have wrong texture bound to texture unit, you use wrong texture unit, texturing is not enabled
Try using GL_REPEAT and use some RGB texture (don't use COMPARE_R_TO_TEXTURE) to see if you have S and T calculated correctly.
If that works - use GL_TEXTURE matrix to swap S with R or T with R and see if you have R calculated correctly.
Then try that again with GL_CLAMP - if you are sure your texture is in place then continue trying with COMPARE_R_TO_TEXTURE.

Bruno
09-05-2006, 09:21 AM
-S and T are calculated correctly, if i do all this with a rgba texture, i can see the alpha being projected correctly on all the surfaces.
Rendering to a depth texture seems to be ok, as i see the objects in the texture itself, and the projection is working too ( when i disable COMPARE_R_TO_TEXTURE), but once i enable it i just get all white.
I also inverted the culling when rendering the objects(no go), and played around with the several depth tests that i can feed to the COMPARE_R_TO_TEXTURE field.
So, my guess is the R coordinate is out of range somehow ?
I haven't used any clipplanes or anything that might screw up the z-buffer.
Btw, i forgot to mention, the framebuffer is rendered with a perspective camera, the shadowmap is using a orthographic camera, i don't think this is a problem, right ?
How can i see if the R coordinate is in a wrong range ?

Komat
09-05-2006, 10:48 AM
How looks the matrix that you use to project the shadowmap. When do you scale and bias the coordinates from <-1,1> range into the <0,1> range, do you scale and bias the r coordinate as well? (DirectX based tutorials do not scale that coordiante, for OGL the scalling must be done).



How can i see if the R coordinate is in a wrong range ?
You can draw it as grayscale colors and look if it looks similiary to grayscale image of the shadowmap at appropriate places.

Bruno
09-05-2006, 11:41 AM
I think i got it working, i wasn't scaling\bias the r coordinate.
Thank you very much Komat :)