我是靠谱客的博主 细腻帆布鞋,这篇文章主要介绍软件光栅化渲染器(一),现在分享给大家,希望可以做个参考。

说明

32位存储一像素, 通过操作一段可线性寻址的内存的视频缓存输送给显示设备,刚开始做,暂时只能画点和线和三角形,不支持裁剪, 功能一点点来吧。项目的github地址:https://github.com/xinhua302/3DRender

复制代码
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* *渲染装置 */ #ifndef DEVICE_H #define DEVICE_H #include "3DMath.h" const int RENDER_STATE_WIREFRAME = 1; // 渲染线框 const int RENDER_STATE_COLOR = 2; // 渲染纹理 class Device { public: int width; int height; UINT *frameBuffer; //图像缓存 int renderState; UINT background; //背景颜色 UINT foreground; //线框颜色 public: Device(int width, int height, void *frameBuffer, int renderState, UINT foreground, UINT background) { Init(width, height, frameBuffer, renderState, foreground, background); } ~Device() { } void Init(int width, int height, void *frameBuffer, int renderState, UINT foreground, UINT background) { this->width = width; this->height = height; this->frameBuffer = (UINT*)frameBuffer; this->renderState = renderState; this->foreground = foreground; this->background = background; } void Destroy() { if (frameBuffer != nullptr) { delete frameBuffer; frameBuffer = nullptr; } } void Clear() { memset(frameBuffer, 0, sizeof(UINT)* width * height); } //画点 void DrawPoint(int x, int y, UINT color) { frameBuffer[x + y * width] = color; } //画线 void DrawLine(int x1, int y1, int x2, int y2, UINT color) { int dy = y2 - y1; int dx = x2 - x1; int x = x1; int y = y1; if (dx == 0) { if (y <= y2) { while (y <= y2) { DrawPoint(x, y++, color); } } else { while (y > y2) { DrawPoint(x, y--, color); } } return; } float k = (float)dy / (float)dx; float b = y1 - k*x1; if (abs(k) < 1) { if (x <= x2) { while (x <= x2) { DrawPoint(x, y, color); y = (int)(k * ++x + b + 0.5); } } else { while (x > x2) { DrawPoint(x, y, color); y = (int)(k * --x + b + 0.5); } } } else { if (y <= y2) { while (y <= y2) { DrawPoint(x, y, color); x = (int)((++y - b) / k + 0.5); } } else { while (y > y2) { DrawPoint(x, y, color); x = (int)((--y - b) / k + 0.5); } } } } void DrawLine(const Point2D &p1, const Point2D &p2, UINT color) { DrawLine(p1.x, p1.y, p2.x, p2.y, color); } //画三角形 void DrawTriangle(const Point2D &p1, const Point2D &p2, const Point2D &p3, UINT color) { if (RENDER_STATE_WIREFRAME == renderState) { DrawLine(p1, p2, color); DrawLine(p2, p3, color); DrawLine(p3, p1, color); } else if (RENDER_STATE_COLOR == renderState) { //是否是平底或平顶三角形 if (p1.y == p2.y) { if (p3.y > p2.y) { FillBottomTriangle(p1, p2, p3, color); } else if (p3.y < p2.y) { FillTopTriangle(p1, p2, p3, color); } } else if (p1.y == p3.y) { if (p2.y > p1.y) { FillBottomTriangle(p1, p2, p3, color); } else if (p2.y < p1.y) { FillTopTriangle(p1, p2, p3, color); } } else if (p2.y == p3.y) { if (p3.y > p2.y) { FillBottomTriangle(p1, p2, p3, color); } else if (p3.y < p2.y) { FillTopTriangle(p1, p2, p3, color); } } //不是平底也不是平顶,需要拆分 else { //从上到下排列顶点 Point2D pList[] = { p1, p2, p3 }; SelectionSort(pList, 3); //(y - b) / k float k = (pList[0].y - pList[2].y) * 1.0f / (pList[0].x - pList[2].x); float b = pList[2].y - k*pList[2].x; int CenterPointX = (int)((pList[1].y - b) / k + 0.5); Point2D centerPoint = { CenterPointX, pList[1].y }; FillTopTriangle(pList[0], centerPoint, pList[1], color); FillBottomTriangle(centerPoint, pList[1], pList[2], color); } } } //填充平顶三角形 void FillTopTriangle(const Point2D &p1, const Point2D &p2, const Point2D &p3, UINT color) { Point2D top, left, right; if (p1.y == p2.y) { top = p3; left = p1; right = p2; } else if (p2.y == p3.y) { top = p1; left = p2; right = p3; } else { top = p2; left = p1; right = p3; } if (left.x > right.x) { Swap(left, right); } float leftDxDivDy = ((float)top.x - (float)left.x) / ((float)top.y - (float)left.y); float rightDxDivDy = ((float)right.x - (float)top.x) / ((float)right.y - (float)top.y); float xleft = (float)left.x; float xRight = (float)right.x; int y = left.y; while (y >= top.y) { DrawLine((int)(xleft + 0.5), y, (int)(xRight+0.5), y, color); y--; xleft -= leftDxDivDy; xRight -= rightDxDivDy; } } //填充平底三角形 void FillBottomTriangle(const Point2D &p1, const Point2D &p2, const Point2D &p3, UINT color) { Point2D bottom, left, right; if (p1.y == p2.y) { bottom = p3; left = p1; right = p2; } else if (p2.y == p3.y) { bottom = p1; left = p2; right = p3; } else { bottom = p2; left = p1; right = p3; } if (left.x > right.x) { Swap(left, right); } float leftDxDivDy = ((float)bottom.x - (float)left.x) / ((float)bottom.y - (float)left.y); float rightDxDivDy = ((float)right.x - (float)bottom.x) / ((float)right.y - (float)bottom.y); float xleft = (float)left.x; float xRight = (float)right.x; int y = left.y; while (y <= bottom.y) { DrawLine((int)(xleft + 0.5), y, (int)(xRight + 0.5), y, color); y++; xleft += leftDxDivDy; xRight += rightDxDivDy; } } //渲染主函数 void Render() { Clear(); Point2D p1 = { 30, 250 }; Point2D p2 = { 110, 350 }; Point2D p3 = { 200, 220 }; DrawTriangle(p1, p2, p3, foreground); DrawTriangle(p1, p2, p3, foreground); } }; #endif

最后

以上就是细腻帆布鞋最近收集整理的关于软件光栅化渲染器(一)的全部内容,更多相关软件光栅化渲染器(一)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部