Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » OpenGL, glFrustum and Perspective

This thread is locked; no one can reply to it. rss feed Print
OpenGL, glFrustum and Perspective
Albin Engström
Member #8,110
December 2006
avatar

well, I'm using openGL now and I'm trying to get used to it (again) but some things are troubling me, first of all what do i have to do to not have to call glFrustrum after every time i call glLoadIdentity? if i don't the objects untill next call to glFrustrum wont draw :(. another problem is that I'm trying to get the perspective right, and right in this case means no perspective at all(i hope I'm thinking of the right thing), i want objects that are far away to be drawn exactly as large as the objects near the camera, but i cant fix it.. glut had some perspective fixing function if i recall correctly, how would i do this with "pure" openGL functions?

thanks a lot!

Tobias Dammers
Member #2,604
August 2002
avatar

First of all, glFrustum goes into the PROJECTION matrix, and unless you need to overlay something, you set that once (or once per frame) and then switch to the MODELVIEW matrix, which is where you translate, rotate and scale your world and objects. They're explained in the OpenGL red book and blue book (look for glMatrixMode).
Then; if you don't want perspective projection, then don't call glFrustum, butt use glOrtho instead. glOrtho basically keeps x and y coordinates intact, and uses z coordinates for depth buffering (and nothing else). This is in fact an isometric projection, and if you ignore depth buffering, you have in fact a 2D API. glOrtho, too, is described in redbook & bluebook.
There's one caveat to glFrustum and glOrtho: they only work properly if you first glLoadIdentity into the projection matrix, and then call either function respectively - otherwise the (unknown / undefined) former contents of the projection matrix are multiplied by the frustum or ortho projection matrix, and trust me, that is not what you want.
Third; read up on glPushMatrix() and glPopMatrix(); they allow you to save the current matrix status and restore it later. Very useful, and the proper way of doing things. With a simple scene, you'll need to call glLoadIdentity no more than once per frame.

So, in short, rendering a frame goes like so:
1) switch into projection matrix mode
2) load identity
3) glFrustum or glOrtho
4) switch into modelview matrix mode
5) load identity
6) apply camera transformation(s)
7) for each object, push matrix, apply model transforms, render, pop matrix
8) flip

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Quote:

This is in fact an isometric projection

It's orthogonal projection, not isometric one. Isometric projection happens only when projections of unit X, Y and Z vectors are equally long and angles between them are exactly 120 degrees.

Albin Engström
Member #8,110
December 2006
avatar

Thanks a lot! :D, things look MUCH better now.

Carrus85
Member #2,633
August 2002
avatar

Quote:

So, in short, rendering a frame goes like so:
1) switch into projection matrix mode
2) load identity
3) glFrustum or glOrtho
4) switch into modelview matrix mode
5) load identity
6) apply camera transformation(s)
7) for each object, push matrix, apply model transforms, render, pop matrix
8) flip

Just as a note you should be able to do steps 1-3 only once on the first frame; you don't have to do it again and again every frame, as long as you leave the projection matrix alone. (AFAIK; this has always worked for me, but it might be driver dependant :-/)

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Quote:

Just as a note you should be able to do steps 1-3 only once on the first frame; you don't have to do it again and again every frame, as long as you leave the projection matrix alone. (AFAIK; this has always worked for me, but it might be driver dependant :-/)

It doesn't depend on driver or at least shouldn't if driver wants to comply with OpenGL specification. You can even remove step 2 as all matrices are identity matrices by default and you can do step 4 just once. :)

Albin Engström
Member #8,110
December 2006
avatar

noted :).

Go to: