Search code examples
opengl3drotationtranslationscene

OpenGL (simple) scene & object navigation basics for "lookthrough" camera


I've read through several over-complicated articles on rendering a 3D scene with OpenGL, and haven't really found something that helps me to visualize the basic concepts of scene navigation with respect to glRotate and glTranslate calls.

From experimenting with the examples provided by LWJGL (my particular OpenGL library), I understand very basically what effect comes of their use. I can use glTranslate to move to a point, glRotate to rotate about that point, and glLoadIdentity to snap back to the origin or glPopMatrix to go back to the last glPushMatrix, which are essentially snapshots of a location and rotation. Finally, the scene will render to screen with respect to the origin.

So basically, to put a cube at point A with rotation B:

  1. glTranslate(A.x,A.y,A.z) [now focused on point A]
  2. glRotate(B.,,*,*) for pitch, yaw, and roll; [now rotated to rotation B]
  3. glBegin(GL_QUADS) and glVertex3f()x4 for each 'side'(quad) relative to object's origin
  4. glEnd()
  5. glLoadIdentity() [reset to origin for other objects, not needed if only drawing the cube]

As for the "camera", since openGL's "eye" is fixed, the scene has to move inversely from the camera in order to simulate moving the eye with the camera. This is my understanding so far, and if this is wrong, please put me on the right path.

Now for my specific situation and question:

My 'scene' consists of terrain and a player (some arbitrary shape, located at a terrain-relevant location and a 'camera' head). The view should rotate with respect to the player, and the player move with respect to the terrain. I want the final rendering to essentially "look through" the player's head, or camera. My confusion is with the order of translate/rotate calls for rendering each part, and the direction of translation. Is the following the proper way to render the terrain and player with respect to the player's "camera"?

  1. translate away from the player by the player's distance from origin (move the scene)
  2. rotate away from the player's rotation (player looks 40 degrees right, so rotate scene 40 left)
  3. render terrain
  4. reset via glLoadIdentity
  5. render player's head (if needed)
  6. translate to body/hands/whatever position
  7. rotate away from the player's rotation (step 2 again)
  8. render body/hands/whatever

Also, does rotating have an effect on translation? Aka, does OpenGL translate with respect to the rotation, or does rotation have no bearing on translation?


Solution

  • I can use glTranslate to move to a point, glRotate to rotate about that point, and glLoadIdentity to snap back to the origin or glPopMatrix to go back to the last glPushMatrix, which are essentially snapshots of a location and rotation.

    No not quite. Those functions have now idea of what a scene is. All they do is manipulating matrices. Here's an excellent article about it:

    http://www.opengl.org/wiki/Viewing_and_Transformations

    One important thing to keep in mind when working with OpenGL is, that it is not a scene graph. All it does is rendering flat points, lines and triangles to the framebuffer. There's no such thing like a scene you navigate.

    So basically, to put a cube at point A with rotation B: (...)

    Yes, you got that one right.

    As for the "camera", since openGL's "eye" is fixed

    Well, OpenGL got no camera. It's only pushing vertices through the modelview matrix, does lighting calculations on them, then passes them through the projection and maps them to the viewport.