View Full Version : Very beginner questions (Camera)

07-18-2007, 04:53 AM
I am an university students studying computer science at the moment. Recently I finished a course on computer graphic so I want to start writing things up.

Right now I am implementing a camera for a first person view which should have the following options:
*Foward, backward
*Strafe left, right
*look up,down,left,right
*fly up, down

As a newbie at this stuff ofcouse I did go search how do people do this and I come across three methods:
Method 1

glTranslatef(cam.pos.x, cam.pos.y, cam.pos.z);
glRotate(cam.rot.x, cam.rot.y, cam.rot.z);Method 2

cam.pos.x + cam.dir.x,
cam.pos.y + cam.dir.y,
cam.pos.z + cam.dir.z,
cam.up.x, cam.up.y, cam.up.z
)Method 3

cam.up.x, cam.up.y, cam.up.z
)Now here is my question, which one is better (in term of efficiency), from what i found methods 3 is most common but also more complicated. everyone's implementation is different and it really give me headache to understand all of them.

please give a bright light to a newbie that want to learn :D

thanks in advance

07-18-2007, 07:54 AM
My suggestion for you is to:
1. store camera as:
-angles of rotation
2. every frame convert your camera to alternate representation:
-forward vector
-up vector
-right vector
Simply create 3 vectors: (0,0,-1), (0,1,0), (1,0,0) and rotate them by your angles.
At beginning you will only need first representation so you can add the other one later.

Representation #1 is easier to manipulete:
turn right = increase one of angles.

Representation #2 is easier to use for many algorithms:
if (dot(forwardVector, vectorToObject) > 0) { object is in front } - as you can see I didn't even have to look at angles - I only performed one dot product operation.

To put it another way - for something simple (FPP camera) you can use first representation.
For something more complex (camera placed in aircraft's cockpit) using angles will only make things more difficult.

Basically you should avoid computing physics or AI using position+rotation - use this representation only when you need to manipulate object directly (in FPP camera and all objects are manipulated directly by controller, in aircraft simulation they're based on physics). For anything more than just move, rotate and jump, always use pure vector representation.

Once you get the basics, master two operations: dot product of 2, 3 and 4-component vectors and cross product of 2 and 3-component vectors. They're the base for almost any physics, AI or rendering computations in 3D on both CPU and GPU.
They're so simple and so usefull.

07-18-2007, 08:25 AM
thanks you, i will try to work on this direction