# Problems with frustums, etc...

Hi guys,

An assignment I’m doing requires that we implement our own matrice and vector classes, etc so we can draw in software mode (using SDL, if it’s relevant), using everything in OpenGL as a base and implementing it ourselves.

I’m almost finished, but the last part of the assignment deals with frustums and has me stumped. Here’s the instructions:

6.2.3 Projection Transformation
There are two parts to this. First, we transform the polygon into the clip coordinates
http://www.songho.ca/opengl/gl_projectionmatrix.html#perspective
Once we have the frustum, we normalise so that X, Y, and Z all range from -1 to 1, which
is called the Normalised Device Coordinates (NDC).
The two steps:

1. Apply Perspective transform to each of the vertices in the triangle
2. Divide x, y, and z by the w value of the vector.

To me, this implies that we should be constructing our frustum and transforming the polygon into the clip coords every frame, which I’m sure isn’t correct, as I’m lead to believe that frustums are constructed in the projection matrix at the very beginning of your program and aren’t altered. I’m confused because we also have to construct our own perspective matrix, so the two are supposed to somehow co-exist.

So here’s the gist of what I think I’m supposed to be doing (according to the instructions)

initialisation (called once):
matrix mode to projection
glMultMatrix perspective matrix (equilavent to gluPerspective)

matrix mode to model view
glMultMatrix look at

render (called repeatedly):
matrix mode to model view
glpushmatrix

transformations (translations, rotations, etc.) on an Object

glpopmatrix

matrix mode to projection
glpushmatrix

glMultMatrix frustum (of my own creation)
normalise x, y and z of Object using frustum
map the Object’s x and y coords to the pixels (0 - width, 0 - height)

glpopmatrix

But I’m really not sure about this because as I said, I don’t think it’s good practice to change the mode to the projection matrix every time rendering occurs. I know I’m asking a lot here but I’m really unsure, so if you guys could at least tell me if I’m going wrong and where, that would be awesome.

Thanks heaps.

You’re obliged to do so. Even if in OpenGL you use Push and Pop for the matrix stack, OpenGL on its own uses the top matrix from the stack to do calculations. So, even if you don’t declare the projection matrix every frame, it is used by OpenGL.

On the other hand, there’s really a little overhead having to redeclare the projection matrix every frames (just copying the matrix on the top of the stack, which is more than very fast [16 floating point values]. Many programs do so, mainly if they have to deal with different projections (managing several views, for shadow mapping…).

Each camera in your GL application will have a perspective matrix defined - either by glFustrum or gluPerspective.

When your application become more complex and involves multiple cameras (for example shadow mapping) then you need to render the scene from different points of view. Each light could also have different properties such as FOV, etc which affect the projection matrix for that light.

Therefore you need to calculate the projection matrix for each ‘camera’ at least once and again if some property changes.

Thanks heaps for that guys! That makes a lot more sense now.