Opengl中的矩阵变换

Translation matrices:
x,y,z是你想给某个方向添加的值。例如,如果我们想给向量(10,10,10,1) 在x方向上加10个单位,我们可以有

1. 单位矩阵(Identity matrix)
in C++
glm :: mat4 myIdentityMatrix = glm :: mat4( 1.0 f);
2. Scaling matrices

in c++:
glm :: mat4 myScalingMatrix = glm :: scale( 2.0 f, 2.0 f , 2.0 f);
3. rotation matrices
比较复杂,相关链接: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/
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
某个对象的代表矩阵。需要从模型空间的坐标系转移到世界坐标系。
Opengl中的矩阵变换_第1张图片
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 );
Opengl中的矩阵变换_第2张图片
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(所有的顶点都定义在一个小的立方体中,里面的所有顶点都将显示到屏幕上)。
Opengl中的矩阵变换_第3张图片

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 ); }

你可能感兴趣的:(opengl)