The Viewmatrix of the MVP matrix

Source: Internet
Author: User
Matrix Derivation

The Viewmatrix is used to convert coordinates directly under the world coordinate system to the camera coordinate system. The coordinate system of a known camera and the coordinates of the camera in world space. You can find out the Viewmatrix, the following concrete derivation.


The UVN is a camera coordinate system of three base, for a camera, it at the beginning and the world coordinate system is coincident, the user control camera in the world space after moving, the state of the camera can be described by two properties-orientation and position. 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



Figure I. The transformation and inverse transformation of camera

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

C = TR

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



r^ (-1) is difficult to find out, the use of some of the orthogonal basis of some knowledge, you can refer to the collision detection of the ray-cylinder detection of the previous part of the orthogonal base.

When the camera changes to finish inverse translation This step, the camera's origin and the world origin are coincident, that is, the transformation of the translation is finished. 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.

Convert formulas by coordinates


For coordinate V ', which has been inverse translated in world coordinates, it coordinates v ' in the camera coordinate system r, and the camera coordinate system is



The complete formula for the camera transform is



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





There are three common cases of Viewmatrix, one is to use the LookAt function, the second is similar to the FPS game through pitch and yaw to calculate, there is a similar trajectory ball algorithm.


The final approach is to convert to the base of the camera's coordinate system.


Look at Camera

The reference here is the left-handed coordinate system, and in camera space, the camera's forward is in the negative direction of the z axis, consistent with unity and OpenGL.


Figure Two. Left-hand coordinate system of the camera


As shown below, this method needs to know the location of the camera eye, an up vector (global), and the camera's observer point at



Figure Three. LookAt function Model




matrix4x4 LookAt (const vector3& Eye, const vector3& target, const vector3& up)
{
	Vector3 Z ((eye-targ ET). normalized ());
	Vector3 x ((Vector3::cross (z, up). Normalized ()));
	Vector3 y (vector3::cross (x, z));

	matrix4x4 result;

	Result[0] = x.x;
	RESULT[4] = x.y;
	RESULT[8] = x.z;
	RESULT[12] =-vector3::D ot (x, eye);

	RESULT[1] = y.x;
	RESULT[5] = y.y;
	RESULT[9] = y.z;
	RESULT[13] =-vector3::D ot (y, eye);

	RESULT[2] = z.x;
	RESULT[6] = z.y;
	RESULT[10] = z.z;
	RESULT[14] =-vector3::D ot (z, eye);

	RESULT[3] = result[7] = result[11] = 0.0f;
	RESULT[15] = 1.0f;
	return result;
}

Study Examples

C + +
Qdebug () << Transform::lookat (Vector3 (1, 2, 3), Vector3 (0, ten, 0), vector3::up);



Figure Four. LookAt function Calculation results

Unity
transform.position = new Vector3 (1, 2, 3);
Transform. LookAt (New Vector3 (0, 0), vector3.up);
Debug.Log (Camera.main.worldToCameraMatrix);



Figure Five. Calculation results in Unity


(Seemingly the Transform.lookat function in unity is the rotation of a directly modified camera.) )


FPS Camera

Pass the previous conclusion

Assuming that the original coordinate system is x (1,0,0), Y (0,1,0), Z (0,0,1), and that the coordinate system is rotated by a rotation matrix matrix, the new coordinate system is the transpose three-column vector of the rotation matrix.

The only thing that is required here is the transpose of the rotation matrix.

Note that the z-direction of the camera is not the right direction of the camera, but the back of the camera.


matrix4x4 Transform::fpsview (const vector3& Eye, quaternion rotation)
{
	matrix4x4 Rotmatrix = rotation. Getrotmatrix (). Transpose ();
	Vector3 x (rotmatrix[0], rotmatrix[4], rotmatrix[8]);
	Vector3 y (rotmatrix[1], rotmatrix[5], rotmatrix[9]);
	Vector3 Z (-rotmatrix[2],-rotmatrix[6],-rotmatrix[10]);

	matrix4x4 result;

	Result[0] = x.x;
	RESULT[4] = x.y;
	RESULT[8] = x.z;
	RESULT[12] =-vector3::D ot (x, eye);

	RESULT[1] = y.x;
	RESULT[5] = y.y;
	RESULT[9] = y.z;
	RESULT[13] =-vector3::D ot (y, eye);

	RESULT[2] = z.x;
	RESULT[6] = z.y;
	RESULT[10] = z.z;
	RESULT[14] =-vector3::D ot (z, eye);

	RESULT[3] = result[7] = result[11] = 0.0f;
	RESULT[15] = 1.0f;
	return result;
}


Study Examples

C + +
Qdebug () << Transform::fpsview (Vector3, Quaternion::euler (30, 45, 60));

Results


Figure Six. FPV function Calculation results


Unity
transform.position = new Vector3 (1, 2, 3);
Transform.rotation = Quaternion.euler (a);
Debug.Log (Camera.main.worldToCameraMatrix);


Figure Seven. Running results in Unity


Reference

Understanding the View matrix-http://www.3dgep.com/understanding-the-view-matrix/

Tutorial 3:matrices-http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

OpenGL transformation-http://www.songho.ca/opengl/gl_transform.html

Deducing the camera transform matrix-http://blog.csdn.net/popy007/article/details/5120158


links

An explanation of the homogeneous coordinates and Modelmatrix of the MVP matrix

An explanation of the homogeneous coordinates and Viewmatrix of the MVP matrix

An explanation of the homogeneous coordinates and ProjectionMatrix of the MVP matrix

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.