View Full Version : simulate camera rotation
12-13-2007, 11:17 PM
I wrote a primitive 3D renderer (from scratch, no opengl etc), I can apply 4x4 matrix transformations to my models. My camera is statically located looking down the z-axis.
Is it possible to figure out what rotation matrix should be applied to the models to simulate rotating the camera around the x-axis, or any other axis for that matter?
I'm not sure how you make the camera move, since all I have right now is transforming individual objects.
12-14-2007, 05:44 AM
First you need to learn to put single transformation into matrix. If you look over the web, you should find many examples of matrices representing singla transformation.
Rotation matrix (around Z axis) would look more less like this:
cos(a) -sin(a) 0 0
sin(a) cos(a) 0 0
0 0 1 0
0 0 0 1
Since we rotate around Z, then Z axis doesn't change but X and Y axes are "exchanging" their values sinusoidally. If a=0 then sin(a) = 0 and cos(a) = 1 which means we get an identity matrix.
Making matrix containing multiple transformation is done by multiplying single-transformation matrices.
The movement you're interested in should do the folowing transformations:
1. rotate object around one axis (Y)
2. rotate object around another axis (X)
3. translate object away from camera (Z axis)
Since at beginning object and camera are at the same location, rotating only changes angle of view. Finally you move such rotated object away.
12-14-2007, 07:07 AM
I have all of that built into my Matrix3d class, so I'm set there.
What I'm wondering though is how you get the 'camera' to move about the world axes? Right now my engine (written from scratch) doesn't really have a concept of camera, it's just at (0,0,0) looking down the z-axis.
For right now I do all my matrix transformations on my objects, then move them to about (0,0,-200) so that they are in view of the camera.
Now I want to start moving the camera though, and don't really know how to begin doing that.
The other option is to simulate camera movement by transforming all objects using the 'world' as the root as a large assembly. I don't quite know how to do that given that I'd need to figure out dynamically which angles to rotate by to simulate the 'camera' moving.
12-14-2007, 07:38 AM
Maybe you can use the gluLookAt (http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html) function.
gluLookAt( GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ )
Then you can rotate about the X-axis by setting eyeY=sin(a) and eyeZ=cos(a)
12-14-2007, 09:07 AM
I definitely would, it's just that I wrote this rendering engine from scratch (no opengl or anything). I'm not quite sure mathematically how to approach this. I mean I'm rotating my models about the world axes, but then how do you do a 'camera' transformation? I don't know if these rendering engines just figure out a world transform to apply to all models to make it 'appear' as if you're really moving 'the camera', or if there really is some special class/implemenation of a camera you're supposed to come up with?
12-14-2007, 09:16 AM
The link I provided you with explains how the 4x4 matrix is built:
M is then constructed as follows:
( s s s 0 )
| u u u 0 |
M = | |
|-f -f -f 0 |
| 0 0 0 1 |
and gluLookAt is equivalent to glMultMatrixf(M);
glTranslated (-eyex, -eyey, -eyez);
Check the glspec (http://www.opengl.org/registry/doc/glspec21.20061201.pdf) chapter 2.11.2 to see how the translate/rotate/scale calls are mapped to 4x4 transformation matrices.
12-15-2007, 06:35 AM
The trick is to figure out the transform that you would use to move your camera object into the world, but instead of transforming the camera, you transform the rest of the world using the *inverse* of this transform.
12-22-2007, 01:39 PM
I was just about to put a similar post up, so I hope no one minds me asking a quick question here to prevent a similar thread.
I know you can do the camera movements/rotations with gluLookAt, OR you could simply apply all the opposite transformations to everything else. But I was wondering which is more efficient for moving the camera with mouse and keyboard input?
12-23-2007, 03:29 AM
There really is no difference in efficiency, the gluLookAt function also sets up a modelview matrix that is used to transform all other objects you're drawing. The only difference is in the way you think about the problem.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.