glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(width, 0); glVertex3f(width, 0, 0);
glTexCoord2f(width, height); glVertex3f(width, height, 0);
glTexCoord2f(0,height); glVertex3f(0,height, 0);
glEnd();
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x1111);
...
glDisable(GL_LINE_STIPPLE);
glm::vec3 unProject(const glm::vec3 &_loc){
GLdouble x = 0.0;
GLdouble y = 0.0;
GLdouble z = 0.0;
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
gluProject(_loc.x, _loc.y, _loc.z,
mvmatrix, projmatrix, viewport,
&x, &y, &z);
return glm::vec3(x,y,z);
}
Reference:
- http://www.opengl.org/archives/resources/faq/technical/transformations.htm
- http://forum.openframeworks.cc/t/how-to-get-screen-x-amp-y-position-of-object-on-a-3d-space/5173/2
- http://nehe.gamedev.net/article/using_gluunproject/16013/
First do this (copied from OpenGL_Programming):
glGetIntegerv( GL_VIEWPORT, viewport );
glGetDoublev( GL_MODELVIEW_MATRIX, mvmatrix );
glGetDoublev( GL_PROJECTION_MATRIX, projmatrix );
realy = viewport[3] - (GLint) y - 1;
gluUnProject( (GLdouble) x, (GLdouble) realy, 0.0, mvmatrix,
projmatrix, viewport, &wx1, &wy1, &wz1 );
gluUnProject( (GLdouble) x, (GLdouble) realy, 1.0, mvmatrix,
projmatrix, viewport, &wx2, &wy2, &wz2 );
You then have the endpoints of a line that represents all 3D places that mouse click could have been. Then, for each point, calculate the distance between this line and the point. Find the minimum, and you're done. In the case where two points tie, or are both reasonably close to zero distance from the line, then simply find which of the two are closest to (wx1, wy1, wz1), which represents the point on the hither plane (the closest one to the eye is probably the one you clicked on.)