Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: hidden surface removal

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    hidden surface removal

    I've having trouble implementing hidden surface removal. Here's the setup. I draw two polygons. Let's call them A and B. When I enable depth test (and clear the depth buffer), then A always gets drawn in front of B (even when I move the camera to behind B). When I disable depth test, B always gets drawn in front of A.

    How do I make it so that the farthest polygon from the camera is the one that gets occluded? The tutorials I've looked at merely say to enable depth testing. But it seems there's more to it than that.

    Thanks.

  2. #2
    Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    466

    Re: hidden surface removal

    Nothing more to do.

    If you use glut, ensure to create your window with GLUT_DEPTH attribute.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    Re: hidden surface removal

    Thanks art. But if there's nothing more to do, then why would it always draw triangle A in front (even when it's behind B)?

    It's evidence that enabling depth test does something since it makes A always in front instead of B. But then is there a way to make it so that A is only in front when it's closer?

    I'm not using glut. (Do I have to to solve this? If so, what command(s) do I use exactly?)

  4. #4
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    352

    Re: hidden surface removal

    Arts is right, you need a buffer with the ZBuffer, enable depth test and set the depth function and clear the depth buffer at the beginning, also check glDepthMask this tell if the polygon are writing on the zBuffer

    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);
    these are the default.. but just to be sure.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    Re: hidden surface removal

    BTW: here's my code (which is in a step function):

    GL.Enable(GLFeature.DepthTest);

    GL.Clear(Buffer.COLOR_BUFFER_BIT);

    GL.Begin(DrawMode.Triangles);

    GL.Color(1, 0, 0, 1);

    GL.Vertex(0, 0, 10);
    GL.Vertex(100, 0, 10);
    GL.Vertex(100, 100, 10);

    GL.Color(0, 0, 1, 1);

    GL.Vertex(100, 100, 20);
    GL.Vertex(100, 0, 20);
    GL.Vertex(0, 0, 20);
    GL.End();

    It draws two triangles--one red and the other blue. The red one is always the one on top (no matter where I position the camera). If I instead disable depth test, then the blue one is always on top.

  6. #6
    Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    466

    Re: hidden surface removal

    If depth test is disabled, it's normal that it's always the blue triangle on top since you draw it after the red one.

    I'm sure you have to ensure you create your window with depth buffer enabled (I don't know how to do it for java or so), something like gl attributes for your window.

  7. #7
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    386

    Re: hidden surface removal

    Not sure if this is your problem, but you should also be clearing the depth buffer bit with your GL.Clear command.
    Am I doing your homework for you?

  8. #8
    Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    466

    Re: hidden surface removal

    Nice point MaxH, haven't seen it...

  9. #9
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    Re: hidden surface removal

    I use the Clear command. I'll investigate how to create the window with depth buffering enabled...

  10. #10
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    Re: hidden surface removal

    I added:

    GL.Clear(Buffer.DEPTH_BUFFER_BIT);
    GL.DepthMask(GLFeature.TRUE);
    GL.DepthFunc(GLFeature.LESS);

    And I made sure that I created my window with depth buffer enabled.

    And guess what? It still doesn't work. A reference to code that successfully implements basic hidden surface would be helpful...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •