PDA

View Full Version : Getting object coordinates on mouse click



davidb
12-18-2000, 06:15 AM
Hey,

I am displaying a building model and would like to know the actual object coordinates of a mouse click on the building. For testing purposes, I am displaying a cube. The dimensions of the cube are -1 to 1 on all sides. When I click on the edge of the cube, I would expect values close to -1 or 1. But the values come back adjusted by zoom factor, etc. How can I strip all these adjustments off and get the results I am expecting?

I am currently using this code (grabbed it from another message here)

if (HitTest(point.x, point.y))
{
GLdouble winz= 0.0;
GLdouble objx = 0.0, objy = 0.0, objz = 0.0;
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projMatrix[16];

CRect rc;
GetClientRect(rc);
int winx = point.x;
int winy = rc.Height() - point.y;

glReadPixels(winx, winy, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &winz);

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

gluUnProject((GLdouble)winx, (GLdouble)winy, winz, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);
}

The cube is just for testing purposes. I have models for bunkers and other buildings, and would like to get the actual coordinates of the mouse click, as an algorithm we have uses X, Y and Z coordinates. Are the coordinates always going to come back just in relation to the current rotation, etc? I would like to know if I clicked on the back of the building, or top, or side. I have used the select buffer for other models, but we need more specific information here.

Thanks,

Dave

zed
12-18-2000, 11:41 AM
what u get back from gluUnproject() are the coords in world space.
it looks like u want the coords in object space.
how to get those ummm? i guess u've gotta mult the coords by some matrix sorry dont know which one perhaps some kind soul will know more

Schlogenburg
12-18-2000, 11:59 AM
Will this code he has submitted work in the case of a real time strategy game, where say the user clicks on terrain it will return the world coordinate that they clicked on?

However maybe he wants some different result, but would it work for my purposes?

davidb
12-18-2000, 01:23 PM
Yes, Zed, it's object space I'm shooting for. Sounds like I'll have to grab the OpenGl books tonight and try and figure out if anything in the matrices will help.

Thanks...