Application of OpenGL Matrix transform GLM Library

Source: Internet
Author: User
Tags sin

GLM and MVP matrix operations shorthand
Work 15 hours continuously, tired, sleep. include "GLM/GLM.HPP" include "GLM/GTC/MATRIX_TRANSFORM.HPP"

If not specifically stated, the following example assumes that the matrix/vector is four-dimensional
GLM::MAT4 Mat;
GLM::VEC4 VEC;
For VEC, the fourth digit is 1 for the coordinates and 0 for the direction.

Translation matrix
| 1 0 0 X |
| 0 1 0 Y |
| 0 0 1 Z |
| 0 0 0 1 |

Constructing translation matrices
GLM::MAT4 mat = Glm::translate (X, Y, Z);

Stretching matrix
| X 0 0 0 |
| 0 Y 0 0 |
| 0 0 Z 0 |
| 0 0 0 1 |

Constructing the tensile matrix
GLM:MAT4 mat = Glm::scale (x, y, z);

Rotation matrix
If the hinge is x, Y, z, the angle is a
x = x sin (a), y = y sin (a), z = z sin (a), W = cos (a)
| 1-2 (y*y+z*z) | 2 (X*Y+Z*W) | 2 (X*Z-Y*W) | 0 |
| 2 (X*Y-Z*W) | 1-2 (x*x+z*z) | 2 (Y*Z+X*W) | 0 |
| 2 (X*Z+Y*W) | 2 (Y*Z-X*W) | 1-2 (x*x+y*y) | 0 |
| 0 | 0 | 0 | 1 |

Constructing the rotation matrix
GLM::VEC3 axis (x, y, z);
Glm::mat4 Transformedmatrix = Glm::rotate (Mat, A, axis);

The elementary transformation matrix is multiplied by the coordinates to get the transformed coordinates.
Mat * VEC (= VEC)
GLM::VEC4 Transformedvector = Mat * VEC;

Constructing model matrices
GLM::MAT4 mat = transmat3 * TRANSMAT2 * TRANSMAT1 * MAT;

Constructing the View matrix
GLM::MAT4 mat = Glm::lookat (Camerapos, Cameratarget, upvector);
If the camera is set, then Upvector = GLM:VEC3 (0, 1, 0)

Constructing projection matrices
GLM::MAT4 mat = glm::p erspective (FoV, Aspectratio, Nearclipplane, Farclipplane);

            Model matrix                view matrix                 projection matrix

Model coordinates ———-> absolute coordinates ———-> camera coordinates ———-> homogeneous coordinates
The final step is to simulate the process of human eye imaging with affine transformation.
GLM:MAT3 MVP = projection * View * model;

Register handle before loop
Gluint Matrixid = glgetuniformlocation (ProgramID, "MVP");
Pass to GLSL in the loop
GLUNIFORMMATRIX4FV (Matrixid, 1, Gl_false, &mvp[0][0]);

GLSL vertex shader End processing
Layout (location = 0) in VEC3 vertexposition_modelspace;
Uniform mat4 MVP;
Void Main () {
Vec4 v = vec4 (vertexposition_modelspace, 1);
Gl_position = MVP * v;
}

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.