Original Post
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?) 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.
// --- 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);