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

Thread: From Screen Coordinate to OpenGL coordinate

  1. #1
    Intern Contributor
    Join Date
    Mar 2011
    Posts
    87

    From Screen Coordinate to OpenGL coordinate

    Can some one tell me how to transform from Screen Coordinate to OpenGL coordinate?
    I want to draw a 3d line,so i obtain the mouse position in screen coordinate before i draw the line ,i have to transform the screen coordinate to the OpenGL coordinate. However , the line's position is not correct !why?

    My transformation function
    Vector3 GetOGLPos(int x, int y)
    {
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;
    Vector3 v;
    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);

    v.x=posX;
    v.y=posY;
    v.z=posZ;

    return v;
    }
    My mouse message function: i found the position tranformed into OpenGL coordinate is not correct!!!why?
    void MouseCallback(int button, int state, int x, int y)
    {
    Vector3 v;
    v.x=GetOGLPos(x,y).x;
    v.y=GetOGLPos(x,y).y;
    switch ( button )
    {
    case GLUT_LEFT_BUTTON:
    if ( state == GLUT_DOWN )
    {
    temp1.x = v.x;
    temp1.y = v.y;
    temp1.z=0;
    }
    else if ( state == GLUT_UP )
    {
    temp2.x = v.x;
    temp2.y = v.y;
    temp2.z=0;
    glutPostRedisplay();
    }
    break;
    }
    }

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: From Screen Coordinate to OpenGL coordinate

    Hi,
    Is your depth test enabled?

    Are u using orthographics projection or perspective. You should start with ortho first.
    Regards,
    Mobeen

  3. #3
    Intern Contributor
    Join Date
    Mar 2011
    Posts
    87

    Re: From Screen Coordinate to OpenGL coordinate

    i enabled depth test and use perspective. When i rotate the object in the scene ,the whole thing is not correct ! How can i get correct OpenGL coordinate from screen coordinate?

  4. #4
    Advanced Member Frequent Contributor Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    943

    Re: From Screen Coordinate to OpenGL coordinate

    Quote Originally Posted by apapaxionga
    My transformation function...
    Is it really your function?

    I've found the same function on some blog. Why don't you ask the poster?

    Well, ensure the following:
    - that you have a rendering context bound to the thread executing the particular code,
    - if you are using double buffering, that you are reading from the adequate buffer (set appropriate buffer with glReadBuffer(), I think GL_FRONT is default)

  5. #5
    Intern Contributor
    Join Date
    Mar 2011
    Posts
    87

    Re: From Screen Coordinate to OpenGL coordinate

    i found this code in GameDev.
    if i change the code like this ,it works and return the correct screen coord.Why? But when i rotate the whole scene ,i still didn't work.Why?
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ,posXf, posYf, posZf;
    Vector3 v;

    glPushMatrix();
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );
    glPopMatrix();
    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

    gluUnProject( winX, winY, 0, modelview, projection, viewport, &posX, &posY, &posZ);
    gluUnProject( winX, winY, .1, modelview, projection, viewport, &posXf, &posYf, &posZf);

    //v.x=posX;
    //v.y=posY;
    v.x=-posZf/(posZ-posZf)*(posX-posXf)+posXf;
    v.y=-posZf/(posZ-posZf)*(posY-posYf)+posYf;

  6. #6
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: From Screen Coordinate to OpenGL coordinate

    You need to call glReadPixels at the right moment.
    You need to glClear, render something and then use glReadPixels.

    If you are swapping buffers and then calling glReadPixels, then you might end up with some random values.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  7. #7
    Intern Contributor
    Join Date
    Mar 2011
    Posts
    87

    Re: From Screen Coordinate to OpenGL coordinate

    Could you please explain in detail about your fellowing code?i can not understand it very well.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  8. #8
    Intern Contributor
    Join Date
    Mar 2011
    Posts
    87

    Re: From Screen Coordinate to OpenGL coordinate

    i found x,y is correct !but z is not correct! how can i get correct z ?

  9. #9
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,714

    Re: From Screen Coordinate to OpenGL coordinate

    Could you please explain in detail about your fellowing code?
    That's his (distractingly long) signature, not a reply to your post.

Posting Permissions

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