来源:《Computer Graphics Programming in OpenGL Using C++ 》by V Scott Gordon John L Clevenger
内容:程序3.1 Program 3.1: Building Transformation Matrices in GLSL,书P56页,PDF75/403
说明:以下构造矩阵的函数模块
#include "include/glm/glm.hpp"
#include <iostream>
// 创建并返回一个平移矩阵;
glm::mat4 buildTranslate(float x, float y, float z)
{
glm::mat4 trans = glm::mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0);
return trans;
}
// builds and returns a matrix that performs a rotation around the X axis
// 创建并返回一个绕x轴旋转的矩阵
glm::mat4 buildRotateX(float rad)
{
glm::mat4 xrot = glm::mat4(1.0, 0.0, 0.0, 0.0,
0.0, cos(rad), -sin(rad), 0.0,
0.0, sin(rad), cos(rad), 0.0,
0.0, 0.0, 0.0, 1.0);
return xrot;
}
// builds and returns a matrix that performs a rotation around the Y axis
// 创建并返回一个绕 Y 轴旋转的矩阵
glm::mat4 buildRotateY(float rad)
{
glm::mat4 yrot = glm::mat4(cos(rad), 0.0, sin(rad), 0.0,
0.0, 1.0, 0.0, 0.0,
-sin(rad), 0.0, cos(rad), 0.0,
0.0, 0.0, 0.0, 1.0);
return yrot;
}
// builds and returns a matrix that performs a rotation around the Z axis
// 创建并返回一个绕 Z 轴旋转的矩阵
glm::mat4 buildRotateZ(float rad)
{
glm::mat4 zrot = glm::mat4(cos(rad), -sin(rad), 0.0, 0.0,
sin(rad), cos(rad), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
return zrot;
}
// builds and returns a scale matrix
// 创建并返回一个缩放矩阵
glm::mat4 buildScale(float x, float y, float z)
{
glm::mat4 scale = glm::mat4(x, 0.0, 0.0, 0.0,
0.0, y, 0.0, 0.0,
0.0, 0.0, z, 0.0,
0.0, 0.0, 0.0, 1.0);
return scale;
}