Hi, I have a 2D Hud (using glOrtho) over a 3D scene.
What I need is to obtain the 2D choords of an object displayed in the “3D” layer. (just the object origin: 0,0,0)
In other words, I need to know the on screen x,y coordinates of a point at x1,y1,z1 in the 3D world…
Probably going to have to get both the model view and perspective matricies from GL, and apply them both to the untransformed location of your 3d object.
Just need a simple vector * matrix function, and use glGetFloatv() calls to get the matrix data.
A potentially easier way that I have never tried is to use glFeedbackBuffer(). I beleive you can get the exact 2d coordinates of rendered objects through it.
Actually, you have a function called gluProject that will do exactly that for you… It implements what jmX described except that it also takes the viewport into account (yes, you need it !).
So, if you want to use it:
GLdouble modelMatrix[16],projMatrix[16];
GLint viewport[4];
GLdouble winx,winy,winz; // Coordinates of the projected point on the screen //
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
glGetIntegerv(GL_VIEWPORT,viewport);
Actually, you have a function called gluProject that will do exactly that for you… It implements what jmX described except that it also takes the viewport into account (yes, you need it !).
Works! Thanks a LOT!
Moreover I haven’t seen any speed drop (calculating just 5 points/frame)