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: Screen Coordinates to OpenGL Coordinates

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Derry, NH
    Posts
    126

    Screen Coordinates to OpenGL Coordinates

    How can I convert a mouse point's screen coordinates to its OpenGL drawing coordinates if the drawing is a 2-D one? Thanks!

  2. #2
    Junior Member Regular Contributor
    Join Date
    Oct 2002
    Location
    King George, Virginia
    Posts
    132

    Re: Screen Coordinates to OpenGL Coordinates

    if you use an orthographic projection (using glOrtho) the mouse coordinates map directly to window coordinates.

    jebus

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Derry, NH
    Posts
    126

    Re: Screen Coordinates to OpenGL Coordinates

    Most of the time, the screen coordinates's unit is pixel. When we draw points, we need to know the positions of the points. However, in this case we do not know the unit. That is to say, if we use glVertex2f(1.0f, 1.0f), we do not know the unit of these 1.0s. How can I solve the problem? That is to say, if my mouse screen coordinates are (100, 200), how much is the 100 in OpenGL? 1.0f? 0.5f? 2.0f?....

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: Screen Coordinates to OpenGL Coordinates

    Of course we know the unit take the following ortho view.

    void glOrtho( GLdouble left,
    GLdouble right,
    GLdouble bottom,
    GLdouble top,
    GLdouble zNear,
    GLdouble zFar )

    gluOrtho2D(-10.0, 10.0, -10.0, 10.0, -1, 1)


    glViewport(0,0, 500, 500) Screen 500 x 500

    Given that:

    mouse_x = 0 = -10.0 openGL units
    mouse_y = 0 = 10.0 openGL units

    And we know the screen, we can make the following code:

    x = -10.0 - (500 / (mouse_x + 1) * 20)

    y = 10 - (500 / (mouse_y + 1) * 20)

    Will give you the current mouse position in the current openGL units.


    Originally posted by Rong Yao:
    Most of the time, the screen coordinates's unit is pixel. When we draw points, we need to know the positions of the points. However, in this case we do not know the unit. That is to say, if we use glVertex2f(1.0f, 1.0f), we do not know the unit of these 1.0s. How can I solve the problem? That is to say, if my mouse screen coordinates are (100, 200), how much is the 100 in OpenGL? 1.0f? 0.5f? 2.0f?....

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Derry, NH
    Posts
    126

    Re: Screen Coordinates to OpenGL Coordinates

    Thanks a lot, Jebus and nexusone!

  6. #6
    Junior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Derry, NH
    Posts
    126

    Re: Screen Coordinates to OpenGL Coordinates

    nexusone,

    Do you mean 500 pixels = 20 OpenGL units in your case? If so, could it be

    x = -10.0 + mouse_x/500 * 20
    y = 10 - mouse_y/500 * 20

    ?

    Why do you use mouse_x + 1 in your formula?

    Thanks! Please reply.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: Screen Coordinates to OpenGL Coordinates

    I am a bit brian dead from the flu.

    Sorry you are correct on the math, could be one other error on x or y in which should get the + or - 10.





    Originally posted by Rong Yao:
    nexusone,

    Do you mean 500 pixels = 20 OpenGL units in your case? If so, could it be

    x = -10.0 + mouse_x/500 * 20
    y = 10 - mouse_y/500 * 20

    ?

    Why do you use mouse_x + 1 in your formula?

    Thanks! Please reply.

  8. #8
    Junior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Derry, NH
    Posts
    126

    Re: Screen Coordinates to OpenGL Coordinates

    nexusone,

    Thank you for your reply. Sorry to hear you get flu. I was little bit confused just now.
    The reason I post the last message is just to clarify my confusion and make sure I understand your method. Nothing else. Thanks again for your help and wish you recover from the flu soon!

Posting Permissions

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