1

pseudo code :

drawScene() {
    for(every 3Dobject) {
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(eye, targ, up); //is there a better way?
        3Dobject[n].draw(); //this involves calling translations / rotations
    }
    //of course theres 2D GUI stuff to draw next

Is this the proper (or at least not terribad) way to do it?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
jason
  • 4,721
  • 8
  • 37
  • 45

2 Answers2

3

You probably shouldn't be calling glLookAt at all. You sure don't want every object drawn in the middle of the screen. Do you have a problem with the objects changing the modelview matrix and not putting it back? Use glPushMatrix and glPopMatrix instead of repeating the look-at calculation.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • "You sure don't want every object drawn in the middle of the screen" - each object holds its 3D world coordinates. In other words, while the camera always looks at targ, an object may have moved off to the left somewhere. Im very new at this and this way just seemed obvious, let me know if Im going about it wrong. thank you for the glPush / Pop functions, looks interesting. – jason Feb 03 '12 at 22:32
  • @jason: Well it wasn't obvious that `targ` isn't connected to the current object in the loop. And an implicit suggestion that it is, since otherwise you'd do it outside the loop. – Ben Voigt Feb 03 '12 at 22:48
1

No, call it once to 'set up the camera' and then draw all objects, which are then transformed accordingly, by just applying their own transformation in world space.

EDIT: As suggested by Ben Voigt, you should push and pop appropriately to retain this transform until all objects have been drawn.

Example:

gluLookAt(...);

//it is essential to track this state, since there are multiple stacks
glMatrixMode(GL_MODELVIEW); 

for(object : objects){
    glPushMatrix();
    glMultMatrix(object.transform);
    draw(object);
    glPopMatrix();
}

I also want to mention, that this functionality is deprecated in newer OpenGL versions/profiles.

Sam
  • 7,778
  • 1
  • 23
  • 49
  • Doing this in OpenGL ES 1.something (targeting android 2.2). Good example, muchas gracias! – jason Feb 03 '12 at 22:43