View Full Version : coordinates
I'm modelling a room -in which I'd like to be able to obtain the exact position that I click with the mouse in that room. How do I go about doing this?Any code?
If you mean the exact position in world coordinates, you can do it like this.
Save the screen coordinates (in pixels) when you click (as winx and winy). Then read the depth buffer to get the depth value (this will be winz) at that point (with glReadPixels). Then use gluUnProject to obtain the world coordinates.
proto of gluUnProject:
int gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz, const GLdouble modelMatrix, const GLdouble projMatrix, const GLint viewport, GLdouble *objx, GLdouble *objy, GLdouble *objz);
Map the specified window coordinates (winx, winy, winz) into object coordinates, using transformations defined by a modelview matrix (modelMatrix), projection matrix (projMatrix), and viewport (viewport). The resulting object coordinates are returned in objx, objy, and objz. The function returns GL_TRUE, indicating success, or GL_FALSE, indicating failure (such as an noninvertible matrix). This operation does not attempt to clip the coordinates to the viewport or eliminate depth values that fall outside of glDepthRange().
Taken from http://helios.scripps.edu/cgi-bin/infosr.../OpenGL_PG/6635 (http://helios.scripps.edu/cgi-bin/infosrch.cgi?cmd=getdoc&coll=0650&db=bks&fname=/SGI_Developer/OpenGL_PG/6635)
void WhenIClick(int button, int state, int winx, int winy)
// Check here if it's the event you waited for, i.e. the right button and state.
GLdouble objx, objy, objz;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winz);
gluUnproject((GLdouble)winx, (GLdouble)winy, winz, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);
This is an example of a callback function that you can use with glut. Register it like this:
When you click, objx, objy and objz will hold the world coordinates of the point you clicked.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.