views:

322

answers:

2

Hi all,

I have a direction vector that applied to a position gives me the point at which the camera should look. How can I get from that yaw, pitch and roll in order to use glRotatef properly?

Thanks in advance

+5  A: 

You probably don't actually want the yaw, pitch and roll. You just need the correct transformation. Try using gluLookAt to build it. Documentation.

Thank you for the hint. I just started using OpenGL and I didn't know about this function. I solved using this.
Jack
+1  A: 

You cannot get yaw, pitch and roll from a direction vector as the direction vector will only tell which direction to look in (yaw and pitch)

To get the yaw and pitch you use trigonometry - I assume you have some working knowledge. Check out this wiki page for some useful diagrams to visualize the angles.

Letting Y = yaw, P = pitch.

First to get yaw you want:

tan(Y) = x/(-y)

Now to get pitch:

tan(P) = sqrt(x^2 + y^2)/z

To get the actual values for Y and P you'll need to use inverse tan, I've written it above using tan to make the derivation clearer.

Note that the minus signs depend on how you define you angles and axes, but you should get the idea.

You can then set roll to be 0 or whatever you like.

pheelicks
But in practice there is usually an 'up' direction in any application and you can almost always choose an orientation that keeps 'up' as close to upwards as possible.
Thanks a lot for the reply. Yes you are right, the roll cannot be determined only by a vector I need an up vector (as user207442 suggests) or a scalar that determine the angle.I solved my problem using gluLookAt but I'm still curious about how to get these angles.
Jack