Results 1 to 2 of 2

Thread: Window coordinates - simple question

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    28

    Window coordinates - simple question

    Hi,
    If I have a pair of (x,y) window coordinates (where the mouse clicked in an opengl view) how do I find the corresponding coordinates of that click in terms of the opengl world space? I'm using gluPerspective for a 3d perspective projection matrix.

    I've looked around but I'm not sure what the official name for this is, which makes it hard to search.

    Thanks

  2. #2
    Member Contributor
    Join Date
    Jul 2004
    Location
    Toronto
    Posts
    97

    Re: Window coordinates - simple question

    You can't get 3D coordinates from 2D coordinates because the projection loses information. However if you have the depth as well then you can use:
    glReadPixels(x,y,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
    gluUnProject(x,y,depth, model_view, projection, viewport, &outX, &outY, &outZ);
    See documentation for explanation of the function.

    Typically this sort of thing is called "picking" because when user clicks somewhere inside a window, you project a ray into 3D and return the intersection with the first object. That's kind of what the code above does - the depth component will contain the z for the closest object.

Similar Threads

  1. Returning from the Window Coordinates to Object Coordinates
    By tiregarfio in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 09-19-2006, 09:31 AM
  2. converting window coordinates to world coordinates
    By pwonder in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 09-26-2004, 12:51 PM
  3. Replies: 4
    Last Post: 05-24-2004, 09:03 AM
  4. Problems mapping 2d window coordinates to 3d system coordinates
    By stefboombastic in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 01-04-2003, 01:11 AM
  5. convert window coordinates to OpenGL coordinates
    By grodslukaren in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-02-2001, 11:30 AM

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