views:

61

answers:

2

I'm putting together a simple game for the iPhone, and am trying to implement the effect of moving the camera around the GLView.

I'm drawing about a hundred objects using glDrawArrays with vertex and color pointers. After this, I want to move the camera to the right by 1 unit. This is the snippet of code I have in my drawView method. I change the matrix mode to the projection stack, and then change back to model view mode after the project manipulation is complete (I may be getting this wrong, I am a newbie to OpenGL).

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glTranslatef(1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);

In any case, the result is definitely not expected. What happens is that I see my objects very briefly (for perhaps a frame), and then they disappear. The same thing happens if I take away the glTranslatef in the block above.

What am I doing wrong?

Thanks in advance!

Before

alt text

After

alt text

+2  A: 

You probably want to read http://www.opengl.org/resources/faq/technical/viewing.htm (especially section 8.080).

Use something along the following lines:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// setup your projection matrix here, e.g. with glFrustum or gluPerspective
glFrustum(...)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...) // with your gluLookAt parameters, of course
glTranslatef(1.0, 0.0, 0.0);
// your drawing code here

Just for clarification: gluLookAt creates a matrix which is meant to be multiplied with the modelview matrix. If you use the order of function calls suggested above, things should work as expected. I hope that helps.

Greg S
+4  A: 

One of the confusing aspects of OpenGL for beginners is the distinction between what happens in the projection matrix versus what happens on the modelview matrix (and why would you even need both of them?)

The projection matrix is in charge of transforming the coordinates of your vertices to points in a 2D coordinate system (it splats the world onto your virtual film - the viewport). The projection matrix only specifies the behavior of your camera (for example: should it be a wide-angle lens, or telephoto, or a completely orthogonal one, like architectural oblique drawings?).

The modelview matrix, on the other hand, is in charge of specifying where in 3D space your vertices go to. So for example, to specify where a character's arm is with respect to the character's body, or where this character is with respect to the world, you will want to change the modelview matrix. It is important to notice, in particular, that changes in the position and orientation of the camera belong on the modelview matrix (it is the "view" part of it)

The reason it gets confusing is that at the end of the day, the vertices you give OpenGL are multiplied by the modelview matrix and then by the projection matrix. That is, given a modelview matrix M, a projection matrix P, and a vertex v, the final coordinate of the vertex is given by P*M*v. This means that some transformations seem to work regardless of which matrix you use. You should be careful about this - when you get to fancier OpenGL techniques, you will run into situations in which using the correct matrices makes a difference.

Until you get to that point, however, let me give you a good rule of thumb. Until you get used to the distinction between the two matrices, only use glOrtho or glFrustum on projection matrices (gluPerspective and their friends are ok too). All other calls (glTranslate, glScale, glRotate, etc) belong to the class of things you should be doing to the modelview matrix.

Carlos Scheidegger
I really like this answer because it gives a holistic explanation of what's going on behind the scenes here. Any books you would recommend?
Dan Loewenherz
Chapter 3, "Viewing", of the OpenGL Programming guide ("The Red Book") has a discussion on the transformations that happen on the OpenGL pipeline.
Carlos Scheidegger