PDA

View Full Version : Write to Z-Buffer



atulrungta
02-27-2012, 01:48 AM
Hi,

I am an OpenGL newbie and was looking at way(s) to write to the Z-Buffer. I can read the values using glReadPixel, but can I write to the buffer and bind a window to it? If yes, how ?

Thanks!

Atul

carsten neumann
02-27-2012, 09:11 AM
You write to the depth buffer by rendering primitives with glDepthMask(GL_TRUE) (the default). If you don't want to render to the color buffer(s) at the same time you can use glColorMask() to turn that off.

I don't understand what you are asking for with "... and bind a window to it?".

atulrungta
02-27-2012, 09:41 AM
Thanks for the reply carsten! I am sorry for not being clear. Let me try and explain what exactly am I trying to achieve here. I want to render 2 OpenGL windows with simple figures (e.g spheres), combine the Z-buffers of both into a single one and then render the two spheres using this new Z-buffer in new window. So, that is where that 'bind a window to it (i.e. the new, combined Z-buffer)' comes into the picture.

Hope I was clear this time!

Thanks.

Atul

mbentrup
02-27-2012, 10:06 AM
You can use glDrawPixels to write to the depth buffer if you use a compatibility profile. Otherwise you'll have to create a shader that reads the depth value from a texture and outputs to gl_fragDepth.

In your case that shader could read both depth textures and do the combining (e.g. take the minimum of both values) on the fly.

atulrungta
02-27-2012, 10:48 AM
Okay! Thanks a lot, mbentrup.


Atul

atulrungta
02-27-2012, 11:32 PM
Hi mbentrup,

I was able to read the z-buffer values of the two windows I had created. But when I use glDrawPixels to set the z buffer for the 3rd window I get some weird artifacts (pic attached). Basically, I tried something like this :


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawPixels(500, 500, GL_DEPTH_COMPONENT, GL_FLOAT, z_buffer_combined);
glutSolidSphere(1.0, 20, 16); //sphere 1
glPushMatrix();
glTranslatef (1.f, 1.f, 1.f); //translated so that they're not concentric
glutSolidSphere(0.5, 20, 16); //sphere 2
glPopMatrix();


Why is this happening?

Thanks,
Atul

tonyo_au
02-28-2012, 08:28 PM
You may be getting a precision problem. You could use multiple render buffers and collect glFragCoord.z from the first 2 windows into textures rather than reading the depth buffer. The depth buffers are typically 24bits whereas a float texture is 32bits