Results 1 to 2 of 2

Thread: Getting "wrong" Mouse Coordinates

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2002

    Getting "wrong" Mouse Coordinates

    i want to be able to "Drag and Drop" a Sphere im my App.
    So, I get the Mouse-Coordinates with :

    void GetMouseCoord(int PosX, int PosY) {

    GLdouble PosZ ;
    GLdouble objx, objy, objz;
    GLint viewport[4];
    GLdouble modelMatrix[16];
    GLdouble projMatrix[16];

    glReadPixels(PosX, PosY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &PosZ);

    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);

    gluUnProject((GLdouble)PosX, (GLdouble)PosY, PosZ, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);

    Move_Obj_X = (objx) ;
    Move_Obj_Y = -(objy) ;
    Move_Obj_Z = (objz) ;

    This work's fine, but the Cordiantes are somekind of "wrong".

    When i move the Sphere to the Move_Obj_X,
    Move_Obj_Y and Move_Obj_Z _ Coordiantes,
    this point is far away from the mouse.

    When i zoomed out the Scene (changing the Z-Value of the Camera), evrything is messed up, too
    I want to have my Object exactly, where the Mouse-Cursor is....

    Can Anyone help me ???

    PS : Please, no "Read The Red Book" - Answers

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2001

    Re: Getting "wrong" Mouse Coordinates

    Hi !

    First of all, you have switched the mouse Y coordinate first I guess ?, OpenGL has it's origin in it's lower left corner by default.

    Now, if you are in perspecitive mode it's not easy for OpenGL to transform a 2D point into a 3D point, OpenGL does not now the depth value you are after, so you might get a Z value that is far from what you expected, I don't now a good way to solve it though.


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. Wrong shadow map coordinates
    By dletozeun in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-05-2008, 07:40 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