Modern Opengl (4.x) unproject

Ok… so my old code used GLU unproject to transform mouse coords into world coords.

Now that i have converted to OpenGL 4.x i have run into an issue.

The param used to call unproject include the viewport matrix. I used to get this by asking opengl to return it to me.
Now it seems i will have to create the matrix my self before calling the function.

anyone ever found any documentation on how to convert your width / height into a view port matrix?

thanks
j.

w/2 0 w/2+x

0 h/2 h/2+y

0 0 1

the inner 2x2 matrix handles the scaling, the 3rd column handles the translation. after everything is projected its almost like having a large 2D plane. this matrix just moves along this plane and scales how big everything is (rather than scaling how big your view port is as is common in openGL, it moves the world around the camera not the camera itself).

Anyway. enjoy

gluUnproject() doesn’t take the viewport matrix (there isn’t actually any such thing as far as OpenGL is concerned), it takes the viewport parameters. ie the stuff you pass in glViewport(): x, y, width, height.

First, the viewport transformation can be written as a matrix so there is actually such a thing. Second, he is using modern opengl and is probably trying to avoid glu calls since these calls tend to reduce freedom and control over the program. Ive never used the gluUnproject but if it doesn’t take in the projection matrix as a parameter, then it probably assumes you are using the fixed function pipeline which he is not. All he is doing is multiplying the point on the window by the inverse of the viewport and the inverse of the projection. gluUnproject does the same thing in the background it just hides the matrix math from you.

Firstly I meant that “as far as OpenGL is concerned” there is no such thing. Ie OpenGl doesn’t use a matrix. It could depending on the driver implementation but it would just be slower to use a matrix for such a consistent operation. It is always: times by a, add b. Otherwise your viewport matrix is quite right.

And jmguillemette said he is still using the gluUnproject() function he just needed a way to get the viewport.

Ah ok your right, looked up the gluUnproject method and what he was asking made a lot more sense.