PDA

View Full Version : calculate Up Vector

Doctor100
03-25-2014, 07:06 AM
Hi,

here is my code:

Vector3f CameraUpVector() const
{
Vector3f cameraDirection = m_cameraTarget - m_cameraPosition;

Vector3f cameraUp = cameraDirection.Cross(Vector3f(1.0f, 0.0f, 0.0f));
return cameraUp;
}

Vector3f cameraUp = CameraUpVector();
p.SetCamera(m_cameraPosition , m_cameraTarget , cameraUp);

Is this calculation OK?

Aleksandar
03-25-2014, 08:26 AM
No, it is not. If camera is looking down the X-axis, you'll get zero-vector.
Up vector should be the property of the camera. Or even better, use quaternions.

Doctor100
03-25-2014, 08:51 AM
i AM NEW IN opengl
PLEASE IF YOU CAN SHOW ME HOW TO CORRECT IT
because now my target is (0,0,0)
and the camrea pos is (0,500,0) and I cant see nothing
How can I use Quaternions?

Aleksandar
03-25-2014, 09:44 AM
If the camera position is (0,500,0) and the target is at (0,0,0), there is an infinite number of Up vectors that can produce valid output.
For example, if Up vector is (0,-1,0) you'll see the object at the origin like you are inclined toward -Z axis. Also, (-1,0,0) will form the same result as previous, but rotated 90 degrees to the right. Can you understand that there is an infinite number of Up vectors? So, in order to have an unique camera orientation, you need to store Up vector in an attribute of the camera and rotate it along with Direction vector (only together they define camera orientation).

Quaternions are the further abstraction of the complex numbers. They proved to be very useful in defining orientation, since they are more numerically stable and avoid some problems (like gimbal lock). It would be ideally to define a camera position/orientation as a pair (P,Q), where P is a 3D vector and defines a position, while Q is quaternion defining orientation.

Doctor100
03-25-2014, 09:52 AM
so what I need to change in the code above if I want to define camera and up vector orientation?

Aleksandar
03-25-2014, 10:02 AM
You can't determine it! You have to set it.
Please, carefully read the following chapter (http://www.glprogramming.com/red/chapter03.html), and take a look at fig.3-12.
Up vector can be whatever you set. It should be perpendicular to Direction, but it is not required.

Doctor100
03-25-2014, 02:17 PM
Ok
i CHANGED THE FUNC TO :
Vector3f CameraUpVector() const
{
Vector3f cameraDirection = m_cameraTarget - m_cameraPosition;
Vector3f cameraRight = cameraDirection.Cross(Vector3f(0.0f, 1.0f, 0.0f));

return cameraUp.Normalize();
}
now my target is (0,0,0) and the camrea pos is (0,500,0) and the up vector is (0,1,)
Is this ok? now I can see the model from an upper view?