View Full Version : Mouse click position?

03-10-2003, 10:52 AM

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?

Tom Nuydens
03-10-2003, 11:35 PM
Read the Z-buffer value at (X, Y), then use gluUnproject() with (X, Y, Z).

-- Tom

03-11-2003, 05:02 AM
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"

Tom Nuydens
03-11-2003, 05:34 AM
You can't typecast the matrices to GLdouble* like that. Instead, declare them as GLdouble[], and use glGetDoublev() to retrieve them.

-- Tom

03-11-2003, 06:21 AM
Thx - It works =D