# 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?

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

– Tom

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;
GLfloat modelMatrix;
GLfloat projMatrix;

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-&gt;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”

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

– Tom

Thx - It works =D