Camera Transformation matrix

Source: Internet
Author: User

Some netizens write to me hoping to understand the concrete derivation process of world space to camera space transformation matrix in fixed pipeline. In fact, I used the vector geometry in game programming in 6 has simply described the camera transform as an example of the use of base theory, but may still not be specific enough. In this article, I will try to explain the entire context of the camera transformation. Hope to be able to learn the fixed line of friends are helpful. Here we will still introduce a few theoretical knowledge before deducing the camera transformation, in order to better understand the derivation process. We'll start right away. What is a camera transform.

In the pipeline, when the object is transformed from the model coordinate through the world matrix into World space, it transforms from the world space to the camera space through the camera transform. In the fixed pipeline below, the part of the blue box is the process.

In fact, the so-called camera space, is a camera as the coordinates of the origin of a reference system, so, from the world space to the camera space, is the object from the world coordinate system, the camera as the origin of the camera coordinate system, as shown in the figure below.

The left half is the position of the villain in world space, and the right half is the place where the villain transforms into the camera space. Such a transformation can be implemented in a number of ways: the Euler camera system, the UVN system, the Points & A Twist, and so on. Here we discuss the most extensive UVN system to build the camera matrix, if the reader is interested in other methods, you can find relevant information. We still discuss the derivation of OpenGL's camera matrix, and other APIs can be similar in derivation. Coordinate conversion formula

We refer to the orthogonal matrix in the use of vector geometry in game programming 6, which is a concept based on the base theory (if you are not familiar with base theory, refer to the use of vector geometry in game programming 6). Orthogonal matrix all column (row) vectors constitute a standard orthogonal base (its column vectors are orthogonal to each other, and the length is 1), so the orthogonal matrix can be regarded 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, you can use an orthogonal matrix to represent a coordinate system (or as a base) to write down the coordinates of a vector in a uniform reference system (the global coordinate system) in different bases.

The above equation indicates that the coordinate of vector v in the reference system is V ', and the coordinates in base R are V ' (note that in this environment the base matrix is represented by a column vector, so that the result after multiplication represents a linear combination of the base vector). As shown below, the black base is the reference system, red is the base Q, and Blue is the base r,v is a vector in the reference system.

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

The above means: the vector v in the reference system, in the base Q (1 0 0), (0 1 0), (0 0 1) coordinates are (1 2 6), in the base r (0 1 0), (0 0 1), (1 0 0) coordinates are (2 6 1). Note that all of the base and vector relationships we are discussing are linear representations of relationships, there is no displacement relationship, so we use 3D vectors instead of 4D homogeneous representations (if the alignment coordinates are not very familiar, refer to the part of the homogeneous coordinates in "Deep Exploration perspective projection transformation").

Thus, it is known that a base Q and a vector v in its coordinates v ', and another base R, we can calculate V ' by V=qv ' =RV ' formula.

Above is the formula for V ', notice that the right side needs to calculate the inverse matrix of the base R R^-1, because the base R is an orthogonal matrix, and an important property of the orthogonal matrix is inverse equal to transpose. So, 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

This coordinate conversion formula can be interpreted as: for the vector v ' in the world coordinate system, it coordinates in the coordinate system R is V '. We'll use this formula in the back.

In addition to using orthogonal matrices to illustrate coordinate transformations, we can also use the collinear degree represented by the dot product (colinear amount) to describe the coordinate transformation (Andrélamothe's Tricks of the 3D Game programming Gurus "). This theory is based on the geometric meaning of the dot product: How much a vector is collinear on another vector. For example, two vectors v and s dot product

Geometric meaning is V in the s direction of the projection length and s of the long product, or s in the v direction of the projection length and V long product (product symbol: If the angle of V and S is less than 90 degrees, product is positive, if it is right angle, product is zero, otherwise negative).

Further, if V is a unit vector, the dot product can be interpreted as the projection length of s in v direction, and 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 level, the dot product of a vector v ' and a base r's three unit axis vectors, and the three values from the dot product represent the vector at the base of the coordinates V '

Mathematically expressed as

Note that in order for V ' to be able to dot product with each axis vector of the base, we must write the base as a transpose and multiply the vectors, otherwise it becomes the form of a linear combination. The meaning of this formula is that the axis vectors of the vector v ' and the base r in World space are dot product to get the collinear degree of V ' under r-coordinate V '. This formula is exactly the same as the coordinate conversion formula we got above. In fact, we are explaining the same formula from two different directions and hope you can understand it in two directions. UVN system

The UVN system itself is a base. As shown in the following figure, the three base vector u,v,n points to the right, top, and rear of the camera, respectively, to form the left-hand coordinate system and the camera at the coordinates origin.

The camera orientation can be easily set using the UVN system. The build process is as follows

In the reference system (here is the world coordinate system), we give the position of the camera--eye, the position of the observed villain--lookat, and an auxiliary vector-the vector up in the reference system that represents "above", which affects the generation of U and V, Because the V-vectors that are later calculated will be on the plane determined by the up and n vectors (which you are interested in proving yourself), you can use this vector to give the camera a different deflection.

First we find the vector n

It's easy to subtract the position of the camera with the target position, which is step 2 in the diagram. 3rd Step, we find the vector u. This step requires the use of the auxiliary vector up, if you do not want the camera to be deflected, generally take (0, 1, 0)

You use the cross-multiply implementation of the vector, which is step 3 in the diagram. Finally, using N and u to calculate the vector v

Finally, the calculated U,v and n are unit, and the UVN system of the camera is obtained. In conjunction with the coordinate transformation theory we talked about above, we can think of the UVN system as the base of the camera, which makes it easy to convert a vector into world coordinates and camera coordinates.

OpenGL's Glulookat (Eyex, Eyey, Eyez, Lookatx, Lookaty, Lookatz, UPX, Upy, Upz) method is the setting of the camera matrix using the steps above. Its first three parameters are the camera position vector, the middle three parameters are the observed target position vector, the last three parameters are auxiliary vector up. Derivation of the camera matrix

Above we have explained the UVN system, the standard pipeline is the use of the UVN system to describe the camera. U, V, n correspond to the three base vectors of the camera coordinate system, respectively.

Also, for a camera, it is coincident with the world's coordinate system at the beginning, and the camera's state can be described with two attributes-orientation and position-after the user controls the camera in world space. That is, with these two properties, the state of a camera model in the world is determined. And these two properties, we use the transformation theory to describe, is the rotation and the movement. As you can imagine, for any camera state in the world, we can think of it as: the camera rotates a certain angle around its base origin, and then pans to somewhere in the world space. The following illustration shows the process

In the picture, red is the base of the camera, and black is the base of the world, which is the reference system. A villain is an object in the world. Before the camera moves, the two bases are coincident. When the camera is positioned in the screen, it first determines the orientation-rotation, and then the position is determined-panning. The rotation and translation two steps in the figure are the transformations that occur when the camera is positioned. You can see the motion of the camera relative to the villain. When the camera is transformed, the villain should shift from the world base to the kiri surface of the camera. In this way, he should carry out a camera positioning of the inverse positioning, first counter-translational villain and camera, and then reverse the rotation of the villain and the camera, the last camera homing, the villain with the camera into the camera space. This is done by the inverse translation and inverse rotation two steps, which are the camera transforms. Now we derive this transformation. We write the relationship, the transformation of the camera itself C includes two elements

where T is a translation transformation, R is a rotation transformation. And the camera transform is the inverse transformation of the camera itself.

This c^-1 is the camera transform we asked for. Where T^-1 is easy to find out that

And r^-1 is not so easy to find out. So, we don't ask for it, we use the UVN system. What do you mean. Take a look at the picture of the camera transformation above, when the camera changes to finish inverse translation This step, the camera's origin and the world origin is coincident, that is, the transformation of the translation is processed. The next thing we want to do is reverse rotation, and in fact the aim of the inverse rotation is to get the coordinates of the villain in the coordinate system of the camera in the coordinates of the inverse translation in the current world. is not familiar. Our theory of coordinate transformation comes in handy. We recall the formula for the above coordinate transformation

This coordinate conversion formula can be interpreted as: for the vector v ' in the world coordinate system, it coordinates in the coordinate system R is V '. So, we can apply it here: for coordinates v ' in world coordinates that have been translated in reverse, it coordinates v ' in the camera coordinate system R. What is the camera coordinate system R. Is our camera UVN system. It is

The complete formula for the camera transform is

Here, V is the villain's coordinate in world space, and V ' is the villain's coordinate in camera space. The Camera transformation matrix is

At this point, we have completed the deduction of the camera matrix. The object passes through this matrix and transforms from world space to camera space, waiting for the pipeline to transform it into a projection. OpenGL uses the last matrix deduced above. I hope you can understand this derivation process, if you have any questions or different views, please be sure to send me a letter J next time see.

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.