Camera Transformation Matrix

Source: Internet
Author: User

Http://blog168.chinaunix.net/space.php? Uid = 20221192 & Do = Blog & id = 278899

Some netizens wrote to me hoping to understand the specific derivation process from the world space to the camera space transformation matrix in the fixed assembly line. In fact, I have explained how to use the camera transform as an example of using the basic theory in vector ry 6, but it may not be specific enough. In this article, I will try my best to explain the entire ins and outs of camera transformation. Hope to be helpful to friends who are learning a fixed pipeline. Here we will introduce several theoretical knowledge before deriving the camera transform to better understand the derivation process. Let's get started!

What is camera transformation?

In a pipeline, when an object is transformed from the model coordinate to the world space through the world matrix, it will be transformed from the world space to the camera space through the camera transformation. In the fixed assembly line, the part in the blue box is in this process.

 

 

 

 

 

In fact, the so-called camera space is a reference system that uses the camera as the coordinate origin. Therefore, the transformation from the world space to the camera space is to take the object from the world coordinate system, shows the camera coordinate system that is transformed to the origin of the camera.

 

 

The left half is the position of the villain in the world space, and the right half is the position of the villain after being transformed to the camera space. Such a transformation can be implemented in many ways: Euler's camera system, uvn system, two points & a twist, and so on. Here we will discuss the most extensive uvn system to build a camera matrix. If you are interested in other methods, you can find relevant materials. We still discuss the derivation of OpenGL's camera matrix. Other APIs can be similar.

Coordinate Conversion Formula

We mentioned orthogonal matrix in vector RY's use in game programming 6, which is a concept based on the basic theory (if you are not very familiar with the basic theory, see Use of vector ry in game programming 6). All column (ROW) vectors of the orthogonal matrix constituteStandard orthogonal basis(Its column vectors are orthogonal to each other and the length is 1). Therefore, we can regard the orthogonal matrix as a description of a coordinate system. At the same time, we know that:The coordinates of the same vector under different bases are different.. Therefore, orthogonal matrices can be used to represent the coordinate system (or as the basis), so as to write the coordinates of the same vector in different bases under the unified reference system (global coordinate system.

 

 

The formula above indicates that the coordinate of vector V in base Q in the reference system is V ', the coordinates in the base R are v'' (note that the base matrix is represented by a column vector in the environment, and the result after multiplication represents a linear combination of the base vector ). For example, black base indicates the reference system, red indicates the base Q, blue indicates the base R, and V indicates a vector in the reference system.

To make it clearer, let me give you an example:

The preceding formula indicates the vector V in the reference system, in the base Q (1 0 0), (0 1 0), (0 0 1) the coordinates are (1 2 6), the coordinates under the base R (0 1 0), (0 0 1), and (1 0 0) are (2 6 1 ). Note that all the relations between the base and the vector we discuss are linear, and there is no displacement relationship. Therefore, we use 3D vectors to represent them, instead of the homogeneous representation of 4d (if the alignment sub-coordinates are not very familiar, please refer to the homogeneous coordinate section in deep exploration perspective projection transformation ).

In this way, we know the coordinates of one base Q and vector V in it, and the other base R, you can use the V = qv' = RV ''formula to calculate v ''.

 

 

The above is the formula for finding v''. It is noted that the inverse matrix R ^-1 of base R needs to be calculated on the right side, because base R is an orthogonal matrix, an important property of the orthogonal matrix is that the inverse is transpose. Therefore, we can write it

This formula is the coordinate conversion formula. In particular, if Q is the same coordinate system as the reference system (in most cases in 3D programming), such as the World coordinate system, Q is a unit matrix I, then we can write it

The coordinate conversion formula can be interpreted as follows: For the vector V' in the world coordinate system, its coordinate in the coordinate system R is v ''. We will use this formula later.

In addition to the orthogonal matrix, we can also useCollinearity(Colinear amount) to describe the Coordinate Transformation (André Lamothe tricks of the 3D game programming gurus). This theory is based on the geometric meaning of dot product: the linearity of one vector on another vector. For example, two vectors V and S dot product

 

Ry means the product of the projection length of V in the S direction, or the product of the projection length of S in the V direction and the length of V (The product symbol is: if the angle of V and S is less than 90 degrees, the product is positive. If it is a right angle, the product is zero, otherwise it is negative ).

Furthermore, if V is a unit vector, the dot product can be interpreted as the projection length of S in the V direction. If S is a unit vector, it can be interpreted as the projection length of V in the S direction. Now, we extend the dot product to the base layer, and perform the dot product for one vector V and three units of the base R, the three values obtained by the dot product represent the coordinate v'' of the vector under this base''

Mathematical expression:

Note that in order to allow V to perform dot product on each of the Base's axial quantities, we must write the base as a transpose form, that is, row vector multiplication. Otherwise, it will become a linear combination. The significance of this formula is that the vector V' In The World Space and the axial volume of R are accumulated to obtain the collinearity of V 'under R-coordinate v ''. This formula is exactly the same as the coordinate conversion formula we obtained above. In fact, we explain the same formula in two different directions. I hope you can understand both directions.

Uvn System

The uvn system itself is a base. As shown in, the three base vectors u, v, N point to the right, top, and rear of the camera respectively to form the right-hand coordinate system, and the camera is at the coordinate origin.

 

With the uvn system, you can easily set the camera orientation. Its construction process is as follows:

In the reference system (here is the world coordinate system), we can specify the camera location-eye, the location of the observed villain-lookat, and an auxiliary vector-The up vector representing the "TOP" in the reference system, which affects the generation of U and V, because the V vector obtained in the future will be on the plane determined by the up and n vectors (if you are interested, you can prove it yourself), you can use this vector to make the camera produce different deflection.

First, we obtain the vector n.

It is easy to subtract the camera position from the target location, that is, step 2 in the figure. Step 2: Calculate the vector u. This step requires the help vector up. If you do not want the camera to produce deflection, generally take (0, 1, 0)

U is implemented using the cross multiplication of vectors, Which is step 3 in the figure. Finally, use N and u to calculate the vector v

Finally, the calculated u, v, and n are unitized to obtain the uvn System of the camera. Based on the coordinate transformation theory we mentioned above, we can regard the uvn system as the basis of the camera, so that we can easily convert a vector in the world coordinates and camera coordinates.

The glulookat (eyex, eyey, Eyez, lookatx, lookaty, lookatz, UPX, upy, and upz) method of OpenGL is used to set the camera matrix. The first three parameters are the camera's position vector. The three parameters in the middle are the observed target position vector, and the last three parameters are the auxiliary vector up.

Derivation of camera Matrix

We have already explained the uvn system. The uvn system is used in the standard assembly line to describe the camera. U, V, and N correspond to the three base vectors in the camera coordinate system.

In addition, for a camera, it is coincident with the world coordinate system at the beginning. After the user controls the camera to move in the world space, the camera status can be described using two attributes: Orientation and position. That is to say, with these two attributes, the state of a camera model in the world is determined. The two attributes are described by the theory of transformation, that is, rotating and moving. As you can imagine, for any camera in the world, we can regard it as: the camera first rotates a certain angle around its own base point, and then translates to a certain place in the world space. Demonstrate this process

 

 

In the figure, red is the base of the camera, while black is the base of the world, that is, the reference system. A villain is an object in the world. Before the camera moves, the two bases overlap. When the camera locates on the screen, it first determines the orientation-rotation, and then determines the position-translation. The rotation and translation steps in the figure are the transformations that occur when the camera locates. You can see the movement of the camera relative to the villain. When the camera is changed, the villain should transform from the world base to the camera base. In this way, he should carry out a reverse positioning of the camera. First, he should reverse the translation of the camera and the camera, and then reverse the rotation of the camera and the camera, and finally the camera is back, the villain as the camera changes to the camera space. This is caused by inverse translation and Inverse
Rotation is completed in two steps, namely camera transformation. Now let's deduce this transformation. We write down the relationship, and the transformation C of the camera itself includes two elements.

T indicates the translation transformation, and r indicates the Rotation Transformation. Camera transformation is the inverse transformation of the camera itself.

This C ^-1 is the camera transformation we require. T ^-1 is easy to find, that is

R ^-1 is not so easy to find. Therefore, we don't want it. We use the uvn system. What does it mean? Please refer to the figure of the camera transformation above. After the camera transformation completes the inverse translation step, the camera's origin and the world's origin will overlap, that is, the translation transformation is completed. What we need to do next is reverse rotation.The purpose of the inverse rotation is to obtain the coordinates of the villain in the current world's coordinate system after the inverse translation.. Are you familiar with me? Our coordinate transformation theory comes in handy. Let's recall the above Coordinate Transformation Formula

The coordinate conversion formula can be interpreted as follows: For the vector V' in the world coordinate system, its coordinate in the coordinate system R is v ''. So, we can apply it here: for the coordinate V that has been reversed in the world coordinate system, the coordinate in the coordinate system R of the camera is v ''. What is camera coordinate system R? Our camera uvn system! Yes

The complete formula for camera transformation is

Here, V is the coordinates of the villain in the world space, and v'' is the coordinates of the villain in the camera space. The camera transformation matrix is

So far, we have completed the derivation of the camera matrix. After the object passes through this matrix, it is transformed from the world space to the camera space, waiting for the pipeline to perform projection transformation on it. OpenGL uses the last matrix derived above. I hope you can understand this derivation process. If you have any questions or different opinions, please send me a letter to J next time!

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.