Jump to content
  • Advertisement
Sign in to follow this  
Structural

OpenGL Creating a gray scale image of the z-buffer without readpixels

This topic is 4153 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

Hello people, I'm looking for a way to render a gray scale image of the z-buffer to a second OpenGL viewport without reading the pixels into local memory. I am familiar with doing this using readPixels/drawPixels, but as you may know this is slow as hell. I'm now reading up on glCopyPixels and glPixelTransfer, but I can not find out how to copy from the z-buffer to the color buffer using that, if that is even possible. Does anyone have experience with this?

Share this post


Link to post
Share on other sites
Advertisement
You can use FBOs. You can attach a texture that you use as the depth buffer and then render the texture on a screen aligned quad that. You can use MRT and, using a shader, write the depth on a second buffer.

Share this post


Link to post
Share on other sites
Quote:
Original post by apatriarca
You can use MRT and, using a shader, write the depth on a second buffer.
It's the case to say that even reading FragDepth is likely to change how the pipe works.
The "fixed function depth" will initialize the zbuff by default (the FBO-only way would work just ok) but reading depth to init another buff (MRT way) may give a few headaches.

Share this post


Link to post
Share on other sites
Thank you for your replies. I'll give the FBO thing a shot. I'm working with an Intel onboard video chip and I hope it supports it.

Share this post


Link to post
Share on other sites
To get back on this, the FBO thing didn't quite work out because my cheap ass DELL with Intel video chip doesn't support FBOs.
I now implemented it with glReadPixels and glDrawPixels, but as expected it's awfully slow.
I am still open for any suggestions to speed this up.

Share this post


Link to post
Share on other sites
You can read from the depth buffer with glCopyTexImage2D if your chip supports OpenGL 1.4 or greater. It should be faster than gl*Pixels, at least in theory, having to only do a VRAM->VRAM copy.

Share this post


Link to post
Share on other sites
Thank you Sharlin, this does kind of work. I can see the depth buffer on the texture as a luminance channel so it seems.

I do get the depth buffer onto the texture, however, the glPixelTransferf with the GL_DEPTH_SCALE and BIAS have no effect. The GL_RED_SCALE and others do work correctly though.

I found also that the performance hit when scaling the channels is similar or even greater than copying and writing the pixels to memory. I do not know if this is again purely the lack of features of my hardware, or it the scaling of the channels is inherently slow.

Share this post


Link to post
Share on other sites

void CopyDepthBuffer(int width , int height)
{
glBindTexture(GL_TEXTURE_2D, m_shadowMap);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
0, 0, width, height, 0);

}

void DrawDepthBuffer()
{
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);

//Draw a quad somewhere here:

//Change texture mode to GL_MODULATE, otherwise everything is black
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

Share this post


Link to post
Share on other sites
P.S. Your going to have to print screen and change the contrast in a photo tool, as neighboring pixels are almost the same color.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!