Results 1 to 6 of 6

Thread: Finding clicked point in openGL

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    17

    Finding clicked point in openGL

    I have working code which finds a clicked point with the depth buffer enabled. However, how can I do the same thing with the depth buffer disabled?

    Works:
    Code :
    glEnable(GL_DEPTH_TEST);
    ...
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;
     
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);
     
    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
     
    glReadPixels(x, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
     
    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
    At this point, posX, posY, and posZ are valid.

    The above code DOES NOT work if I do glDisable(GL_DEPTH_TEST);

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: Finding clicked point in openGL

    from glDisable:

    GL_DEPTH_TEST
    If enabled, do depth comparisons and update the depth buffer. Note that even if the depth buffer exists and the depth mask is non-zero, the depth buffer is not updated if the depth test is disabled. See glDepthFunc and glDepthRange.

  3. #3
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Finding clicked point in openGL

    Don't know as I have never tried turning off depth test. Is there some reason why it can't be left turned on even if just for the duration of the function call?

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    17

    Re: Finding clicked point in openGL

    I turned it on then off again for just this function call, with no luck. The reason I have it turned off is because I'm trying to do some simple 2D plane drawing for some debugging code which will eventually be 3D. Right now, I just need to test basics.

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    17

    Re: Finding clicked point in openGL

    So what I'm doing at this point is just using the GL_ALWAYS depth function to prevent the tearing I had before, and leaving the depth buffer enabled. Sorry it took me a while to figure this one out...openGL is still a black box to me.

  6. #6
    Member Contributor
    Join Date
    Sep 2009
    Posts
    55

    Re: Finding clicked point in openGL

    If you are using a perspective projection matrix, the depth is required to do the unprojection. If you cleared the depth buffer, and did all your rendering with GL_DEPTH_TEST disabled, then the read-back depth will be at the far plane. This will goof up the unproject calculation, since your objects are presumably not actually located at the far plane.

    If you are using an orthographic projection, then the depth value doesn't matter at all -- you would get the same unprojected screen value regardless of the depth value, because things don't get smaller in the distance in orthographic view.

    But with a perspective view, things in the distance look smaller, even though they don't change size in "world" coordinates. So a small delta in screen pixels is a big "world" distance at a large depth, but that same pixel distance is a small "world" distance if depth is small (the object is close, and looks big, but it isn't really). The unproject uses the depth value to tell the difference. If you were sampling depth at the far plane, the calcualted unprojected "world points" would be further apart than they should be.

Similar Threads

  1. Finding if a point is in front or behind a plane
    By overTaker in forum OpenGL: General
    Replies: 4
    Last Post: 03-05-2014, 09:28 AM
  2. rotate about the mouse clicked point
    By thinhare in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-14-2009, 05:35 AM
  3. Finding a point (x,y) between two other points.
    By cutting_crew in forum OpenGL: General
    Replies: 7
    Last Post: 08-25-2008, 02:22 PM
  4. finding pixel value from point given in 3D
    By bob367 in forum OpenGL: Windows
    Replies: 1
    Last Post: 09-07-2004, 02:44 AM
  5. finding a point on a square
    By thasmin in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-25-2001, 01:01 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