Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: ? Clipping Planes and Depth Buffers with gluPerspective() ?

  1. #1
    Intern Contributor
    Join Date
    Jul 2008
    Posts
    99

    ? Clipping Planes and Depth Buffers with gluPerspective() ?

    I'm trying to draw a space scene where I can be very close to one object and yet see another object far away. I've been developing my application in JOGL (Java OpenGL bindings) and upto now I was using version 1.1.1, which is fairly old. That version seemed to allow me to have a very, very large ratio of near/far clipping planes and everything was working well. I was likely breaking the rule that requires the ratio to be reasonably small, but now I'm paying the price and with my update to JOGL 2.0+ I am having problems.

    Basically, the picture below is what I am trying to achieve visually. I want to be able to have a camera located on a vehicle that approaches the International Space Station, and I want to be able to see parts of the vehicle from that camera view, while also seeing the station when it is far away.



    So, currently I am unable to move too far away from the space station before it disappears, basically it falls outside of my clipping region. I tried to expand the clipping region to the same values I used under version 1.1.1 and it was broken - it wouldn't render either when it got too far away, although under the old version is used to be fine.

    My question is NOT about JOGL versions - that's not important. I obviously need to keep those clipping plane ratio small, my question is what other options I have. I realize I could do a dual pass rendering where I render the far away scene to a texture and then project that texture, but my current setup would require too much code change to accomodate that so I was looking for another solution.

    Specifically I read the following posts that mention a possible solution

    https://www.opengl.org/discussion_boards/showthread.php/184983-glfrustum-near-and-far-values

    https://www.opengl.org/discussion_bo...-draw-distance

    I believe visually this means I am doing the following with my opengl gluPerspective() calls...ie left image is the old style where you have close near and distant far clipping planes, while the right image shows (what I imagine is being discussed above), that you have two (2) calls to gluPerspective() in the same pass with one call with one set of near/far clipping planes and the other call with a second set of clipping planes.



    Effectively, I tried this kind of code:

    Code :
        glClear(GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, ar, nearBACK, farBACK);
        glMatrixMode(GL_MODELVIEW);  
     
        DRAWBACKGROUND();
     
        glClear(GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, ar, nearFWD, farFWD);
        glMatrixMode(GL_MODELVIEW); 
     
        DRAWFOREGROUND();

    But this doesn't seem to be working for me. It seems like the second gluPerspective() is what the DRAWBACKGROUND() is using, because if I change the farFWD clipping distance to something closer than the BACKGROUND, then the BACKGROUND disappears.

    Am I doing this correctly?

    btw, why can't I upload any images...keeping getting "not a valid image error" /rant
    Last edited by Z-Knight; 03-23-2016 at 06:58 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    388
    Out of curiosity, what happens if you don't call glClear the second time?

    I would look into using stencil planes.
    Draw the foreground first using nearFWD and farFWD.
    Use the same foreground routines to write to stencil planes such that the pixels used to draw the foreground are disabled for writing.
    Then draw the space station using nearBACK and farBACK.

    This is a 3 pass approach: 1) foreground, 2) stencil, 3) background.
    Should not require major re-coding on your part.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,519
    Quote Originally Posted by Z-Knight View Post
    my question is what other options I have. I realize I could do a dual pass rendering where I render the far away scene to a texture and then project that texture, but my current setup would require too much code change to accomodate that so I was looking for another solution.
    You don't need to render to a texture. Just render the far objects followed by the near objects, with different near/far values for the projection matrix used in each pass. Either clear the depth buffer between passes or use glDepthRange() to allocate a portion of the range to each pass.

    Quote Originally Posted by Z-Knight View Post
    while the right image shows (what I imagine is being discussed above), that you have to calls to gluPerspective() in the same pass with one call with one set of near/far clipping planes and the other call with a second set of clipping planes.
    Yes, that's exactly what's being discussed.

    Quote Originally Posted by Z-Knight View Post
    But this doesn't seem to be working for me. It seems like the second gluPerspective() is what the DRAWBACKGROUND() is using, because if I change the farFWD clipping distance to something closer than the BACKGROUND, then the BACKGROUND disappears.

    Am I doing this correctly?
    Yes.

    Quote Originally Posted by Z-Knight View Post
    btw, why can't I upload any images...keeping getting "not a valid image error" /rant
    Image upload is broken. For now, you'll need to use an external hosting site.

  4. #4
    Intern Contributor
    Join Date
    Jul 2008
    Posts
    99
    ok, I think it was working after all. When the station was very far away I was drawing a large wireframe sphere around it so I could still see it in the distance...and that sphere was disappearing. It turns out I wasn't applying the same near/far clipping planes to that sphere as I was to the station itself - I was using the close in clipping distances and that was cutting it off. I'll double check it, but I think that was it. Thanks.

Tags for this Thread

Posting Permissions

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