PDA

View Full Version : combining frustum and ortho views



reakin
06-17-2009, 12:22 PM
Hi,

I have been developing a set of tools for visualizing sound models in OpenGL, right now in python. I have a system that works loosely as follows: frames are added to a main class, which all do basic drawing commands. Within the frames lay objects for drawing. Each (including the frame) have a draw() function that will make opengl calls. At the beginning of each frame, a glViewport() call is made with the dimensions for that frame, thereafter normalizing the dimensions within the frame for other objects.

The problem I now need to overcome is how to allow for some frames to draw using frustrum view and others in ortho (or perspective I suppose). Before, I was calling the following in the main resize method:

glFrustum (0.0, 1.0, 0.0, 1.0, 1., 100.0)
gluLookAt (0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)

But, this makes everything frustum and nothing can be in 3d. I would like the frames to all be drawn in 2 dimensions, but I would like their contents to be optionally 3d.

So, I tried adding the following to the beginning of the draw() method of the frames:

glViewport(self.x, self.y, self.width, self.height)
glLoadIdentity()
glMatrixMode(GL_PROJECTION)
if self.Ortho:
glOrtho (0.0, 1.0, 0.0, 1.0, 1., 100.0)
else:
glFrustum (0.0, 1.0, 0.0, 1.0, 1., 100.0)
glMatrixMode(GL_MODELVIEW)

But, then I can see nothing in the frame, whether self.Ortho is true or false.

Can anyone tell me if I am on the right track? I am indeed quite new to opengl programming, so it is difficult to know why I am not seeing anything.

regards,
Rich

strattonbrazil
06-17-2009, 02:11 PM
Hey Rich,

ortho is the opposite of perspective. In perspective, things get smaller when they are drawn farther from the camera. With ortho, they are not.

If you look at your code, you have GL_MODELVIEW set as the last matrix mode. When you come back around it will still be in that mode and you'll load the identity into it, but you never clear the GL_PROJECTION matrix, which means you're multiplying the glOrtho matrix or glFrustum matrix against itself again and again.

glLoadIdentity() # clears the last matrix, looks to be modelview
glMatrixMode(GL_PROJECTION) # switch to projection
if self.Ortho:
glOrtho(0.0, 1.0, 0.0, 1.0, 1., 100.0) # multiply the projection matrix by this new matrix. If you haven't cleared it, it will probably give you an unusable matrix
else:
glFrustum (0.0, 1.0, 0.0, 1.0, 1., 100.0) # same issue here
glMatrixMode(GL_MODELVIEW) # switch to modelview

What you might want to do to make it a little cleaner...

glViewport(self.x, self.y, self.width, self.height)
glMatrixMode(GL_PROJECTION) # switch to projection
glLoadIdentity() # clear out the projection
if self.Ortho:
... set a projection
else:
... set a projection

All the matrix functions like glFrustum multiply against the current matrix that is there from last time. If you multiply two of these together, you get results you don't expect. Like glFrustum will project the projection over and over and if you see it slow enough, you'll see your scene fly off into the distance. :)
glMatrixMode(GL_MODELVIEW) # switch to modelview
glLoadIdentity() # clear out the modelview
... do your camera pointing stuff here