PDA

View Full Version : Mouse click position?



ProfEich
03-10-2003, 11:52 AM
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?

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

-- Tom

ProfEich
03-11-2003, 06: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, 06:34 AM
You can't typecast the matrices to GLdouble* like that. Instead, declare them as GLdouble[], and use glGetDoublev() to retrieve them.

-- Tom

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