仿射矩阵是 ShapeEx 变换系统的核心。本章学习单位矩阵、乘法、平移、缩放、旋转。
| 函数 | 说明 |
|---|---|
xge_shape_ex_matrix_t { fA, fB, fC, fD, fE, fF } | 2D 仿射矩阵结构体 |
int xgeShapeExMatrixIdentity(xge_shape_ex_matrix_t* pMatrix) | 设为单位矩阵 |
int xgeShapeExMatrixMultiply(xge_shape_ex_matrix_t* pOut, const ...* pParent, const ...* pLocal) | 矩阵乘法 Out = Parent × Local |
int xgeShapeExMatrixTranslate(xge_shape_ex_matrix_t* pMatrix, float fTX, float fTY) | 追加平移 |
int xgeShapeExMatrixScale(xge_shape_ex_matrix_t* pMatrix, float fSX, float fSY) | 追加缩放 |
int xgeShapeExMatrixRotate(xge_shape_ex_matrix_t* pMatrix, float fRadians) | 追加旋转(弧度) |
gcc -O2 -I. -o ch34.exe ch34_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch34.exe
xge_shape_ex_matrix_t mat;
xgeShapeExMatrixIdentity(&mat);
// mat = { 1, 0, 0, 1, 0, 0 } → 无变换
// 追加平移 (100, 50)
xgeShapeExMatrixTranslate(&mat, 100.0f, 50.0f);
// 现在 mat.E=100, mat.F=50xge_shape_ex_matrix_t mat;
xgeShapeExMatrixIdentity(&mat);
// 先缩放到 2 倍
xgeShapeExMatrixScale(&mat, 2.0f, 2.0f);
// 再旋转 45°
xgeShapeExMatrixRotate(&mat, 3.14159265f / 4.0f);
// 最后平移到 (300, 200)
xgeShapeExMatrixTranslate(&mat, 300.0f, 200.0f);// 父级变换:平移 (200, 100)
xge_shape_ex_matrix_t parent;
xgeShapeExMatrixIdentity(&parent);
xgeShapeExMatrixTranslate(&parent, 200, 100);
// 子级变换:旋转 30°
xge_shape_ex_matrix_t local;
xgeShapeExMatrixIdentity(&local);
xgeShapeExMatrixRotate(&local, 3.14159265f/6.0f);
// 合成:先应用 local,再应用 parent
xge_shape_ex_matrix_t result;
xgeShapeExMatrixMultiply(&result, &parent, &local);将以下代码保存为 ch34_main.c,编译后运行即可看到仿射矩阵的平移、缩放、旋转组合效果:
#include "xge.h"
static xge_shape_ex g_rect = NULL;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 原始位置(无变换)
xge_shape_ex_matrix_t id;
xgeShapeExMatrixIdentity(&id);
xgeShapeExTransformSet(g_rect, &id);
xgeShapeExFillColor(g_rect, 0x4FD8C240);
xgeShapeExDraw(g_rect, 0.25f);
// 平移 + 缩放
xge_shape_ex_matrix_t m1;
xgeShapeExMatrixIdentity(&m1);
xgeShapeExMatrixTranslate(&m1, 350, 0);
xgeShapeExMatrixScale(&m1, 1.5f, 1.5f);
xgeShapeExTransformSet(g_rect, &m1);
xgeShapeExFillColor(g_rect, 0xFFB454FF);
xgeShapeExDraw(g_rect, 0.25f);
// 旋转 30° + 平移
xge_shape_ex_matrix_t m2;
xgeShapeExMatrixIdentity(&m2);
xgeShapeExMatrixTranslate(&m2, 100, 300);
xgeShapeExMatrixRotate(&m2, 3.14159265f / 6.0f);
xgeShapeExTransformSet(g_rect, &m2);
xgeShapeExFillColor(g_rect, 0xFF6B5EFF);
xgeShapeExDraw(g_rect, 0.25f);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch34 - Affine Matrix";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
// 创建一个矩形路径
xgeShapeExCreate(&g_rect);
xgeShapeExMoveTo(g_rect, 50, 50);
xgeShapeExLineTo(g_rect, 200, 50);
xgeShapeExLineTo(g_rect, 200, 150);
xgeShapeExLineTo(g_rect, 50, 150);
xgeShapeExClose(g_rect);
xgeRun(onFrame, NULL);
xgeShapeExDestroy(g_rect);
xgeUnit();
return 0;
}xgeShapeExTransformSet 改变矩阵,同一矩形路径在不同位置/角度绘制三次。