Skip to main content
gamedev.net
🔒 Locked

Shadowmap problems...

Started by smitty1276 3 replies 550+ views
Original Post
smitty1276
smitty1276
Hey, guys... I'm trying to generate a shadowmap by rendering to a depth texture. So far, it doesn't appear to be working. Can anyone tell me if they see any particular reason why the following code wouldn't work? I an just going to stick the relevant snippets in the order in which they would execute... I'm stumped, and I'm sure I'm missing something dumb but can't see it at this point. I've dumped pixel values for the shadowmap and the depths are all ranging from 0.98-1.0... the view frustum spans from 0.1f to 150.0f, and the distance from the camera to the geometry in the scene ranges from 3.0 to 7.0, or somewhere in that area, so I'm thinking that the depth values are bad. (Does that sound like a reasonable assumption to make?)

// --- INITIALIZATION ---
glGenTextures(1, &shadowMapID);

// Setup shadow map texture object...
glBindTexture(GL_TEXTURE_2D, shadowMapID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 512, 512, 0,
    GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);    

// --- EVERY FRAME ---
// Save state info so we can change some stuff... 
glPushAttrib(GL_ENABLE_BIT      |
             GL_POLYGON_BIT     |
             GL_LIGHTING_BIT    |
             GL_VIEWPORT_BIT    |
             GL_PIXEL_MODE_BIT  |
             GL_COLOR_BUFFER_BIT);
    
// Cull front faces, use fastest shade model
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);

// Use crappy shade model to improve performance...
glShadeModel(GL_FLAT);    

// Disable writing to color buffer...
glColorMask(false, false, false, false);
   
// 512x512 depth texture...
glViewport(0, 0, 512, 512);
glEnable(GL_POLYGON_OFFSET_FILL);
    
// Clear everything...
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glLoadIdentity();
gluLookAt(cgLight.pos[0], cgLight.pos[1], cgLight.pos[2], 
          0.0f, 2.0f, 0.0f,
          0.0f, 0.0f, 1.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, shadowMat);
cgGLSetMatrixParameterfc(shadowMatrixParam, shadowMat);

glCallList(floorID);

glTranslatef(0.0f, 2.0f, 0.0f);
glPushMatrix();
    glRotatef(rotatex, 0,1,0);	
    glRotatef(rotatey, 1,0,0);
    glCallList(torusID);
glPopMatrix();
        
glBindTexture(GL_TEXTURE_2D, shadowMapID);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512);

// Restore all of the saved state information
glPopAttrib();

// Clear the buffers for the actual rendering....
glClear(GL_DEPTH_BUFFER_BIT);

BTW, if I comment out the glClear at the end, I can see the scene from the camera's perspective rendered in solid black "mixed in" with the regular render from the camera's perspective.
zedzeek
zedzeek
Quote:
I'm stumped, and I'm sure I'm missing something dumb but can't see it at this point. I've dumped pixel values for the shadowmap and the depths are all ranging from 0.98-1.0.

that sounds about right, the depth buffer is nonlinear thus most values are gonna be very close to 1, make the near clip plane 10-100x larger and see how it changes
smitty1276
smitty1276
Thanks for the reply. Is there someway to actually LOOK AT my depth texture? I would like to be able to visually see that it is working without having to rely on the actually shadowing stuff to work. Right now, there are so many things that could be wrong I would like to isolate it to different stages.

_the_phantom_
_the_phantom_
You can just bind and draw a depth texture like a normal normal texture, just draw a quad with it on to see whats going on.
Alkiem
Alkiem
If you want to see your depth texture, simply draw a quad with your depth texture on it. But In order to really see sthg, as depth is non linear, I suggest that you put sthg like 1.0-15.0 for znear-zfar.

I don't know if you 're aware of that, but you must put:
glTexParameteri( texTarget, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB );glTexParameteri( texTarget, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL );

to be able to do depth comparison later for the shadow mapping pass. (But don't put it if you just want to see the depth texture).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.