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 8 of 8

Thread: converting 2D screen coordinates to a 3D world vector

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2000
    Location
    Birmingham, UK
    Posts
    7

    converting 2D screen coordinates to a 3D world vector

    Hi,

    I am writing a 3D tic tac toe game where I want the user to be able to click on a square in the grid, the only thing is that the grid is not static ie: it can move around, rotate etc... I'm also using perspective which I think complicates things.

    Does anyone know how to convert 2D screen coords into 3D world coords at say a fixed z value and give it a vector so I can project a line through the grid to find which square it intersects first?

    cheers

    Lawrence

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,663

    Re: converting 2D screen coordinates to a 3D world vector

    normally, gluUnProject can do that, and there may be other things.

    JD

  3. #3
    Intern Contributor
    Join Date
    Jan 2002
    Location
    Stockholm, Sweden
    Posts
    74

    Re: converting 2D screen coordinates to a 3D world vector

    Yes, you can use ReadPixels to get the
    depthcomponent ( z ) & use that with
    glUnproject to get the x,y,z worldcoords.

    cheers

  4. #4
    Intern Contributor
    Join Date
    Feb 2000
    Location
    Gothenburg, Sweden
    Posts
    68

    Re: converting 2D screen coordinates to a 3D world vector

    Hello

    You can also use slightly different colors on each square and use glReadPixels to determine which square the user clicks.

    Osku

  5. #5
    Intern Newbie
    Join Date
    Feb 2002
    Posts
    31

    Re: converting 2D screen coordinates to a 3D world vector

    You could just use the selection buffer, thats what its there for...

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2000
    Location
    Birmingham, UK
    Posts
    7

    Re: converting 2D screen coordinates to a 3D world vector

    Hi,

    the problem with using read pixels is that the squares are empty, they are just bounded by cylinders, so you can see the lower levels through each square should you rotate the grid that way.

    cheers

    bleach



    [This message has been edited by bleach (edited 02-14-2002).]

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    France
    Posts
    880

    Re: converting 2D screen coordinates to a 3D world vector

    bleach,

    If I understand you correctly, you have the correct approach. Here is how to do what you are looking for:

    1) Get X,Y user input
    2) Use gluUnproject with Z=0 (gives X1,Y1,Z1)
    3) Use gluUnproject with Z=1 (gives X2,Y2,Z2)
    4) Build Vector V=P2-P1
    5) Now you have a vector that you can use to see which block has been clicked by the user.

    For what you want to do, you don't need to read Z from the z-buffer.

    Regards.

    Eric

  8. #8
    Junior Member Newbie
    Join Date
    Oct 2000
    Location
    Birmingham, UK
    Posts
    7

    Re: converting 2D screen coordinates to a 3D world vector

    Fantastic!

    thank you.


    bleach

Posting Permissions

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