0

I feel like I understand the usage of the modelview matrix and gluLookAt. That is, by declaring gluLookAt(ex,ey,ez,lx,ly,lz,ux,uy,uz), one can easily set up a way to look at a certain point directly.

My question is - since gluLookAt postmultiplies the current matrix, assuming you load the identity prior to calling gluLookAt, and the current matrixmode is modelview, what happens if you draw a cube?

So something like this :

glMatrixMode(GL_PERSPECTIVE);
glLoadIdentity();
glFrustum(-5, 5, -5, 5, 15, 150);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 16, 0, 0, 0, 0, 1, 0);
glutSolidCube(1.0);

Where is the cube rendered in world coordinates? For some reason, in this case, it's rendered at the world origin, and you can see it in the viewport - but why? Shouldn't gluLookAt have modified the modelview matrix with the view rotation and translation? Should't the cube be rendered essentially with the camera inside?

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
geogaddi
  • 565
  • 1
  • 8
  • 18

1 Answers1

2

glutSolidCube(1.0) draws a cube with side-length 1.0 which is centered at the world origin.

so the vertices are:

  • (-0.5, -0.5, -0.5)
  • (-0.5, +0.5, -0.5)
  • (-0.5, +0.5, +0.5)
  • (-0.5, -0.5, +0.5)
  • (+0.5, -0.5, -0.5)
  • (+0.5, +0.5, -0.5)
  • (+0.5, +0.5, +0.5)
  • (+0.5, -0.5, +0.5)

now gluLookAt(0, 0, 16, 0, 0, 0, 0, 1, 0) generates a modeView matrix that transforms these coordinates into the camera coordinates.

Since the camera position is just translated by (0,0,16) wrt the default camera position (0,0,0), this modelView matrix basically translates each vertex by (0,0,-16).

Clear?

D-rk
  • 5,513
  • 1
  • 37
  • 55
  • can I interpret this as, from the world coordinate system, gluLookAt translates the world origin with respect to the camera then? – geogaddi Mar 08 '13 at 14:27
  • in your case yes. But in general it can be rotation+translation. so gluLookAt creates a ModelViewMatrix which transforms (rotates+translates) from model coordinates (which is basically the world-coordinates i.e. origin (0,0,0) viewing dir (0,0,-1)) to view coordinates (the coordinate system as seen from the camera/eye i.e. origin at cam-position, viewing direction of camera) – D-rk Mar 08 '13 at 15:30
  • Excellent! Tricky stuff, but I think I get it now. – geogaddi Mar 08 '13 at 18:24
  • @Dirk, then if i rotate the object by calling rotate(90,0,1,0),whats the final coordinate? is the rotation radius 16? – suitianshi Feb 16 '14 at 12:46
  • glRotate will rotate the scene by the given angle and axis and around the origin(0,0,0). Without the gluLookAt the Cube is centered at origin. in this case you would rotate around the cube's center also but in general you don't. – D-rk Feb 17 '14 at 09:12