Results 1 to 3 of 3

Thread: Mouse Coordinates

  1. #1
    Senior Member Regular Contributor
    Join Date
    Sep 2005
    Location
    Dayton
    Posts
    148

    Mouse Coordinates

    I am creating a Qt-based application with a QGLWidget inside it. I want to use picking and know how to capture the screen coordinates of my mouse location, but I thought there might be some functionality to convert this screen coordinate to the world coordinate. Does OpenGL provide something like this or do I need to calculate it by hand everytime? Thanks!

  2. #2
    Member Newbie
    Join Date
    May 2006
    Location
    Czech republic
    Posts
    42

    Re: Mouse Coordinates

    I use this function:

    vec3 getPixelCoordAtMousePos ()
    {
    POINT mouse;
    GetCursorPos (&mouse);
    ScreenToClient (m_hwnd, &mouse);

    int viewport[4];
    double modelview[16];
    double projection[16];

    glGetDoublev (GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev (GL_PROJECTION_MATRIX, projection);
    glGetIntegerv (GL_VIEWPORT, viewport);

    float winX = (float)mouse.x;
    float winY = (float)m_height - (float)mouse.y; //(float)viewport[3] - (float)mouse.y;
    float winZ;

    glReadPixels ((int)winX, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
    double finalCoordX, finalCoordY, finalCoordZ;

    if (winZ == 1.0)
    return vec3 (0.0, 0.0, 0.0);
    else
    {
    gluUnProject (winX, winY, winZ, modelview, projection, viewport, &finalCoordX, &finalCoordY, &finalCoordZ);
    return vec3 (finalCoordX, finalCoordY, finalCoordZ);
    }
    }

  3. #3
    Senior Member Regular Contributor
    Join Date
    Sep 2005
    Location
    Dayton
    Posts
    148

    Re: Mouse Coordinates

    So gluUnProject will put the ortho-equivalents to the screen coordinates into finalCoordX, finalCoordY, and finalCoordZ? Thanks again!

Similar Threads

  1. Replies: 8
    Last Post: 01-22-2016, 04:57 AM
  2. Convert windows mouse coordinates to opengl coordinates
    By throwaway in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-05-2013, 06:44 PM
  3. Mouse coordinates into World coordinates
    By fiodis in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-30-2012, 07:24 AM
  4. mouse coordinates
    By nick800 in forum OpenGL: Basic Coding
    Replies: 11
    Last Post: 04-01-2003, 10:53 AM
  5. Converting Mouse Coordinates to 3D Coordinates
    By Infamous in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-18-2002, 08:33 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