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: Screen to world coordinates

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2001
    Posts
    1

    Screen to world coordinates

    I'm trying to translate an object using mouse input coordinates and I would like the object to track the mouse. I'm using a perspective viewing transformation. I've tried using gluUnproject() with glReadPixels to give me the correct depth as hinted at in the FAQ, but I can't seem to get it to work. The object motion simply does not track the mouse motion. Am I doing this all wrong? Does anyone have any pointers or code snippets?

    Thanks!

  2. #2
    Intern Contributor
    Join Date
    Jul 2001
    Location
    Czech Republic
    Posts
    79

    Re: Screen to world coordinates

    I had a similar problem. I wanted to track a cube on the screen with mouse input. It didn't work, the object was jumping on random positions. The problem was, that I was using modelview matrix already set by translations and rotations. Solution: Load identity matrix.

    some code:

    case WM_MOUSEMOVE:
    {
    GLdouble modelmatrix[16];
    GLdouble projmatrix[16];
    GLint viewport[4];
    int realY;
    GLdouble getX, getY, getZ;

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
    realY = viewport[3] - (int)HIWORD(lParam) - 1;
    gluUnProject(LOWORD(lParam), realY, -4.0f, modelmatrix, projmatrix, viewport, &getX, &getY, &getZ);

    break;
    }

    p.s.: this code is a little bit changed code from the Red Book.

    Don't know if this is what you want, just hope it helps.

  3. #3
    Intern Contributor
    Join Date
    Jul 2001
    Location
    Czech Republic
    Posts
    79

    Re: Screen to world coordinates

    P.S.:This is just for fixed depth.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    839

    Re: Screen to world coordinates

    Ja, well, of course...?

    <looks dumbfounded> if you calculate a world coordinate for a given screen cordinate, and then render that world coordinate, the current modelview matrix when the coordinate is rendered MUST be the same as the modelview matrix you pass to gluUnproject. If you just pass the identity matrix to unproject, then.. yes, you NEED to set the modelview matrix to I when its rendered...

    cheers,
    John

Posting Permissions

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