-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster.cpp
271 lines (227 loc) · 6.52 KB
/
raster.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
#pragma once
#include "image.h"
#include "model.h"
#include <array>
#include <Eigen/Dense>
void line(int x0, int y0, int x1, int y1, Image& img, Color color) {
if (x0 > x1) {
std::swap(x0, x1);
std::swap(y0, y1);
}
int dy = abs(y1 - y0);
int dx = x1 - x0;
bool decrease = false;
if (y0 > y1) {
decrease = true;
}
bool swapped = false;
if (dy > dx) {
std::swap(dy, dx);
std::swap(x0, y0);
std::swap(x1, y1);
swapped = true;
}
int error = 0;
if (swapped && decrease) {
int y = y1;
for (int i = x1; i <= x0; i++) {
img.set(y, i, color);
error += dy;
if (error >= dx) {
error -= dx;
y--;
}
}
}
else if (swapped && !decrease) {
int y = y0;
for (int i = x0; i <= x1; i++) {
img.set(y, i, color);
error += dy;
if (error >= dx) {
error -= dx;
y++;
}
}
}
else if (!swapped && decrease) {
int y = y0;
for (int i = x0; i <= x1; i++) {
img.set(i, y, color);
error += dy;
if (error >= dx) {
error -= dx;
y--;
}
}
}
else {
int y = y0;
for (int i = x0; i <= x1; i++) {
img.set(i, y, color);
error += dy;
if (error >= dx) {
error -= dx;
y++;
}
}
}
}
void hline(int x0, int x1, int y, Image& img, Color color) {
if (x1 < x0) {
std::swap(x0, x1);
}
for (int i = x0; i <= x1; i++) {
img.set(i, y, color);
}
}
void yline(int y0, int y1, int x, Image& img, Color color) {
if (y1 < y0) {
std::swap(y0, y1);
}
for (int i = y0; i <= y0; i++) {
img.set(x, i, color);
}
}
void triangle(int x0, int y0, int x1, int y1, int x2, int y2, Image& img, Color color) {
if (y0 > y1) {
std::swap(y0, y1);
std::swap(x0, x1);
}
if (y0 > y2) {
std::swap(y0, y2);
std::swap(x0, x2);
}
if (y1 > y2) {
std::swap(y1, y2);
std::swap(x1, x2);
}
double step1 = (double)(x1 - x0) / (double)(y1 - y0);
double step2 = (double)(x2 - x0) / (double)(y2 - y0);
double step3 = (double)(x2 - x1) / (double)(y2 - y1);
double side1 = x0;
double side2 = x0;
if (y0 != y1) {
for (int i = y0; i <= y1; i++) {
side1 += step1;
side2 += step2;
hline(side1, side2, i, img, color);
}
}
else {
side1 = x1;
}
if (y1 != y2) {
for (int i = y1; i <= y2; i++) {
side1 += step3;
side2 += step2;
hline(side1, side2, i, img, color);
}
}
}
void partial_rref(Eigen::MatrixXf& tbn, Eigen::MatrixXf& inv) {
if (tbn(0, 1) == 0) {
tbn.row(2) = tbn.row(1);
tbn.row(1) = tbn.row(0);
tbn.row(0) = tbn.row(2);
}
float factor = 1.f/tbn(0, 0);
tbn.row(0) *= factor;
inv.row(0) *= factor;
factor = tbn(1, 0) / tbn(0, 0);
tbn.row(1) -= tbn.row(0) * factor;
inv.row(1) -= inv.row(0) * factor;
factor = 1.f / tbn(1, 1);
tbn.row(1) *= factor;
inv.row(1) *= factor;
factor = tbn(0, 1) / tbn(1, 1);
tbn.row(0) -= tbn.row(1) * factor;
inv.row(0) -= inv.row(1) * factor;
}
void finish_rref(Eigen::MatrixXf& tbn, Eigen::MatrixXf& inv) {
float factor;
for (int i = 0; i < 2; i++) {
factor = tbn(2, i) / tbn(i, i);
tbn.row(2) -= tbn.row(i) * factor;
inv.row(2) -= inv.row(i) * factor;
}
factor = 1.f / tbn(2, 2);
tbn(2, 2) = 1.f;
inv.row(2) *= factor;
for (int i = 1; i > -1; i--) {
factor = tbn(i, 2) / tbn(2, 2);
tbn.row(i) -= tbn.row(2) * factor;
inv.row(i) -= inv.row(2) * factor;
}
}
void bary_triangle(Eigen::Vector3f v1, Eigen::Vector3f v2, Eigen::Vector3f v3, float* zbuffer, Image& img, Model& head,
std::array<Eigen::Vector3f, 3>& normals, Eigen::MatrixXf& texture_vertices, Eigen::Vector3f lighting) {
Eigen::Matrix2f T(2, 2);
T << v1[0] - v3[0], v2[0] - v3[0],
v1[1] - v3[1], v2[1] - v3[1];
Eigen::Matrix2f T_inv = T.inverse();
Eigen::Vector2f screen_v3 = { v3[0], v3[1] };
Eigen::Vector3f intensities;
for (int i = 0; i < 3; i++) {
intensities(i) = std::max(normals[i].dot(lighting), 0.f);
}
Eigen::Vector3f u_vector = { texture_vertices(0, 1) - texture_vertices(0, 0), texture_vertices(0, 2) - texture_vertices(0, 0), 0.f };
Eigen::Vector3f v_vector = { texture_vertices(1, 1) - texture_vertices(1, 0), texture_vertices(1, 2) - texture_vertices(1, 0), 0.f };
Eigen::MatrixXf tbn(3, 3);
tbn.row(0) = (v2 - v1).normalized();
tbn.row(1) = (v3 - v1).normalized();
tbn.row(2) = Eigen::Vector3f(0, 0, 0);
Image* texture = head.get_texture();
int tx_w = texture->get_width();
int tx_h = texture->get_height();
Image* normal_map = head.get_normal_map();
int xmin = std::max((int)std::min(v1[0], std::min(v2[0], v3[0])), 0);
int xmax = std::min((int)std::max(v1[0], std::max(v2[0], v3[0])), img.get_width());
int ymin = std::max((int)std::min(v1[1], std::min(v2[1], v3[1])), 0);
int ymax = std::min((int)std::max(v1[1], std::max(v2[1], v3[1])), img.get_height());
std::vector<std::array<float, 2>> y_params;
for (int i = xmin; i <= xmax; i++) {
std::array<float, 2> x_params = { 0, 0 };
x_params[0] = T_inv(0, 0) * (i - screen_v3[0]);
x_params[1] = T_inv(1, 0) * (i - screen_v3[0]);
for (int j = ymin; j <= ymax; j++) {
if (i == xmin) {
std::array<float, 2> y_comp = { 0, 0 };
y_comp[0] = T_inv(0, 1) * (j - screen_v3[1]);
y_comp[1] = T_inv(1, 1) * (j - screen_v3[1]);
y_params.push_back(y_comp);
}
Eigen::Vector2f params = { x_params[0] + y_params[j - ymin][0], x_params[1] + y_params[j - ymin][1] };
Eigen::Vector3f bary = { params(0), params(1), 1.f - params(0) - params(1) };
float zval = (bary(0) * v1[2]) + (bary(1) * v2[2]) + (bary(2) * v3[2]);
int z_idx = (j * img.get_width() + i);
if ((bary(0) >= 0 && bary(1) >= 0 && bary(2) >= 0) && (zbuffer[z_idx] < zval)) {
zbuffer[z_idx] = zval;
Eigen::Vector2f pos_ratios = texture_vertices * bary;
float light_ratio = intensities.dot(bary);
if (light_ratio <= 0) {
return;
}
Eigen::Vector3f tri_normal = { 0, 0, 0 };
for (int k = 0; k < 3; k++) {
tri_normal = tri_normal + (normals[k] * bary(k)); // turn this into matrix mult
}
int texture_x = pos_ratios(0) * tx_w;
int texture_y = pos_ratios(1) * tx_h;
Color texture_color = texture->get(texture_x, texture_y);
Eigen::Vector3f tan_normal = normal_map->get_normal(texture_x, texture_y);
tbn.row(2) = tri_normal.normalized();
Eigen::MatrixXf tbn_inv = tbn.inverse();
Eigen::MatrixXf basis(3, 3);
basis.col(0) = (tbn_inv * u_vector).normalized();
basis.col(1) = (tbn_inv * v_vector).normalized();
basis.col(2) = tri_normal.normalized();
Eigen::Vector3f n = (basis * tan_normal).normalized();
light_ratio = std::max(n.dot(lighting), 0.f);
Color lighted_color = texture_color * light_ratio;
//Color lighted_color = Color(255, 255, 255, 255) * light_ratio;
img.set(i, j, lighted_color);
}
}
}
}