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

Thread: Mouse click position?

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2003
    Posts
    17

    Mouse click position?

    Hi!

    I've got a 3D scene with a heighmap.
    The camera shows the heightmap from the top like a ISO-perspective. It's free turnable and you can zoom a little bit in.

    If the user clicks on a position on the screen I like to calculate the X and Y position on the heightmap where the user clicked on - How is that possible?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Oct 2000
    Location
    Belgium
    Posts
    807

    Re: Mouse click position?

    Read the Z-buffer value at (X, Y), then use gluUnproject() with (X, Y, Z).

    -- Tom

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2003
    Posts
    17

    Re: Mouse click position?

    Well, here's my code:

    GLdouble winz;
    glReadPixels(Cursor.X, Cursor.Y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winz);
    GLdouble objx, objy, objz;
    GLint viewport[4];
    GLfloat modelMatrix[16];
    GLfloat projMatrix[16];

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

    gluUnProject((GLdouble)Cursor.X, (GLdouble)Cursor.Y, winz, (GLdouble *)modelMatrix, (GLdouble *)projMatrix, viewport, &objx, &objy, &objz);

    GLdouble tx = objx; //Just to test
    float x = tx;
    float y = objy / 10000.0;
    float z = objz / 10000.0;

    SetWindowText(g_window->hWnd, (FloatToStr(float(x)) + " - " + FloatToStr(float(y)) + " - " + FloatToStr(z)).c_str());


    but if I run it and try it there comes an error in line "GLdouble tx = objx"

    German error: "GleitkommaŁberlauf"

    English should be something like "OVERFLOW"

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Oct 2000
    Location
    Belgium
    Posts
    807

    Re: Mouse click position?

    You can't typecast the matrices to GLdouble* like that. Instead, declare them as GLdouble[], and use glGetDoublev() to retrieve them.

    -- Tom

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2003
    Posts
    17

    Re: Mouse click position?

    Thx - It works =D

Posting Permissions

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