Archived

This topic is now archived and is closed to further replies.

Conceptual question

This topic is 5533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings all, First, please tell me if I am correct about the following: In OpenGL, there is a camera coordinate system and a world coordinate system. In the beginning, the axes of the two worlds are in the same place and the camera is at 0,0 of *its* coordinate system facing down the negative z axis. By default, all calls to glTranslatef move the coordinate system of the *world*, and all calls to glRotatef rotate the axes of the *world* about whichever axes you specify. So, you almost have to reverse how you think - if you have drawn a world and you want the camera to start out at the point 8,8 in the xz plane facing the origin, you don''t actually move the *camera* to 8,8 - you rotate the *world''s* axes and then *translate* the *world* to -8 and -8. It took me forever to finally figure this out - I was always trying to move the *camera* and I couldn''t figure out why everything was so out of whack! Now my question is this - *is* the camera *always* at its own 0,0 pointing down the z axis? Is there a way in OpenGL to move the camera''s axes instead of the world''s? Appreciating any input, Joe

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
From the
OpenGL Programming Guide
The Official Guide to
Learning OpenGL, Version 1.1


Using the gluLookAt() Utility Routine
Often, programmers construct a scene around the origin or some other convenient location, then they want to look at it from an arbitrary point to get a good view of it. As its name suggests, the gluLookAt() utility routine is designed for just this purpose. It takes three sets of arguments, which specify the location of the viewpoint, define a reference point toward which the camera is aimed, and indicate which direction is up. Choose the viewpoint to yield the desired view of the scene. The reference point is typically somewhere in the middle of the scene. (If you''ve built your scene at the origin, the reference point is probably the origin.) It might be a little trickier to specify the correct up-vector. Again, if you''ve built some real-world scene at or around the origin and if you''ve been taking the positive y-axis to point upward, then that''s your up-vector for gluLookAt(). However, if you''re designing a flight simulator, up is the direction perpendicular to the plane''s wings, from the plane toward the sky when the plane is right-side up on the ground.

The gluLookAt() routine is particularly useful when you want to pan across a landscape, for instance. With a viewing volume that''s symmetric in both x and y, the (eyex, eyey, eyez) point specified is always in the center of the image on the screen, so you can use a series of commands to move this point slightly, thereby panning across the scene.

void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz);
Defines a viewing matrix and multiplies it to the right of the current matrix. The desired viewpoint is specified by eyex, eyey, and eyez. The centerx, centery, and centerz arguments specify any point along the desired line of sight, but typically they''re some point in the center of the scene being looked at. The upx, upy, and upz arguments indicate which direction is up (that is, the direction from the bottom to the top of the viewing volume).

Share this post


Link to post
Share on other sites
the way the openGL ''camera'' works is pretty simple... it''s just there are two matricies... the projection and modelview.. Projection, after setup, imo, should not be touched, it''s the modelview that sets the camera...

To understand how and why a matrix works will let you understand how to exploit it...
simply, a matrix is 16 numbers.
9 are used for rotation, 4 for translation, and the rest are effectivly useless (although technically they arn''t).


When you multiply a (x,y,z) vector through a matrix, what will happen, is the 9 rotation values in the matrix scale how each of the 3 x y and z values are distributed through the resulting x,y,z values..

eg:

say, we have 3 values that we will fill, call them x1,y1, and z1.. these represent the results of the matrix multiplication.

now, given the x,y,z you input (eg, a vertex), the first 3 rotation values in the matrix will work on x...
so,
say these values are m1,m2,m3 (matrix values)
then:

x1 += x*m1
y1 += x*m2
z1 += x*m3

The same happens for the next three values in the matrix (which do scale the input y value), the next 3 scale the input values of z...

This is where an identity matrix comes about,
an identity matrix looks like:

1 0 0
0 1 0
0 0 1

this means, if you have x,y,z as input values, you will get x,y,z as your result.. This is what glLoadIdentity() does.

Finally, consider the _4_ values for translation in the matrix,
simply put, these values are added to the result vector, but multiplied by the 4th (the w coordinate - this value is almost always 1).

the final identity matrix looks like:

1 0 0 *
0 1 0 *
0 0 1 *
0 0 0 1

(the values with the * can be ignored)

This is a complete identity matrix, and will see the values you input (verticies) come out exactly the same.

translate the matrix, to say, 3,7,5, and you will get:

1 0 0 *
0 1 0 *
0 0 1 *
3 7 5 1

and then, an input vector of 0,0,0 will result in 3,7,5... which is why you say that to move the ''camera'' to, say, 10,10,10, you need to translate to -10,-10,-10 since that way, a vertex at 10,10,10 will have -10,-10,-10 added, and therefor, be in the same position as the ''camera''...

A final word,

have a play around with glRotate, glTranslate, glScale, etc and glLoadMatrix and see what it does to the values in the matrix.

Think about it:

if the matrix is:

0 0 1 *
0 1 0 *
1 0 0 *
0 0 0 1

then an input value of x,y,z will result in an output of z,y,x (by using combinations of cos/sin values, this is how you can rotate a point)


Also remember that when a vector is multiplied through a matrix, the translation part is added after rotation, which is why you get the (hard to understand at first) order of calls to glRotate, translate, etc.

Share this post


Link to post
Share on other sites