opengl 第六课(3D数学,矩阵变换)

概念

向量
M3DVector3f

矩阵
M3DMatrix44f
opengl 默认:列矩阵
{
Ax, Bx, Cx, Dx
Ay, By, Cy, Dy
Az, Bz, Cz, Dz
0, 0, 0, 1
}

矩阵相乘规则
矩阵A * 矩阵B
当 A的列 等于 B的行
生成 A的行数、B的列数的矩阵

叉乘
m3dCrossProduct3
点乘
m3dDotProduct3

矩阵相关的变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 平移
inline void m3dTranslationMatrix44(M3DMatrix44f m, float x, float y, float z)
// 旋转
void m3dRotationMatrix44(M3DMatrix44f m, float angle, float x, float y, float z)
// 缩放
inline void m3dScaleMatrix44(M3DMatrix44f m, float xScale, float yScale, float zScale)

// 矩阵相乘
void m3dMatrixMultiply44(M3DMatrix44f product, const M3DMatrix44f a, const M3DMatrix44f b)


// --- MVP 矩阵的变换操作
// 几何变换管道,变换两个栈
GLGeometryTransform transformPipeline
GLMatrixStack modelViewMatrix; // 视图模型栈
GLMatrixStack projectionMatrix; // 投影栈
GLFrame cameraFrame; // 视图3D向量, 可转为视图矩阵
GLFrame objectFrame; // 模型3D向量, 可转为模型矩阵
GLFrustum viewFrustum; // 投影空间模型(圆锥台),可获取投影矩阵

// A
// 设置变换管道
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);

// B
// 设置投影矩阵
viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 500.0f);
// 压入投影矩阵
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix())
// 压入单元矩阵
modelViewMatrix.LoadIdentity();

// C
// 改变模型变换
objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
objectFrame.MoveForward(15.0f);
...

// 压入矩阵计算,并且替换栈顶数据
modelViewMatrix.PushMatrix(objectFrame)
// 记得要 pop
modelViewMatrix.PopMatrix()

// D
// 获取 MVP 矩阵结果
transformPipeline.GetModelViewProjectionMatrix()