Using modelview/projection matrix to do a projection myself


Here is what I would like to do :
1- I am rendering a scene with a camera C at position P1.
2- I would like to project some given points as if my camera C was at position P2. Project means compute the coordinates in a function but not to render the points.
3- using the result of step 2- I will move my camera C in a new position.

To do the step 2 I do this in a function :

glMatrixMode(GL_MODELVIEW);  // set the mode
glPushMatrix();              // save the current matrix
gluLookAt(P2.X, P2.Y, P2.Z, NTarget.X, NTarget.Y, NTarget.Z, m_up.X, m_up.Y, m_up.Z); // set the position P2
glGetDoublev(GL_MODELVIEW_MATRIX, ModelView);    // retrieve the matrix corresponding to P2

Is this approach (without rendering) valid to obtain the matrix corresponding to position P2 ? I.e. will the call to gluLookAt create the matrix ?

If this approach is correct I will show you what I do to compute the projection because the result is not correct…


Just so that you know, you are using deprecated Legacy OpenGL functions. Obviously, they are still supported, but I do not recommend it. You may succeed in drawing something, but sooner or later you are going to run into the situation where you can’t use the full power of modern graphics cards, our you will find that you want to do some special tweaking that is very difficult unless you program the shaders on your own.

The VIEWING transform with P2 as the eyepoint? Yes.

Most of the rest of your post talks about projections though. Different beast. VIEWING transforms map from WORLD-SPACE to EYE-SPACE. Projections map from EYE-SPACE to CLIP-SPACE.

Yes with P2. Ok, thanks.

History of the step 2- I first do an orthogonal projection of my points on a plane P. But it is not good.
I think I have to do the perspective projection of the points on the plane P.
So I was thinking : if I set the camera on position P2 so that the image plane is my projection plane P then I have to retrieve the opengl matrices and do : projected_point = projection_matrix * modelview_matrix * point
But you are right : I will obtain Clip-Space coords :sick: I am not sure that it’s what I want… It is always difficult for beginners to well understand the opengl transformations… :sick:

Do you think that my approach is bad ?
If yes I think I have to build my own pesrpective matrix…