Results 1 to 8 of 8

Thread: Modifying the content of the depth buffer

  1. #1
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    9

    Modifying the content of the depth buffer

    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

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    Quote Originally Posted by CodcULaval View Post
    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 to set the origin of the destination rectangle.

  3. #3
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    9
    Quote Originally Posted by GClements View Post
    Use glWindowPos 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.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    Quote Originally Posted by CodcULaval View Post
    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.

  5. #5
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    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.

  6. #6
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    9
    Quote Originally Posted by GClements View Post
    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.



    Quote Originally Posted by mhagain View Post
    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.

  7. #7
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    Quote Originally Posted by CodcULaval View Post
    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:
    Code :
    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.

  8. #8
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    9
    Quote Originally Posted by mhagain View Post
    That's where the scissor test comes in - glClear obeys the current scissor test options, so for your initial example:
    You can use:
    Code :
    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

Similar Threads

  1. Modifying buffer after calling glVertexAttribPointer()
    By gcs128 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-13-2016, 06:14 PM
  2. Content of a depth texture
    By sdrouin in forum OpenGL: GLSL
    Replies: 1
    Last Post: 06-15-2012, 07:58 PM
  3. Getting content of color buffer?
    By taytay in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-15-2009, 11:48 AM
  4. Modifying Depth Buffer Values
    By Whilibarj in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 09-09-2004, 04:56 PM
  5. read depth buffer content.
    By A027298 in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 10-15-2002, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean