I'm building a game in openframeworks where a camera moves over a stationary 2d plane in 3d space. I need to pick 5 coordinates for every frame as the camera is always moving (1 for the mouse and 4 for the corners of the viewport to workout what to draw). However I've found the gluUnproject function too slow. As I'm only picking coordinates on a stationary plane, currently on Z = 0, I figure I should be able to work out my coordinates pretty cheaply by using the modelview and projection matrices from my camera class but I just can't work out how to do the math.
To summarise, I have
Camera - MODELVIEW and PROJECTION matrices, VIEWPORT Plane aligned to z-axis with dimensions world_dims.x, world_dims.y
I want to transform screen coordinates into unprojected coordinates on the plane without using gluUnproject.
Incase I'm being dumb with my gluUnproject heres the code for that bit
ofVec3f ofxGrabCam::pickCoordinate(ofVec2f t_mouseP) {
//read z value from depth buffer at mouse coords
ofVec3f t_mouseW(t_mouseP.x, t_mouseP.y, 0);
glReadPixels(t_mouseW.x, ofGetScreenHeight()-1-t_mouseP.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &t_mouseW.z);
if (t_mouseW.z == 1.0f){
return ofVec3f(-99,-99,-99); //represents point not found
}
GLdouble c[3];
gluUnProject(t_mouseW.x, ofGetScreenHeight()-1-t_mouseW.y, t_mouseW.z, matM, matP, viewport, c, c+1, c+2);
ofVec3f mouseW(c[0],c[1],c[2]);
return mouseW;
}