OpenGL Observation axis

Source: Internet
Author: User

The rotation matrix can be constructed by observing vectors, which can be two or three points in 3D space. If an object at Point P1 faces point P2, the observed vector is the P2-P1, for example:

First, the forward axis vector is simply calculated using the normalized observation vector.

Secondly, the Left axis is calculated by specifying the difference between the vector in the upward direction and the forward axis. The upward vector is used to determine the roll angle of an object. And she does not have to be vertical to the front axis. If we do not consider the roll rotation of an object, we can use (0, 1, 0 ). This means that the object is always standing up.

In fact, the vector of the upper axis that is orthogonal to the front axis and the Left axis can be calculated from the difference multiplication between the former axis and the Left axis. In order to have a vector of unit length, the Left axis and the upper axis need to be normalized after the difference multiplication.

The following is the c ++ code for calculating the Left axis, the upper axis, and the front axis from the observation vector. The first code block is the simplest implementation of the vector3 struct variable. The second code block calculates three axes from two points (position and target vector. The last code block calculates the three axes from three points (Position, target vector, and up vector.

// The simplest implementation of vector3 struct vector3 {float X; float y; float Z; vector3 (): x (0), y (0), Z (0) {}; // constructor vector3 (float X, float y, float Z): x (x), y (Y), Z (z ){}; // function vector3 & normalize (); // vector3 operator-(const vector3 & RHs) const; // subtract vector3 operator * (const vector3 & RHs) const; // difference multiplication vector3 & operator * = (const float scale); // scaling and updating}; vector3 & vector3: normalize () {float invlength = 1/sqrtf (x * x + y * Y + z * z); x * = invlength; y * = invlength; z * = invlength; return * This;} vector3 vector3: Operator-(const vector3 & RHs) const {return vector3 (x-rhs.x, y-rhs.y, z-rhs.z);} vector3 vector3 :: cross (const vector3 & RHs) const {return vector3 (y * RHS. z-z * RHS. y, z * RHS. x-x * RHS. z, x * RHS. y-y * RHS. x );}

 

//////////////////////////////////////// //////////////////////////////////////// /Calculate the transformation axis from the object location and target point ////////////////////////////// //////////////////////////////////////// //////// void lookattoaxes (const vector3 & position, const vector3 & target, vector3 & left, vector3 & up, vector3 & forward) {// calculate the forward vector forward = target-position; forward. normalize (); // calculate the On-Demand Vector Based on the pre-vector. // note that the up/down angle is 90 °. // For example: the forward vector is on the Y axis if (FABS (forward. x) <Epsilon & FABS (forward. z) <epsilon) {// forward vector pointing to + Y axis if (forward. y> 0) up = vector3 (0, 0,-1); // The forward vector points to the-Y axis else up = vector3 (0, 0, 1 );} // normally, the vector is the erect else {up = vector3 (0, 1, 0);} // calculate the left vector left = up. cross (forward); // cross product left. normalize (); // recalculate the orthogonal upper vector up = forward. cross (left); // difference multiplication up. normalize ();}

 

//////////////////////////////////////// //////////////////////////////////////// /Calculate the transformation axis from the position, target, and upward direction ///////////////////////////// //////////////////////////////////////// //////// void lookattoaxes (const vector3 & Pos, const vector3 & target, const vector3 & updir, vector3 & left, vector3 & up, vector3 & forward) {// calculate the forward vector forward = target-Pos; forward. normalize (); // calculates the left vector left = updir. cross (forward); // difference multiplication left. normalize (); // calculate the orthogonal upper vector up = forward. cross (left); // difference multiplication up. normalize ();}

 

Http://www.songho.ca/opengl/gl_lookattoaxes.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.