forked from skywind3000/RenderHelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_03_box.cpp
96 lines (77 loc) · 2.85 KB
/
sample_03_box.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include "RenderHelp.h"
// 定义顶点结构
struct VertexAttrib { Vec3f pos; Vec2f uv; Vec3f color; };
// 顶点着色器输入
VertexAttrib vs_input[3];
// 模型
VertexAttrib mesh[] = {
{ { 1, -1, 1, }, { 0, 0 }, { 1.0f, 0.2f, 0.2f }, },
{ { -1, -1, 1, }, { 0, 1 }, { 0.2f, 1.0f, 0.2f }, },
{ { -1, 1, 1, }, { 1, 1 }, { 0.2f, 0.2f, 1.0f }, },
{ { 1, 1, 1, }, { 1, 0 }, { 1.0f, 0.2f, 1.0f }, },
{ { 1, -1, -1, }, { 0, 0 }, { 1.0f, 1.0f, 0.2f }, },
{ { -1, -1, -1, }, { 0, 1 }, { 0.2f, 1.0f, 1.0f }, },
{ { -1, 1, -1, }, { 1, 1 }, { 1.0f, 0.3f, 0.3f }, },
{ { 1, 1, -1, }, { 1, 0 }, { 0.2f, 1.0f, 0.3f }, },
};
// 定义属性和 varying 中的纹理坐标 key
const int VARYING_TEXUV = 0;
const int VARYING_COLOR = 1;
void draw_plane(RenderHelp& rh, int a, int b, int c, int d)
{
mesh[a].uv.x = 0, mesh[a].uv.y = 0, mesh[b].uv.x = 0, mesh[b].uv.y = 1;
mesh[c].uv.x = 1, mesh[c].uv.y = 1, mesh[d].uv.x = 1, mesh[d].uv.y = 0;
vs_input[0] = mesh[a];
vs_input[1] = mesh[b];
vs_input[2] = mesh[c];
rh.DrawPrimitive();
vs_input[0] = mesh[c];
vs_input[1] = mesh[d];
vs_input[2] = mesh[a];
rh.DrawPrimitive();
}
int main(void)
{
RenderHelp rh(800, 600);
// 定义一个纹理,并生成网格图案
Bitmap texture(256, 256);
for (int y = 0; y < 256; y++) {
for (int x = 0; x < 256; x++) {
int k = (x / 32 + y / 32) & 1;
texture.SetPixel(x, y, k? 0xffffffff : 0xff3fbcef);
}
}
// 定义变换矩阵:模型变换,摄像机变换,透视变换
Mat4x4f mat_model = matrix_set_rotate(-1, -0.5, 1, 1); // 模型变换,旋转一定角度
Mat4x4f mat_view = matrix_set_lookat({3.5, 0, 0}, {0,0,0}, {0,0,1}); // 摄像机方位
Mat4x4f mat_proj = matrix_set_perspective(3.1415926f * 0.5f, 800 / 600.0, 1.0, 500.0f);
Mat4x4f mat_mvp = mat_model * mat_view * mat_proj; // 综合变换矩阵
// 顶点着色器
rh.SetVertexShader([&] (int index, ShaderContext& output) -> Vec4f {
Vec4f pos = vs_input[index].pos.xyz1() * mat_mvp; // 扩充成四维矢量并变换
output.varying_vec2f[VARYING_TEXUV] = vs_input[index].uv;
output.varying_vec4f[VARYING_COLOR] = vs_input[index].color.xyz1();
return pos;
});
// 像素着色器
rh.SetPixelShader([&] (ShaderContext& input) -> Vec4f {
Vec2f coord = input.varying_vec2f[VARYING_TEXUV]; // 取得纹理坐标
Vec4f tc = texture.Sample2D(coord); // 纹理采样并返回像素颜色
return tc; // 返回纹理
});
// 绘制盒子
draw_plane(rh, 0, 1, 2, 3);
draw_plane(rh, 7, 6, 5, 4);
draw_plane(rh, 0, 4, 5, 1);
draw_plane(rh, 1, 5, 6, 2);
draw_plane(rh, 2, 6, 7, 3);
draw_plane(rh, 3, 7, 4, 0);
// 保存结果
rh.SaveFile("output.bmp");
// 用画板显示图片
#if defined(_WIN32) || defined(WIN32)
system("mspaint.exe output.bmp");
#endif
return 0;
}