Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: calculate Up Vector

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    25

    calculate Up Vector

    Hi,

    here is my code:

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

    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?

  2. #2
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,145
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    25
    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?

  4. #4
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,145
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    25
    so what I need to change in the code above if I want to define camera and up vector orientation?

  6. #6
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,145
    You can't determine it! You have to set it.
    Please, carefully read the following chapter, 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.

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    25
    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));
    Vector3f cameraUp = cameraRight.Cross(cameraDirection );

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •