PDA

View Full Version : Modifying the content of the depth buffer



CodcULaval
05-26-2015, 12:26 PM
Hello,

I was wondering if there was any way to manually modify the content of the depth buffer. I'd like to be able to manually set the depth value of specific pixels. I know there's glReadPixels() to read values of the depth buffer but I haven't found any functions to write to the depth buffer. I've also read about glDrawPixels(), but there doesn't seem to be a way to specify the starting point of the pixels to draw. ie: drawing over a 4x4 pixel square from pixel (1000,800) to (1004,804).

Thanks

GClements
05-26-2015, 01:42 PM
I've also read about glDrawPixels(), but there doesn't seem to be a way to specify the starting point of the pixels to draw. ie: drawing over a 4x4 pixel square from pixel (1000,800) to (1004,804).

Use glWindowPos (https://www.opengl.org/sdk/docs/man2/xhtml/glWindowPos.xml) to set the origin of the destination rectangle.

CodcULaval
05-27-2015, 10:13 AM
Use glWindowPos (https://www.opengl.org/sdk/docs/man2/xhtml/glWindowPos.xml) to set the origin of the destination rectangle.

Cheers, that works like a charm. Unfortunately, using glDrawPixels also kills my FPS when I have to draw over a large surface.

Looks like I'm going back to the drawing board.

GClements
05-27-2015, 02:15 PM
Unfortunately, using glDrawPixels also kills my FPS when I have to draw over a large surface.

Looks like I'm going back to the drawing board.

Other options include using a framebuffer object with a depth texture and replacing portions with glTexSubImage2D(), and using a fragment shader which writes to gl_FragDepth.

mhagain
05-27-2015, 04:03 PM
Try first calling glClearDepth to select the value to clear it to.

Then enable scissor test and set a scissor rect to the area to be cleared.

Finally glClear (GL_DEPTH_BUFFER_BIT), and when done disable scissor test and restore your original glClearDepth value.

CodcULaval
05-28-2015, 06:42 AM
Other options include using a framebuffer object with a depth texture and replacing portions with glTexSubImage2D(), and using a fragment shader which writes to gl_FragDepth.

I've tried using textures but I'm still getting subpar results for what I want to accomplish. Using glDrawPixels was actually faster than using glTexSubImage2D.:dejection:




Try first calling glClearDepth to select the value to clear it to.

Then enable scissor test and set a scissor rect to the area to be cleared.

Finally glClear (GL_DEPTH_BUFFER_BIT), and when done disable scissor test and restore your original glClearDepth value.


I can't afford to clear the depth buffer as it may already contain important information when my task is executed and the drawing can occur over the whole viewport area and not just small patches. For the same reasons, I'm not too sure of how scissoring could be useful here.

Cheers to both of you.

mhagain
05-28-2015, 07:37 AM
I can't afford to clear the depth buffer as it may already contain important information when my task is executed and the drawing can occur over the whole viewport area and not just small patches.

That's where the scissor test comes in - glClear obeys the current scissor test options, so for your initial example:
drawing over a 4x4 pixel square from pixel (1000,800) to (1004,804)
You can use:
glEnable (GL_SCISSOR_TEST);
glScissor (1000, 800, 4, 4);
glClearDepth (value_to_set_depth_buffer_to);
glClear (GL_DEPTH_BUFFER_BIT);
glClearDepth (original);
glDisable (GL_SCISSOR_TEST);
And the small pixel square will be all that is cleared.

CodcULaval
05-28-2015, 07:44 AM
That's where the scissor test comes in - glClear obeys the current scissor test options, so for your initial example:
You can use:
glEnable (GL_SCISSOR_TEST);
glScissor (1000, 800, 4, 4);
glClearDepth (value_to_set_depth_buffer_to);
glClear (GL_DEPTH_BUFFER_BIT);
glClearDepth (original);
glDisable (GL_SCISSOR_TEST);
And the small pixel square will be all that is cleared.

Ah okay, I understand now. I will have to test this method to see if it's any faster than drawing.

Cheers