-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rasterizer.cpp
287 lines (249 loc) · 8.01 KB
/
Rasterizer.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
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
284
285
286
#include <algorithm>
#include <opencv2/opencv.hpp>
#include <math.h>
#include <stdexcept>
#include"Rasterizer.h"
#include"Triangle.h"
#include "Global.h"
void Rst::Rasterizer::DrawTriangle(Rst::PosId posBuf,Rst::IndId indBuf,Rst::ColId colBuf, Primitive type)
{
//KS: 按照id取出三角形
auto& buf = posMap[posBuf.id];
auto& ind = indMap[indBuf.id];
auto& col = colMap[colBuf.id];
float f1 = (50 - 0.1) / 2.0;
float f2 = (50 + 0.1) / 2.0;
Eigen::Matrix4f mvp = projection * view * model;
for (auto& i : ind)
{
Triangle t;
//KS: 使用索引取出每个三角形顶点
Eigen::Vector4f v[] = {
mvp * ToVec4(buf[i[0]],1.0f),
mvp * ToVec4(buf[i[1]], 1.0f),
mvp * ToVec4(buf[i[2]],1.0f)
};
//KS: 透视除法 Homogeneous division齐次除法是的w为1
for (auto& vec : v)
{
vec /= vec.w();
}
//KS: 视口变换
for (auto& vert : v)
{
vert.x() = 0.5 * width * (vert.x() + 1.0);
vert.y() = 0.5 * height * (vert.y() + 1.0);
vert.z() = vert.z() * f1 + f2;
}
for (int i = 0; i < 3; i++)
{
t.SetVertex(i, v[i]);
//KS: ???疑问???
/*不太清楚为什么Games101的代码框架中这里要重复设置三次
t.SetVertex(i, v[i].head<3>());
t.SetVertex(i, v[i].head<3>());*/
}
auto colX = col[i[0]];
auto colY = col[i[1]];
auto colZ = col[i[2]];
t.SetColor(0, colX);
t.SetColor(1, colY);
t.SetColor(2, colZ);
if (type == Rst::Primitive::Line)
{
RasterizeTriangleLine(t);//KS: 线框模型
}
else if (type == Rst::Primitive::Fill)
{
RasterizeTriangleFill(t);//KS: 重心坐标着色
}
else
{
throw std::runtime_error("Drawing primitives other than triangle is not implemented yet!");
}
}
}
//KS: 着色模型
void Rst::Rasterizer::RasterizeTriangleFill(const Triangle& t)
{
auto tri = t.ToVector4();
//KS: 确定图形的aabb大小
float minX = std::floor(std::min(tri[0][0], std::min(tri[1][0], tri[2][0])));//KS: 拿出ABC点的x坐标
float minY = std::floor(std::min(tri[0][1], std::min(tri[1][1], tri[2][1])));//KS: 拿出ABC点的y坐标
float maxX = std::ceil(std::max(tri[0][0], std::max(tri[1][0], tri[2][0])));//KS: 拿出ABC点的x坐标
float maxY = std::ceil(std::max(tri[0][1], std::max(tri[1][1], tri[2][1])));//KS: 拿出ABC点的y坐标
//KS: 抗锯齿在global.h中设置
if (MSAA)
{
std::vector<Eigen::Vector2f> pos
{
{0.25,0.25},
{0.75,0.25},
{0.25,0.75},
{0.75,0.75},
};
for (int x = minX; x <= maxX; x++)
{
for (int y = minY; y <= maxY; y++)
{
int count = 0;
for (int i = 0; i < 4; i++)
{
if (InsideTriangle((float)x+pos[i][0], (float)y+pos[i][1], t.v))
{
//KS: 计算重心坐标
auto abg = ComputeBarycentric2D((float)x + pos[i][0], (float)y + pos[i][1], t.v);
auto alpha = std::get<0>(abg);
auto beta = std::get<1>(abg);
auto gamma = std::get<2>(abg);
float w = 1.0 / (alpha / tri[0].w() + beta / tri[1].w() + gamma / tri[2].w());//KS: 视口变换
float z = alpha * tri[0].z() / tri[0].w() + beta * tri[1].z() / tri[1].w() + gamma * tri[2].z() / tri[2].w();
z *= w;
count++;
if (count != 0)
{
//KS: 着色
if (depthBuffer[GetIndex(x, y)] > z)
{
//Eigen::Vector3f point = Eigen::Vector3f(x, y, z);
Eigen::Vector2i point = Eigen::Vector2i(x, y);
SetPixel(point, t.GetColor()*count/4.0);
depthBuffer[GetIndex(x, y)] = z;
}
}
}
}
}
}
}
else
{
for (int x = minX; x <= maxX; x++)
{
for (int y = minY; y <= maxY; y++)
{
if (InsideTriangle(x, y, t.v))
{
//KS: 计算重心坐标
auto abg = ComputeBarycentric2D(x + 0.5f, y + 0.5f, t.v);
auto alpha = std::get<0>(abg);
auto beta = std::get<1>(abg);
auto gamma = std::get<2>(abg);
float w = 1.0 / (alpha / tri[0].w() + beta / tri[1].w() + gamma / tri[2].w());//KS: 视口变换
float z = alpha * tri[0].z() / tri[0].w() + beta * tri[1].z() / tri[1].w() + gamma * tri[2].z() / tri[2].w();
z *= w;
//KS: 着色
if (depthBuffer[GetIndex(x, y)] > z)
{
Eigen::Vector3f point = Eigen::Vector3f(x, y, z);
SetPixel(point, t.GetColor());
depthBuffer[GetIndex(x, y)] = z;
}
}
}
}
}
}
//KS: 线框模型渲染器
void Rst::Rasterizer::RasterizeTriangleLine(const Triangle& t)
{
DrawLine(t.c(), t.a(),Rst::Color.red);
DrawLine(t.c(), t.b(),Rst::Color.blue);
DrawLine(t.b(), t.a(), Rst::Color.green);
}
//KS: bresenham's line
void Rst::Rasterizer::DrawLine(Eigen::Vector4f begin, Eigen::Vector4f end, Eigen::Vector3f color)
{
auto x1 = begin.x();
auto y1 = begin.y();
auto x2 = end.x();
auto y2 = end.y();
int x, y, dx, dy, dx1, dy1, px, py, xe, ye, i;
dx = x2 - x1;
dy = y2 - y1;
dx1 = fabs(dx);
dy1 = fabs(dy);
//KS: 优化为整数运算, 不用除法,将k拆为k=dy/dx
//KS: px为e0值, e0的时候x0=y0=b=0带入公式:dx(d1-d2)= dy(xi+1)-2yi*dx + (2b-1)*dx
px = 2 * dy1 - dx1;
py = 2 * dx1 - dy1;
if (dy1 <= dx1)//KS: 保证k小于+-45度
{
if (dx >= 0)
{
x = x1;
y = y1;
xe = x2;
}
else
{
x = x2;
y = y2;
xe = x1;
}
//KS: e0递归初始值
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
SetPixel(point, color);
for (i = 0; x < xe; i++)
{
x = x + 1;
if (px < 0)
{
px = px + 2 * dy1;
}
else
{
if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))//KS: 判断斜率为正还是负
{
y = y + 1;
}
else
{
y = y - 1;
}
px = px + 2 * (dy1 - dx1);
}
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
SetPixel(point, color);
}
}
else //KS: 大于45度到135度
{
if (dy >= 0)
{
x = x1;
y = y1;
ye = y2;
}
else
{
x = x2;
y = y2;
ye = y1;
}
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
SetPixel(point, color);
for (i = 0; y < ye; i++)
{
y = y + 1;
if (py <= 0)
{
py = py + 2 * dx1;
}
else
{
if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
{
x = x + 1;
}
else
{
x = x - 1;
}
py = py + 2 * (dx1 - dy1);
}
Eigen::Vector3f point = Eigen::Vector3f(x, y, 1.0f);
SetPixel(point, color);
}
}
}