OpenGL study notes, "five" conversion

  Conversion (Transformations), mainly refers to the displacement (Translate), scaling (Scaling), rotation (Rotation) three kinds, the three conversion OpenGL achieved primarily through the operation matrix to achieve. Here it will involve a certain matrix, the vector operation.

  opengl does not provide ready-made matrix, vector operations of the library, the need to introduce our own third-party libraries, in the tutorial author is using to GLM library, you can here download.

  Because the matrix operation is not commutative, so here the matrix arithmetic operation for converting the sequence is required, the need to scale (Scaling), then rotate (Rotation), and finally displacement (Rotation). The actual writing process may well be in this order: T * R * S, call in the code sequence is T, R, S, need to look at here. We understand / read matrix equation combined operations, should be the order from right to left.

  glm provide ready displacement, scaling, rotation matrix operation method, respectively glm :: Translate , glm :: Scale , glm :: rotation , here if we need to implement a scaling, rotation, displacement of the combined operation, our code something like this:

// conversion matrix 
GLM GLM :: :: = MAT4 Trans MAT4 ( 1.0f );
 // displacement units 0.5 x axis, y-axis moving unit -0.5 
Trans GLM :: = Translate (Trans, GLM :: Vec3 ( 0.5f , - 0.5f , 0.0f ));
 // rotate about the y-axis by 45 degrees 
Trans :: = GLM rotation (Trans, GLM :: radians ( 45.0f ), GLM :: Vec3 ( 0.0f , 1.0f , 0.0f ));
 // scale 0.5 
Trans :: = GLM scale (Trans, GLM :: Vec3 ( 0.5f , 0.5f , 0.5f ));

  This process is a combination of the operation to finally obtain a converted matrix. Matrix and then passed to the converted vertex shader, you can see the final results of the conversion. Vertex shader receives the converted matrix:

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aCoord;
out vec3 oColor;
out vec2 oCoord;
uniform mat4 u_transform;
void main()
    gl_Position = u_transform * vec4(aPos, 1.0f);
    oColor = aColor;
    oCoord = aCoord;
}

  Also has a corresponding internal GLSL mat4 types of variables used to store conversion matrix data transmission over, and then multiplied by the vertex coordinates, the coordinate information may be obtained after conversion.

  Assignment of uniform section corresponding to the adjusted

glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(trans));

  1 is a location index uniform variable parameter, the parameter that we want to pass over the 4 matrix data, using glm value_ptr glm method to convert a matrix format stored in the format used by GLSL.

  Corresponding code here .

Guess you like

Origin www.cnblogs.com/zhong-dev/p/11649813.html