To project a point, we can use gluProject function. What the function does is to project a 3D coordinate to the window screen.
GLfloat x,y,z;
double wx,wy,wz,pixel_x,pixel_y;
double modelMatrix[16];
double projMatrix[16];
GLint viewport[4];
// To find the projection
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
glGetIntegerv(GL_VIEWPORT,viewport);
gluProject(x, y, z, modelMatrix, projMatrix, viewport, &wx, &wy, &wz);
If we want to compare the screen coordinate of the point with the location of the mouse, a little bit extra processing is needed before comparing with event->x()/y(), which are mouse locations.
pixel_x = wx;
pixel_y = viewport[1] + viewport[3] - wy;
dist = (pixel_x - event->x()) * (pixel_x - event->x()) + (pixel_y - event->y()) * (pixel_y - event->y());
No comments:
Post a Comment