通过矩阵变换旋转、缩放、平移渐变坐标系,实现斜向渐变、动态旋转渐变等效果。
| 函数 | 说明 |
|---|---|
int xgeShapeExFillGradientTransformSet(xge_shape_ex pShape, const xge_shape_ex_matrix_t* pMatrix) | 设置渐变变换矩阵 |
int xgeShapeExFillGradientTransformIdentity(xge_shape_ex pShape) | 重置为单位矩阵 |
int xgeShapeExFillGradientTransformGet(xge_shape_ex pShape, xge_shape_ex_matrix_t* pMatrix) | 获取当前渐变变换 |
gcc -O2 -I. -o ch45.exe ch45_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch45.exe
xge_shape_ex_color_stop_t stops[] = {
{ 0.0f, 0x4FD8C2FF },
{ 1.0f, 0xFFB454FF },
};
xge_shape_ex shape = NULL;
xgeShapeExCreate(&shape);
xgeShapeExAppendRect(shape, 100,150, 600,300, 16,16, 1);
// 水平渐变
xgeShapeExFillLinearGradient(shape,
0,0, 1,0,
XGE_SHAPE_EX_GRADIENT_OBJECT_BOUNDING_BOX,
stops, 2);
// 旋转渐变坐标系 45°
xge_shape_ex_matrix_t m;
xgeShapeExMatrixIdentity(&m);
xgeShapeExMatrixRotate(&m, 45.0f);
xgeShapeExFillGradientTransformSet(shape, &m);
xgeShapeExDraw(shape, 0.25f);// 在帧循环中每帧更新旋转角度
static float s_fAngle = 0.0f;
void OnFrame(void) {
s_fAngle += 1.0f;
if (s_fAngle >= 360.0f) s_fAngle -= 360.0f;
xge_shape_ex_matrix_t m;
xgeShapeExMatrixIdentity(&m);
xgeShapeExMatrixRotate(&m, s_fAngle);
xgeShapeExFillGradientTransformSet(g_shape, &m);
xgeShapeExDraw(g_shape, 0.25f);
}将以下代码保存为 ch45_main.c,编译后运行即可看到渐变坐标旋转动画效果:
#include "xge.h"
static float s_fAngle = 0.0f;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
s_fAngle += 1.0f;
if (s_fAngle >= 360.0f) s_fAngle -= 360.0f;
xge_shape_ex_color_stop_t stops[] = {
{ 0.0f, 0x4FD8C2FF },
{ 1.0f, 0xFFB454FF },
};
xge_shape_ex shape = NULL;
xgeShapeExCreate(&shape);
xgeShapeExAppendRect(shape, 100,150, 600,300, 16,16, 1);
// 水平渐变(OBJECT_BOUNDING_BOX 模式)
xgeShapeExFillLinearGradient(shape,
0,0, 1,0,
XGE_SHAPE_EX_GRADIENT_OBJECT_BOUNDING_BOX,
stops, 2);
// 旋转渐变坐标系
xge_shape_ex_matrix_t m;
xgeShapeExMatrixIdentity(&m);
xgeShapeExMatrixRotate(&m, s_fAngle);
xgeShapeExFillGradientTransformSet(shape, &m);
xgeShapeExDraw(shape, 0.25f);
xgeShapeExDestroy(shape);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch45 - Gradient Transform";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}