26 May 2018

Matrix MVP

MVP Matrix is the shorthand of Model, View and Projection matrices. Model matrix represents the 3d model’s transformation from object space to world space. View matrix represents the transformation of the 3d model from the world space to the camera(view) space. Projection(Clip) matrix represents the transformation of the 3d model from camera space to clip(screen) space.


Model

Model matrix is used for the transformation of 3D Models from model space(also called object/local space) to the world space. It includes Translation, Rotation, and Scale. For more Detail see post Matrix SRT


View

View matrix is used for the transformation of 3D Models from world space to view(camera) space. This is called view transformation. As we all known, there are two major coordinate system, right hand coordinate system, and left hand coordinate system. In Unity game engine, it uses left hand coordinate system, but in the view space, it will be coverted to right hand coordinate system.

In order to get the model’s position in view space, we can place the origin of the camera at the origin of the world coordinate. based on the Transform Component in above picture, the transformation of camera in world space order is rotate (30,0,0), then translate(0,10,-10). so in order to move the camera’s position back to the world origin, we need to do the inverse transformation, first translate(0,-10,10) then rotate(-30,0,0). we can get a matrix Mview

Mview=[1000 0cos(30)sin(30)0 0sin(30)cos(30)0 0001][1000 01010 00110 0001]

However, due to the view space’s coordinate system is changed to right hand coordinate system , so we have to invert the z component by mulitply another matrix Mnegatez

Mview=MnegatezMview=[1000 0100 0010 0001]Mview

Finally, we can use this Mview muiltiply the 3d model’s world position Pworld=(9,4,18.072,1) to get the 3d model’s position in view space Pview.

PviewT=MviewPworldT=Mview[9 4 18.072 1]

Pview=(9,4,18.072,1)




Projection

Projection matrix is also called clip matrix which is used for the transformation of 3D Models from view space to clip space, then perform standard homogeneous division, also known as perspective division to get the Normalized Device Coordinates(NDC). Finally project on to screen which is known as screen space. Different from view space, the screen space is a 2-Dimensional space.

The goal of clip space is to clip the render objects based on the view frustum. everything inside this view frustum will be rendered onto screen, others will be clipped. There are 2 types of Projection, one is Perspective projection, another is orthographic projection(shown in below image).


1. Perspective Projection

Perspective projection is characterized by the near large far small. There are 4 input parameters in perspective projection, Field of View(FOV), Aspect, Near , and Far.

The purpose of perspective projection is to transform the view frustum into a cuboid for eaier clipping. After the transformation, the upper right corner of the near clipping plane of the view frustum will become the center of the front plane of the cuboid. This is known as Normalized Device Coordinates(NDC)

As can be seen from below figure, the transformation process is to enlarge the smaller part and shrink the larger part to form the final cuboid. That’s why the projection transformation is going to have a smaller effect.The x-coordinate range after transformation is [-1, 1], the y-coordinate range is [-1, 1], and the z-coordinate range is [-1, 1] (DirectX is slightly different, and the z-value range is [0, 1]).

Based on above infomation, we can get the perpective projection matrix Mfrustum.

Mpersp=[cotFOV2Aspect000 0cotFOV200 00Far+NearFarNear2NearFarFarNear 0010]

Then we can get Pclip by multiply Pview.

Pclip=MperspPview

For the derivation method, refer to the this Link



Derivation Method

Step 1:

Based on above image,, calculating y. P=(y,z) and P=(y,z). We can get

z=cot(FOVy2)

Also, because ΔPAO and ΔPAO are similiar triangle, so

yz=yz,z=cot(FOVy2)
y=yzz=yzcot(FOVy2)

Step 2:

We can use the same method to calculate the top view(see img above) to get

x=xzcot(FOVx2)$

However, we dont know the FOVx, but we can based on FOVy and the Aspect of the screen to calculate

Aspect=WH=tan(FOVx2)ztan(FOVy2)z=cot(FOVx2)cot(FOVy2)
cot(FOVx2)=cot(FOVy2)Aspect

Based on above formulas to get x’

x=xzcot(FOVx2)=xzcot(FOVy2)Aspect

Step 3:

Combine above to get P

p=(x,y,z,1)=(xcot(FOVy2)zAspect,yzcot(FOVy2),z,1)

However, z is still unknown. By the nature of the Homogeneous coordinates, if w0, then (x,y,z,1)=(wx,wy,wz,w). We can use P multiply z to get P=(xcot(FOVy2)Aspect,ycot(FOVy2),zz,z)

Step 4:

Observing the components of P’ we got above, we can find P’ actually can be calculated by using P post-multiply a Matrix, Mproj.

[xcot(FOVy2)Aspect ycot(FOVy2) zz z]=Mproj[x y z 1]=[cot(FOVy2)Aspect000 0cot(FOVy2)00 m1m2m3m4 0010][x y z 1]

So, if we can get m1, m2, m3, m4, then we can get the projection matrix M_proj. Firsly we know that the every point in the projection plane has the same z value which means z has no relationship with x or y. so we can get only when m1=m2=0, we can make sure xy won’t influence projection point z’. We can get m3z+m4=zz

In Direct3D platform after projection the range of z’ is [-1,0], we can get z’(-Far) after the z-projection in the far plane is -1, and z’(-Near) after the z-projection in the near plane is 0. Therefore we can get a set of equations

{m3(Far)+m4=Far(1)m3(Near)+m4=Near0

After calculate, we can get

{m3=FarFarNearm4=NearFarFarNear

Then Plug m1 m2 m3 m4 into Mproj, we can get M_{tempProj}:

MtempProj=[cot(FOVy2)Aspect000 0cot(FOVy2)00 00FarFarNearNearFarFarNear 0010]

Step 5.1 Unity Projection Matrix in Direct3D Platform :

In the vertex operation stage, projection transformation can be considered as the last step, then the vertices will go into rasterization stage, in rasterization stage, In Unity, the clip space uses Left Hand Coordinate, so it is necessary to convert back to left hand coordinate system. When x and y components of right hand coordinate system and left hand coordinate system has the same direction, then their z components are opposite. we can calculate it to get

Mproj=[1000 0100 0010 0001][cot(FOVy2)Aspect000 0cot(FOVy2)00 00FarFarNearNearFarFarNear 0010]=[cot(FOVy2)Aspect000 0cot(FOVy2)00 00FarNearFarNearFarFarNear 0010]

Step 5.2: Unity Projection Matrix in OpenGL Platform

Because the range of z’ in OpenGL platform is [-1,1]. We should scale the range of z from [-1,0] to [-1,1]. we can get z’(-Far) after the z-projection in the far plane is -1, and z’(-Near) after the z-projection in the near plane is 1. Therefore we can get a set of equations:

{m3(Far)+m4=Far(1)m3(Near)+m4=Near1

After calculate, we can get

{m3=Near+FarFarNearm4=2NearFarFarNear

Then plug them into MtempProj, we can get:

Mproj=[1000 0100 0010 0001][cot(FOVy2)Aspect000 0cot(FOVy2)00 00Near+FarFarNear2NearFarFarNear 0010]=[cot(FOVy2)Aspect000 0cot(FOVy2)00 00Far+NearFarNear2NearFarFarNear 0010]





2. Orthographic Projection

The view frustum of orthographic projection is a cuboid, so it will be easier to calculate the transformation matrix, Mortho.

Similarly, we need to scale the orthogonal projection matrix’s x,y,z components to the range of -1, 1 become Normalized Device Coordinates(NDC).

Mortho=[1AspectSize000 01Size00 002FarNearNear+FarFarNear 0001]

Then we can get Pclip by multiply Pview.

Pclip=MorthoPview

End –Cheng Gu


Tags: