-
Notifications
You must be signed in to change notification settings - Fork 0
/
RasterizerBase.cpp
220 lines (174 loc) · 6.67 KB
/
RasterizerBase.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
#include <algorithm>
#include <opencv2/opencv.hpp>
#include <math.h>
#include <stdexcept>
#include"Rasterizer.h"
#include"Triangle.h"
#include"Global.h"
//KS: mvp矩阵设置
void Rst::Rasterizer::SetModel(float rotationX, float rotationY, float rotationZ)//KS: 旋转矩阵
{
float angleZ = rotationZ * MY_PI / 180;
float angleY = rotationY * MY_PI / 180;
float angleX = rotationX * MY_PI / 180;
Eigen::Matrix4f tempZ = Eigen::Matrix4f::Identity();
tempZ <<
std::cos(angleZ), -std::sin(angleZ), 0, 0,
std::sin(angleZ), std::cos(angleZ), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
Eigen::Matrix4f tempY = Eigen::Matrix4f::Identity();
tempY <<
std::cos(angleY), 0, std::sin(angleY), 0,
0, 1, 0, 0,
-std::sin(angleY), 0, std::cos(angleY), 0,
0, 0, 0, 1;
Eigen::Matrix4f tempX = Eigen::Matrix4f::Identity();
tempX <<
1, 0, 0, 0,
0, std::cos(angleX), -std::sin(angleX), 0,
0, std::sin(angleX), std::cos(angleX), 0,
0, 0, 0, 1;
model = tempX * tempY * tempZ;
}
void Rst::Rasterizer::SetView(Eigen::Vector3f eyePos,Eigen::Vector3f at)
{
Eigen::Vector3f up (0, 1, 0);
Eigen::Vector3f xAxis, yAxis, zAxis;
zAxis = at - eyePos;
xAxis = xAxis.cross(yAxis);
yAxis = zAxis.cross(xAxis.normalized());
//KS: 而观察矩阵是相机本身变换的逆变换
Eigen::Matrix4f temp = Eigen::Matrix4f::Identity();
Eigen::Matrix4f translate;
translate <<
xAxis.x(), yAxis.x(), zAxis.x(), 0,
xAxis.y(), yAxis.y(), zAxis.y(), 0,
xAxis.z(), yAxis.z(), zAxis.z(), 0,
-(xAxis.dot(eyePos)), -(yAxis.dot(eyePos)), -(zAxis.dot(eyePos)), 1;
view = translate * temp;//KS: set view matrix
}
void Rst::Rasterizer::SetView(Eigen::Vector3f eyePos)
{
//KS: 而观察矩阵是相机本身变换的逆变换
Eigen::Matrix4f temp = Eigen::Matrix4f::Identity();
Eigen::Matrix4f translate;
translate <<
1, 0, 0, -eyePos[0],
0, 1, 0, -eyePos[1],
0, 0, 1, -eyePos[2],
0, 0, 0, 1;
view = translate * temp;//KS: set view matrix
}
void Rst::Rasterizer::SetProjection(float eyeFov, float aspectRatio, float zNear, float zFar)
{
Eigen::Vector4f temp = Eigen::Vector4f::Identity();
auto n = zNear;
auto f = zFar;
//KS: https://smuwm007.feishu.cn/docs/doccn2FNKtTm58i2R4jISC7vd4e#KufB6G
Eigen::Matrix4f p2o;
p2o <<
n, 0, 0, 0,
0, n, 0, 0,
0, 0, f + n, -f * n,
0, 0, 1, 0;
float halve = eyeFov / 2 * MY_PI / 180;
float top = -zNear * std::tan(halve);
float bottom = -top;
float right = top * aspectRatio;
float left = -right;
Eigen::Matrix4f m, s;
m <<
1, 0, 0, -(left + right) / 2,
0, 1, 0, -(top + bottom) / 2,
0, 0, 1, -(zNear + zFar) / 2,
0, 0, 0, 1;
s <<
2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1;
projection = s*m*p2o;
}
//////////////////////////////////////////////////////////////
// 框架基本功能
//////////////////////////////////////////////////////////////
//KS: 初始化窗口
Rst::Rasterizer::Rasterizer(int w, int h) :width(w), height(h)
{
frameBuffer.resize(w * h);
depthBuffer.resize(w * h);
texture = nonstd::nullopt;//KS: nonstd 用于在cpp14实现optional
}
Rst::PosId Rst::Rasterizer::LoadPosition(std::vector<Eigen::Vector3f>& pos)
{
auto id = Rst::Rasterizer::GetNextId();
posMap.emplace(id, pos);
return { id };
}
Rst::IndId Rst::Rasterizer::LoadIndex(std::vector<Eigen::Vector3i>& ind)
{
auto id = Rst::Rasterizer::GetNextId();
indMap.emplace(id, ind);
return { id };
}
Rst::ColId Rst::Rasterizer::LoadColor(std::vector<Eigen::Vector3f>& col)
{
auto id = Rst::Rasterizer::GetNextId();
colMap.emplace(id, col);
return { id };
}
void Rst::Rasterizer::Clear(Buffer buff)
{
if ((buff & Rst::Buffer::Color) == Rst::Buffer::Color)
{
std::fill(frameBuffer.begin(), frameBuffer.end(), Eigen::Vector3f{ 0,0,0 });
}
if ((buff & Rst::Buffer::Depth) == Rst::Buffer::Depth)
{
std::fill(depthBuffer.begin(), depthBuffer.end(), std::numeric_limits<float>::infinity());
}
}
void Rst::Rasterizer::SetPixel(const Eigen::Vector3f& point, const Eigen::Vector3f& color)
{
//KS: 越界判断
if (point.x() < 0 || point.x() >= width
|| point.y() < 0 || point.y() >= height)return;
auto ind = (height - 1 - point.y()) * width + point.x();
frameBuffer[ind] = color;
}
void Rst::Rasterizer::SetPixel(const Eigen::Vector2i& point, const Eigen::Vector3f& color)
{
int ind = (height - point.y()) * width + point.x();
frameBuffer[ind] = color;
}
//KS: 点是否在三角形内
bool Rst::Rasterizer::InsideTriangle(float x, float y, const Eigen::Vector4f* tri)
{
Eigen::Vector2f point(x, y);
Eigen::Vector2f AB = (tri[1].head(2) - tri[0].head(2));
Eigen::Vector2f BC = (tri[2].head(2) - tri[1].head(2));
Eigen::Vector2f CA = (tri[0].head(2) - tri[2].head(2));
Eigen::Vector2f AP = (point - tri[0].head(2));
Eigen::Vector2f BP = (point - tri[1].head(2));
Eigen::Vector2f CP = (point - tri[2].head(2));
return AB[0] * AP[1] - AB[1] * AP[0] > 0 &&
BC[0] * BP[1] - BC[1] * BP[0] > 0 &&
CA[0] * CP[1] - CA[1] * CP[0] > 0;
}
//KS: 重心坐标计算
std::tuple<float, float, float> Rst::Rasterizer::ComputeBarycentric2D(float x, float y, const Vector4f* tri)
{
//KS: https://blog.csdn.net/why18767183086/article/details/107369094
float xp = x, yp = y;
float xa = tri[0].x(), ya = tri[0].y();
float xb = tri[1].x(), yb = tri[1].y();
float xc = tri[2].x(), yc = tri[2].y();
float gamma = (((xb - xa) * (yp - ya) - (xp - xa) * (yb - ya)) / (((xb - xa) * (yc - ya)) - (yb - ya) * (xc - xa)));
float beta = (xp - xa - gamma * (xc - xa)) / (xb - xa);
float alpha = 1.0f - beta - gamma;
/*float alpha = (x * (tri[1].y() - tri[2].y()) + (tri[2].x() - tri[1].x()) * y + tri[1].x() * tri[2].y() - tri[2].x() * tri[1].y()) / (tri[0].x() * (tri[1].y() - tri[2].y()) + (tri[2].x() - tri[1].x()) * tri[0].y() + tri[1].x() * tri[2].y() - tri[2].x() * tri[1].y());
float beta = (x * (tri[2].y() - tri[0].y()) + (tri[0].x() - tri[2].x()) * y + tri[2].x() * tri[0].y() - tri[0].x() * tri[2].y()) / (tri[1].x() * (tri[2].y() - tri[0].y()) + (tri[0].x() - tri[2].x()) * tri[1].y() + tri[2].x() * tri[0].y() - tri[0].x() * tri[2].y());
float gamma = (x * (tri[0].y() - tri[1].y()) + (tri[1].x() - tri[0].x()) * y + tri[0].x() * tri[1].y() - tri[1].x() * tri[0].y()) / (tri[2].x() * (tri[0].y() - tri[1].y()) + (tri[1].x() - tri[0].x()) * tri[2].y() + tri[0].x() * tri[1].y() - tri[1].x() * tri[0].y());*/
return { alpha,beta,gamma };
}