Translation matrices:
![](https://i-blog.csdnimg.cn/blog_migrate/c8bb58a9a1c3b567a6d39352e173d9b8.png)
x,y,z是你想给某个方向添加的值。例如,如果我们想给向量(10,10,10,1) 在x方向上加10个单位,我们可以有
![](https://i-blog.csdnimg.cn/blog_migrate/e699d86da9dacdeb3477bfeb58837af8.png)
1. 单位矩阵(Identity matrix)
![](https://i-blog.csdnimg.cn/blog_migrate/e907d9790718708eee05764ea18d13e5.png)
in C++
glm
::
mat4 myIdentityMatrix
=
glm
::
mat4(
1.0
f);
2. Scaling matrices
![](https://i-blog.csdnimg.cn/blog_migrate/9910e257952c63ab88345bbbda54665d.png)
in c++:
glm
::
mat4 myScalingMatrix
=
glm
::
scale(
2.0
f,
2.0
f ,
2.0
f);
3. rotation matrices
4.Cumulating transformations
TransformedVector
=
TranslationMatrix
*
RotationMatrix
*
ScaleMatrix
*
OriginalVector;
in C++, with GLM :
glm
::
mat4 myModelMatrix
=
myTranslationMatrix
*
myRotationMatrix
*
myScaleMatrix;glm
::
vec4 myTransformedVector
=
myModelMatrix
*
myOriginalVector;
5. Model matrix
某个对象的代表矩阵。需要从模型空间的坐标系转移到世界坐标系。
![](https://i-blog.csdnimg.cn/blog_migrate/9ca4df55035bfffbbf4cbaace60c6f2f.png)
6.View matrix:
view matrix实际上是想根据你的位置来表示对象的矩阵。例如你想从某个角度看一座山,你可以移动camera也可以移动这座山。实际中你不能移动山,但是在计算机中两者都可以。
C++
glm
::
mat4 ViewMatrix
=
glm
::
translate(glm
::
mat4(), glm
::
vec3(
-
3.0
f,
0.0
f ,
0.0
f));
glm
::
mat4 CameraMatrix
=
glm
::
lookAt( cameraPosition,
// the position of your camera, in world space
cameraTarget,
// where you want to look at, in world space
upVector
// probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too
);
![](https://i-blog.csdnimg.cn/blog_migrate/a8a26a227e4c09bf47453c08a1374f3b.png)
7. Projection Matrix
Projection Matrix 的作用是为了让远处的物体看起来小,近处的物体看起来大。
C++:
// Generates a really hard-to-read matrix, but a normal, standard 4x4 matrix nonetheless
glm
::
mat4 projectionMatrix
=
glm
::
perspective( glm
::
radians(FoV),
// The vertical Field of View, in radians: the amount of "zoom". Think "camera lens". Usually between 90° (extra wide) and 30° (quite zoomed in)
4.0
f
/
3.0
f,
// Aspect Ratio. Depends on the size of your window. Notice that 4/3 == 800/600 == 1280/960, sounds familiar ?
0.1
f,
// Near clipping plane. Keep as big as possible, or you'll get precision issues.
100.0
f
// Far clipping plane. Keep as little as possible.
);
这样我们就从Camera Space(所有顶点的位置都是根据camera的位置来确定)转移到了Homogeneous Space(所有的顶点都定义在一个小的立方体中,里面的所有顶点都将显示到屏幕上)。
![](https://i-blog.csdnimg.cn/blog_migrate/318ba19b07eb6a4d230bef5de9b5b684.png)
8. Cumulating transformations: the ModelViewProjection matrix
// C++ : compute the matrix
glm
::
mat4 MVPmatrix
=
projection
*
view
*
model;
// Remember : inverted !
8.1 生成MVP矩阵
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm
::
mat4 Projection
=
glm
::
perspective(glm
::
radians(
45.0
f), (
float
) width
/
(
float
)height,
0.1
f,
100.0
f);
// Or, for an ortho camera ://glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
// Camera matrix
glm
::
mat4 View
=
glm
::
lookAt( glm
::
vec3(
4
,
3
,
3
),
// Camera is at (4,3,3), in World Space
glm
::
vec3(
0
,
0
,
0
),
// and looks at the origin
glm
::
vec3(
0
,
1
,
0
)
// Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
glm
::
mat4 Model
=
glm
::
mat4(
1.0
f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm
::
mat4 mvp
=
Projection
*
View
*
Model;
// Remember, matrix multiplication is the other way around
8.2 交给GLSL
// Get a handle for our "MVP" uniform// Only during the initialisation
GLuint MatrixID
=
glGetUniformLocation(program_id,
"MVP"
);
// Send our transformation to the currently bound shader, in the "MVP" uniform// This is done in the main loop since each model will have a different MVP matrix (At least for the M part)
glUniformMatrix4fv(mvp_handle,
1
, GL_FALSE,
&
mvp[
0
][
0
]);
8.3 在GLSL中用来转换我们的顶点
// Input vertex data, different for all executions of this shader.
layout
(location
=
0
)
in
vec3
vertexPosition_modelspace;
// Values that stay constant for the whole mesh.
uniform
mat4
MVP;
void
main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position
=
MVP
*
vec4
(vertexPosition_modelspace,
1
);
}