Jump to content
  • Advertisement
Sign in to follow this  
Jayinmn

Render only the Depth Buffer

This topic is 3010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, Is there anyway to render a particular scene, such that the depth buffer is visualize. From a normal scene like this: to a scene like this based on its depth buffer: I wanted to show the depth buffer of a particular scene side by side with the original render. Thanks all. Image source from Wikipedia

Share this post


Link to post
Share on other sites
Advertisement
In GLSL, just do a regular tex2D texture lookup with a depth texture. Depending on your setting of GL_DEPTH_TEXTURE_MODE_ARB, this should result in some color value. Try using glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY); for your depth texture.

Getting your depth buffer to a texture can be done using an FBO with a depth attachment or, alternatively and slower, using glCopyTexSubImage2D.

Share this post


Link to post
Share on other sites
Thanks Ignifex,

I manage to try out getting the depth data from the back buffer using the glCopyTexImage2D method.

Below are the codes that I've use to do this.

//screenwidth and screenheight are the width and height of the window respectively.

GLuint i;
unsigned int* data;

glGenTextures(1, &(i));
glBindTexture(GL_TEXTURE_2D,i);

data = (unsigned int*)new GLuint[((512 * 512)* 4 * sizeof(unsigned int))];
ZeroMemory(data,((512 * 512)* 4 * sizeof(unsigned int)));
glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 512, 0,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE, data);
delete []data;

glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT , 0,0,screenwidth,screenheight, 0);

//Drawing the texture i.


Thing is, what I saw on the screen was a white 512 by 512 square, while I've a scene at the back of this. I'm guessing its because the function returns a texture with values clamp between 0 to 1? I'm not sure though.

Share this post


Link to post
Share on other sites
White quads for textures generally means the texture could not be read or the texture id is invalid. In the case of depth textures, the values might also be very close to 1 though. Try playing around with your z-near value in your projection matrix. Setting it further away will get you depth values closer to 0.

There is something about your code you might want to take a look at.
You are creating a 0 filled array of pixeldata. Instead of passing this to OpenGL, you might want to try using a null pointer. This will also internally allocate the texture data, but leaves the data undefined. Then, once you have some depth values in your buffer, you can call glCopyTexSubImage2D (note the Sub) on the whole texture to update all the values. This operation will not reallocate anything. In the end, it saves reallocation and data transfer to the GPU.

If setting your z-near to a higher value doesn't give you more grey-ish tones, then the problem is likely with your texture creation and the fact that it is a depth texture. In that case you might want to try creating a GL_INTENSITY texture instead, skip your copying of the depth, and check to see what the output is.

Share this post


Link to post
Share on other sites
glColor3f(1.0,1.0,1.0);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, m_shadowMap);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glTexCoord2f(0,1);glVertex3f(-1.0f, 1.0f, -.2);
glTexCoord2f(0,0);glVertex3f(-1.0f, -1.0f, -.2);
glTexCoord2f(1,0);glVertex3f(1.0f, -1.0f, -.2);
glTexCoord2f(1,1);glVertex3f(1.0f, 1.0f, -.2);
glEnd();

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Just use the texture parameters as such. You wont need to use a shader.

Share this post


Link to post
Share on other sites
I changed the code a little and tested whether if the texture creation was working.
When I use GL_RGB instead of GL_INTENSITY or GL_DEPTH_COMPONENT
and using the glCopyTexImage2D (without the sub), it
draws what is in the background, which works.
But when I replaced it with the one with subs. Its black.

Then I tried replacing GL_RGB with GL_DEPTH_COMPONENT in glTexImage2D
and then, still using the glCopyTexImage2D(without sub) with GL_RGB replaced with GL_DEPTH_COMPONENT.
A gray texture is observed.

Hmmm?


//screenwidth and screenheight are the width and height of the window respectively.

GLuint i;
GLubyte* data=NULL;

glGenTextures(1, &(i));
glBindTexture(GL_TEXTURE_2D,i);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY , 512, 512, 0,GL_INTENSITY , GL_UNSIGNED_BYTE, data);

//glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, screenwidth,screenheight), 0)
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0 , 0,0,screenwidth,screenheight);

//Drawing the texture i.

Share this post


Link to post
Share on other sites
Do you actually need to put these values in a texture for later use, or do you just want to display a gray-scale image fading from black to white based on distance?

Share this post


Link to post
Share on other sites
There's an easy way to do this with texture coordinate generation. OpenGL can automatically generate texture-coords from position, and if you create a 1D texture fading from black to white, and tell OpenGL to generate the texture coordinate in that texture depending on Z distance, and render your scene with only that texture bound, u get an image where color is based on distance.

Set it up something like this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// -1/100 is because the far-plane is at 100, use the actual far-plane value
GLfloat splane[4] = {0.0f, 0.0f, -1.0f/100.0f, 0.0f};
glTexGenfv(GL_S, GL_EYE_PLANE, splane);
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

glEnable(GL_TEXTURE_1D);
glBindTexture(GL_TEXTURE_1D, fadeTextureId);


// Now set up camera etc. and draw model, and it automatically will fade on distance



I can post a complete example if you want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!