I need to draw a scene in OpenGL that can be viewed in different projections. So far I've been using gluPerspective and glOrtho to create different projections, but now I need to do it without using these functions. Is there even a way to do this?
2 Answers
You can do it by directly setting the projection matrix. Since you used gluPerspective()
, I assume you're using the fixed-function legacy OpenGL pipeline. Is that correct? I ask because if so, you can set the matrix mode to the projection matrix (via glMatrixMode(GL_PROJECTION);
and directly load or construct a matrix with calls like glLoadMatrix()
or glTranslate()
, etc.
If you're trying to move away from the fixed-function pipeline and that's why you can't use gluPerspective()
or glOrtho()
then you need to calculate it manually and pass the resulting matrix to your shaders.
So, if you're still using the fixed function pipeline, section 9.085 of the OpenGL FAQ tells you how to set the projection matrix that gluPerspective()
calculates but using glFrustum()
:
9.085 How can I make a call to glFrustum() that matches my call to gluPerspective()?
The field of view (fov) of your glFrustum() call is:
fov*0.5 = arctan ((top-bottom)*0.5 / near)
Since bottom == -top for the symmetrical projection that gluPerspective() produces, then:
top = tan(fov*0.5) * near
bottom = -top
Note: fov must be in radians for the above formulae to work with the C math library. If you have comnputed your fov in degrees (as in the call to gluPerspective()), then calculate top as follows:
top = tan(fov*3.14159/360.0) * near
The left and right parameters are simply functions of the top, bottom, and aspect:
left = aspect * bottom
right = aspect * top
The OpenGL Reference Manual (where do I get this?) shows the matrices produced by both functions.
The man
page for glFrustum()
explains how it calculates the projection matrix from the above information. (I'd put an excerpt here, but I'm not sure how to get the matrix drawn correctly.) Likewise the man
page for glOrtho
explains how to construct an orthographic projection matrix manually.
If you're using the modern rendering pipeline, you can use the above information with glm, the OpenGL Mathematics Library. glm will help you construct the matrices you need, and you can pass the results to your shaders.

- 1
- 1

- 25,567
- 4
- 55
- 86
As user1118321 states in his answer you can create any projection matrix yourself and load it directly to OpenGL. But for newbie it can be hard to go from there so in C++ it is done like this:
double M[16]=
{
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0, // <- this line is for projections
};
glMatrixMode(GL_PROJECTION)
glLoadMatrixd(M);
- where M is your projection matrix ... change it to any you need
- beware OpenGL matrices are column oriented
- look here: transform matrix anatomy