Reading the depth buffer

Started by
7 comments, last by _WeirdCat_ 10 years, 8 months ago
Hello,
I'm trying to get a simple shadowmapping demo up and running but I've run into a bit of a problem. I need to translate to the light's position, save the depth values into texture memory and finally generate texture coordinates based on the depth values. Now, my current code isn't working so I've been debugging it the whole day and I suspect the problem is with the transfer of depth buffer info into a texture.
Here's my code:
init:

glGenTextures(1, &shadowmap_);
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
render:

// position the light
glLightfv(GL_LIGHT0, GL_POSITION, lightPos_);
 
// set up the projection parameters from the light's POV
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
 
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// translate to the light's position
gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
 
// render the scene to get the depth information
renderSceneElements();
glPopMatrix();
 
// end the projection modification
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
 
// copy over the depth information
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512);
 
// render a simple quad with the shadowmap for debugging
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
glTranslatef(3.0f, 2.0f, 5.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
 
glTexCoord2f(1.0f, 0.0f);
glVertex3f(3.0f, 0.0f, 0.0f);
 
glTexCoord2f(1.0f, 1.0f);
glVertex3f(3.0f, 3.0f, 0.0f);
 
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 3.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
The result is a white quad :/
renderSceneElements() contains a bunch of VAOs.
Also, I know there's a way to copy over the depth buffer using FBOs. I want to implement that afterwards but first I'm curious as to what on Earth I'm doing wrong here.
Thanks in advance!
Advertisement

Welcome to graphics. Look around for shadow map white and you will get hundred of talks about this. The depth buffer is essentially completely white for every shadow map. Print screen, go into photoshop/gimp and change the brightness/contrast till you see an image outline. This works best if the object is close to the camera.

Also GL_UNSIGNED_INT should be GL_FLOAT, GL_DOUBLE, or potentially GL_DEPTH_COMPONENT as well. INT will not work that is for sure, everything in that case would be 1.0 depth for all pixels.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Depth buffers are VERY low contrast. In order to see something clearly, you'll either need to boost contrast in a shader when drawing your test quad, or put something VERY close to the near plane. That's of course assuming your render's even correct. Try clearing to a weird color. Happy debugging.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Welcome to graphics. Look around for shadow map white and you will get hundred of talks about this. The depth buffer is essentially completely white for every shadow map. Print screen, go into photoshop/gimp and change the brightness/contrast till you see an image outline. This works best if the object is close to the camera.

Also GL_UNSIGNED_INT should be GL_FLOAT, GL_DOUBLE, or potentially GL_DEPTH_COMPONENT as well. INT will not work that is for sure, everything in that case would be 1.0 depth for all pixels.

Tried the screenshot method and fiddling with the contrast but I couldn't see anything. But if it's supposed to be white then good. At one point I had a weird pattern appear and then it turned completely black. Yeh, I don't even know how I managed that :P

Depth buffers are VERY low contrast. In order to see something clearly, you'll either need to boost contrast in a shader when drawing your test quad, or put something VERY close to the near plane. That's of course assuming your render's even correct. Try clearing to a weird color. Happy debugging.

I shall have a look at my code tomorrow (and try clearing to an odd color) with a fresh head and if I can't figure it out, I'll post my full code for the scene.

Thank you both for your quick replies!

if you are using intel express graphics card (from notebooks) it is likely you wont see shadowmaps at all

I had posted here suggesting you use PIX, but then saw the opengl calls and hung my head in shame.



if you are using intel express graphics card (from notebooks) it is likely you wont see shadowmaps at all

Nah, I'm on nVidia's GTX580.

So, I had a look at my code from a different perspective this time, assuming that the depth buffer got mapped correctly into texture memory. And I found a different problematic area: I had a problem with this beforehand but somewhere, somehow with all the changes I did to my code it had disappeared. However, now it's back. Basically, as soon as I switch to this scene, I get a white/black variation of the scene viewed from the light's position (I can tell due to the angle one of the walls is at). What I think is happening, is that the first renderSceneElements() renders onto the frame buffer as well (not just the depth buffer) and when I call it the second time, the first rendered image (due to projection settings) wins the depth test and the second image is rendered "underneath" it. When I'm moving around with the camera through the scene, I can still see the rest of the scene but the first render is stuck on top of it.

Now, I tried using glColorMask(false...) just before the first render and glColorMask(true...) just after the render but now instead of white shapes from the first render I get black shapes (or whatever glClearColor() is set to).

Here's my full code:

init:


void Scene_Shadowmap::init() {
lightPos_[0] = 25.0f;
lightPos_[1] = 2.0f;
lightPos_[2] = 10.0f;
lightPos_[3] = 1.0f;
 
lightFOV_ = 45.0f;
lightAspect_ = 1.0f;
lightNear_ = 1.0f;
lightFar_ = 8.0f;
 
glGenTextures(1, &shadowmap_);
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
}
render:

void Scene_Shadowmap::render() {
GLenum error = glGetError();
 
glClearColor(0.5, 0.2, 0.1, 1.0f);
// position the light
glLightfv(GL_LIGHT0, GL_POSITION, lightPos_);
 
// set up the projection parameters from the light's POV
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
 
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// translate to the light's position
gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
 
glColorMask(false, false, false, false);
// render the scene to get the depth information
renderSceneElements();
glColorMask(true, true, true, true);
glPopMatrix();
 
// end the projection modification
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
 
// copy over the depth information
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512);
 
// matrix defining the planes for S, Q, R, T components for texture generation
float planeMatrix[16];
glPushMatrix();
glLoadIdentity();
// compensate for the eye-coordinate to texture coordinate conversion: [-1,1] to [0,1]
glTranslatef(0.5f, 0.5f, 0.0f);
glScalef(0.5f, 0.5f, 1.0f);
 
// do the perspective projection and translate to the light's position
gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
 
glGetFloatv(GL_MODELVIEW_MATRIX, planeMatrix);
glPopMatrix();
 
// go from OpenGL's column-major to row-major matrix form
transposeMatrix16(planeMatrix);
 
// set up the texture parameters for the shadowmap
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
 
// set up the type for texture generation
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
 
// data for texture generation
glTexGenfv(GL_S, GL_OBJECT_PLANE, &planeMatrix[0]);
glTexGenfv(GL_T, GL_OBJECT_PLANE, &planeMatrix[4]);
glTexGenfv(GL_Q, GL_OBJECT_PLANE, &planeMatrix[8]);
glTexGenfv(GL_R, GL_OBJECT_PLANE, &planeMatrix[12]);
 
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_Q);
glEnable(GL_TEXTURE_GEN_R);
 
glEnable(GL_TEXTURE_2D);
 
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
 
renderSceneElements();
 
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
 
/*
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
glTranslatef(3.0f, 2.0f, 5.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
 
glTexCoord2f(1.0f, 0.0f);
glVertex3f(3.0f, 0.0f, 0.0f);
 
glTexCoord2f(1.0f, 1.0f);
glVertex3f(3.0f, 3.0f, 0.0f);
 
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 3.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
*/
 
glDisable(GL_TEXTURE_2D);
 
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_S);
}
The scene renders fine when I disable the call to gluPerspective() before the first render but that's because the gluPerspective() matches the one used for rendering the scene afterwards (why aren't the push/pop matrix functions isolating that call?).
The renderSceneElements() renders just a bunch of VAOs where I have disabled all of the texturing/lighting calls so only the vertices/normals states are enabled.
Visual representation: (on the left call to the first gluPerspective() is commented out, on the right everything is the same as posted above)
9uqi45.png
If I move out of the "room":
213oubb.jpg
Hope you guys can help me out.
Thanks in advance!

Okay, so I decided to use FBOs instead since that problem seems to have been indeed me rendering onto the back framebuffer immediately. And having switched to FBOs I can actually see the shadowmap when I map it onto a quad (it's faint but it's visible at least). However, my problems don't end there: now the entire scene is black (except for the textured quad and brownish glClearColor() defined background). I'm guessing my texture coordinate generation is wrong but not sure. Any help would be greatly appreciated!

new init:


glGenTextures(1, &shadowmap_);
glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
 
//glGenRenderbuffers(1, &renderbuffer_);
//glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_);
//glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 512, 512);
 
glGenFramebuffers(1, &framebuffer_);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer_);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowmap_, 0);

new render:


glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
//glDrawBuffer(GL_NONE);
//glReadBuffer(GL_NONE);
 
glClearColor(0.5, 0.2, 0.1, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// position the light
glLightfv(GL_LIGHT0, GL_POSITION, lightPos_);
 
// set up the projection parameters from the light's POV
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
 
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// translate to the light's position
gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
 
// render the scene to get the depth information
renderSceneElements();
glPopMatrix();
 
// end the projection modification
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
 
glBindFramebuffer(GL_FRAMEBUFFER, 0);
 
// copy over the depth information
//glBindTexture(GL_TEXTURE_2D, shadowmap_);
//glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512);
 
// matrix defining the planes for S, Q, R, T components for texture generation
float planeMatrix[16];
glPushMatrix();
glLoadIdentity();
// compensate for the eye-coordinate to texture coordinate conversion: [-1,1] to [0,1]
glTranslatef(0.5f, 0.5f, 0.0f);
glScalef(0.5f, 0.5f, 1.0f);
 
// do the perspective projection and translate to the light's position
gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
 
glGetFloatv(GL_MODELVIEW_MATRIX, planeMatrix);
glPopMatrix();
 
// go from OpenGL's column-major to row-major matrix form
transposeMatrix16(planeMatrix); 
 
// set up the type for texture generation
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
 
// data for texture generation
glTexGenfv(GL_S, GL_OBJECT_PLANE, &planeMatrix[0]);
glTexGenfv(GL_T, GL_OBJECT_PLANE, &planeMatrix[4]);
glTexGenfv(GL_R, GL_OBJECT_PLANE, &planeMatrix[8]);
glTexGenfv(GL_Q, GL_OBJECT_PLANE, &planeMatrix[12]);
 
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);
 
 
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, shadowmap_);
 
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
 
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
 
renderSceneElements();
 
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
 
glDisable(GL_TEXTURE_2D);
 
glDisable(GL_TEXTURE_GEN_Q);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_S);
 
glPushMatrix();
glEnable(GL_TEXTURE_2D);
//glBindTexture(GL_TEXTURE_2D, shadowmap_);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTranslatef(3.0f, 2.0f, 5.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
 
glTexCoord2f(1.0f, 0.0f);
glVertex3f(3.0f, 0.0f, 0.0f);
 
glTexCoord2f(1.0f, 1.0f);
glVertex3f(3.0f, 3.0f, 0.0f);
 
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 3.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
Note that I had to explicitly bind the texture - I thought it would be automatically related to the framebuffer and hence enabling texturing would have caused that texture to be used? If I don't have that binding there, OpenGL selects my previously used texture.
Thanks in advance!
EDIT: After searching on Google for the millionth time, I ran into someone's post stating that glTexGen works only with specific objects, ie OpenGL wouldn't generate proper coordinates for quads/cubes. Now I knew that before but for whatever reason hadn't considered it until now. The OpenGL Red Book doesn't clarify it either and the given code there is really awkward. I've matched most of my code with the stuff in the book but I'm not really sure about the glTexGen stuff... If I have the shadowmap then how do I map it onto the scene?
EDIT2: Found someone on the official OpenGL forums who helped me wrap my head around this. Nothing more frustrating than running into a forum post and finding someone had a similar problem and had fixed it and not posted the solution so here you go: http://www.opengl.org/discussion_boards/showthread.php/182327-Reading-the-depth-buffer-into-texture-memory

i can provide you a working code but without usage of framebuffers. intersted?

This topic is closed to new replies.

Advertisement