gluLookAt (OpenGL question)

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
666
Reaction score
20
Points
33
Hello together,

I'm just trying a bit with openGL.
My goal is to create a "flying camera" which can move as a spaceship with RCS controll (internal view), but with only one fix spd (Camera is moving with 1 unit/s (with constant speed) or camera is not moving. (if no button/key is pressed)

Im using gluLoookAt (see here).
Code:
gluLookAt(
   pos.posX, pos.posY, pos.posZ,     //Position of camera
   pos.posX, pos.posY, pos.posZ + 1, //Point where the camera is looking too 
    0, 1, 0);                         //Up - Vector

With this code, it's possible to translate, but not to rotate.
My problem now is, that openGL is an absoloute world.
This means, by the standart function, I can rotate and translate the world around it 0/0/0 coordinate.

I have no Idea how to reach my goal :-(
So for the first step, I have the following questions:
1. Is gluLookAt the right way to do this?
1. how to get it rotating in the right way?
2. how to get the translation related to the rotation angle (not absolute)?
 

BLANDCorporatio

New member
Joined
May 29, 2013
Messages
67
Reaction score
0
Points
0
My problem now is, that openGL is an absoloute world.
This means, by the standart function, I can rotate and translate the world around it 0/0/0 coordinate.

There's two parts to this question. One is about how to represent the relative transformations you want, one is about how to use the openGL operations for that.

I haven't worked with openGL much at all, but the first part I can cover, and as to the second, either someone else on this forum will help you, or you will find that you will know what to look for in the openGL documentation.

So then, assume the ship is at origin. You want your camera to point away from the ship in the positive Z direction, where up is positive Y. This gives you two vectors:

v_front_ref = [0, 0, 1]
v_up_ref = [0, 1, 0]

In general the ship won't be at {0, 0, 0} and will not face in the Z direction, nor will up be in the positive Y, so you will want to rotate them, as you found out. So you need a rotation matrix.

If you know the orientation of the ship as a rotation matrix (do you?), and let that matrix be R, then it's easy:

v_front = R*v_front_ref
v_up = R*v_up

and you then need

gluLookAt(
pos.posX, pos.posY, pos.posZ,
pos.posX + v_front.posX, pos.posY + v_front.posY, pos.posZ + v_front.posZ,
v_up.posX, v_up.posY, v_up.posZ);

(There's a subtlety here with how the matrix R is defined: you'd need either it or its transpose to get v_front, v_up; that depends on your orientation representation code)

If you don't know an orientation matrix for the ship, presumably you at least still know two points on the ship that define the local "front" and "up" for it. Then it's even easier: say the two points (that define the front and up for the ship) are ship_front, ship_up with components in world frame. You'd use

gluLookAt(
pos.posX, pos.posY, pos.posZ,
ship_front.posX, ship_front.posY, ship_front.posZ,
ship_up.posX - pos.posX, ship_up.posY - pos.posY, ship_up.posZ - pos.posZ);
 

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
666
Reaction score
20
Points
33
Thank you I solved it!
 
Last edited:
Top