PDA

View Full Version : A few beginner questions?

MelonTime
09-24-2010, 10:14 PM
Hey folks. I've been coding in OpenGL for a few days using python (and a library, pygame, to handle input and drawing), and I'm struggling to wrap my head around it.

1. My first question is about glLoadIdentity(). My current understanding is this: There is an origin, (0,0,0), that the program starts at. If you use glTranslate or glRotate then you move the current location; say, if you translate (0,1,0), then the new current location is 1 tile above the origin. Now, if you draw a vertex at location (0,0,0), it will draw relative to this new coordinate. Then if you use glLoadIdentity(), the current coordinate moves back to the origin again; so if you draw another vertex, it will be relative to (0,0,0). Is this correct?

2. I'm a bit confused as to when I should use it. Should I use it at the start of the frame, and have every object draw relative to it? Should every single object's render function use it to translate to the center, translate by its location and then draw?

4. Also, if anybody is feeling generous, could you write me some really quick pseudocode for a program that draws a plane in a fixed location, allows the camera to move about and draws a cube in front of the camera.
My current code looks like this:

rotate and move camera based on input
glLoadIdentity()
rotate the camera on the x plane
prot.render(camera.position) # prot is the cube in front of the camera
rotate the camera on the y plane
plane.render() # Just a random plane so that I know the cube is moving relative to stationary objects.

Any help? I've browsed tutorials but they aren't so specific so please don't just link me to the red book or anything.

ViolentHamster
09-25-2010, 10:24 AM
Note that all the matrix calls glLoadIdentity, glTranslate, etc. have all been deprecated in the most recent OpenGL versions. A modern OpenGL program would compute all the matrix math on the CPU with a matrix library. You'd make OpenGL calls to upload the matrices right before drawing...

1. Yes. That's the idea using deprecated OpenGL calls.
2. That's how applications used to draw things. There's a lot of overhead communicating with the driver and the GPU though. Modern professional applications strive to reduce the number of state changes, reduce the number of draw calls, and reduce bandwidth to the GPU so the GPU can do things as efficiently as possible. A modern game would pack a huge amount of terrain in a Vertex Buffer Object and make just a few draw calls to render all the terrain. Static objects that are repeated in the scene and dynamic objects would probably use geometry instancing.

So, if you want the best performance, you don't want to go crazy with your loadIdentity calls.
4. That's a lot of online examples of that. And it sounds a little like homework... ;)

I hope that helps.

RefleX
09-26-2010, 10:11 AM
If you looking for classes to manage your matrix maths there are allot of open source engines that have these classes that you can learn from, and rewrite into your engine.