-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
469 lines (369 loc) · 15.3 KB
/
main.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <iostream>
#include <opencv2/opencv.hpp>
#include "OBJ_Loader.h"
#include "Shader.hpp"
#include "Texture.hpp"
#include "Triangle.hpp"
#include "global.hpp"
#include "rasterizer.hpp"
Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos) {
Eigen::Matrix4f view = Eigen::Matrix4f::Identity();
Eigen::Matrix4f translate;
translate << 1, 0, 0, -eye_pos[0], 0, 1, 0, -eye_pos[1], 0, 0, 1, -eye_pos[2],
0, 0, 0, 1;
view = translate * view;
return view;
}
Eigen::Matrix4f get_model_matrix(float angle) {
Eigen::Matrix4f rotation;
angle = angle * MY_PI / 180.f;
rotation << cos(angle), 0, sin(angle), 0, 0, 1, 0, 0, -sin(angle), 0,
cos(angle), 0, 0, 0, 0, 1;
Eigen::Matrix4f scale;
scale << 2.5, 0, 0, 0, 0, 2.5, 0, 0, 0, 0, 2.5, 0, 0, 0, 0, 1;
Eigen::Matrix4f translate;
translate << 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1;
return translate * rotation * scale;
}
// 这里的zNear和zFar表示的是距离, 而非坐标
// 参考: https://zhuanlan.zhihu.com/p/509902950
// 参考: https://zhuanlan.zhihu.com/p/464638515 => OpenGL NDS是左手系(x, y, -z)
// 引用3D图形学书上的公式, games101的推导在正投影中z没有取反
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar) {
// Students will implement this function
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
assert(zNear > 0.0f);
assert(zFar > 0.0f);
// 修复中心对称问题
// 见ppt4, 有标注
// assert(zNear > zFar); // assert failed
// 也就是说, 这里我们这里并不是看向-z
float fovY = eye_fov / 180.0f * MY_PI;
float t = std::tan(fovY) * zNear;
float r = t * aspect_ratio;
/*
Eigen::Matrix4f trans;
// fix, 因为near和far我们看作距离, 所以他们的坐标是-near和-far
// 所以等于-1(-zNear + (-)(zFar)) / 2 = (zNear + zFar) / 2
trans << 1, 0, 0, 0, //
0, 1, 0, 0, //
0, 0, 1, -(zNear + zFar) / 2, //
0, 0, 0, 1; //
Eigen::Matrix4f scale;
// 注意这里是zNear - zFar
scale << 1 / r, 0, 0, 0, //
0, 1 / t, 0, 0, //
0, 0, 2 / (zNear - zFar), 0, //
0, 0, 0, 1; //
Eigen::Matrix4f PerspToOrtho;
// 注意第四行的参数是-1, 我们可以带入 矩阵A*(x, y, z, 1) = (x, y, z, 1)
// z=-n时, 第四行第三列只能是-1
PerspToOrtho << zNear, 0, 0, 0, //
0, zNear, 0, 0, //
0, 0, -(zNear + zFar), -zNear * zFar, //
0, 0, -1, 0; //
Eigen::Matrix4f mirror;
mirror << 1, 0, 0, 0, //
0, 1, 0, 0, //
0, 0, -1, 0, //
0, 0, 0, 1; //
projection = mirror * scale * trans * PerspToOrtho * projection;
*/
Eigen::Matrix4f PerspToOrtho;
Eigen::Matrix4f Ortho;
PerspToOrtho << zNear / r, 0, 0, 0, //
0, zNear / t, 0, 0, //
0, 0, -(zFar + zNear) / (zFar - zNear),
-2 * zNear * zFar / (zFar - zNear), //
0, 0, -1, 0; //
Ortho << 1, 0, 0, 0, //
0, 1, 0, 0, //
0, 0, -2 / (zFar - zNear), -(zFar + zNear) / (zFar - zNear), //
0, 0, 0, 1;
Eigen::Matrix4f mirror;
mirror << 1, 0, 0, 0, //
0, 1, 0, 0, //
0, 0, -1, 0, //
0, 0, 0, 1; //
return mirror * Ortho * PerspToOrtho;
}
Eigen::Vector3f vertex_shader(const vertex_shader_payload &payload) {
return payload.position;
}
Eigen::Vector3f normal_fragment_shader(const fragment_shader_payload &payload) {
Eigen::Vector3f return_color = (payload.normal.head<3>().normalized() +
Eigen::Vector3f(1.0f, 1.0f, 1.0f)) /
2.f;
Eigen::Vector3f result;
result << return_color.x() * 255, return_color.y() * 255,
return_color.z() * 255;
return result;
}
static Eigen::Vector3f reflect(const Eigen::Vector3f &vec,
const Eigen::Vector3f &axis) {
auto costheta = vec.dot(axis);
return (2 * costheta * axis - vec).normalized();
}
struct light {
Eigen::Vector3f position;
Eigen::Vector3f intensity;
};
Eigen::Vector3f
texture_fragment_shader(const fragment_shader_payload &payload) {
Eigen::Vector3f return_color = {0, 0, 0};
if (payload.texture) {
// TODO: Get the texture value at the texture coordinates of the current
// fragment
float u = payload.tex_coords[0], v = payload.tex_coords[1];
//return_color = payload.texture->getColor(u, v);
return_color = payload.texture->getBilinearColor(u, v);
std::cout << "return_color:" << return_color.x() << '\t' << return_color.y() << '\t'
<< return_color.z() << '\n';
}
Eigen::Vector3f texture_color;
texture_color << return_color.x(), return_color.y(), return_color.z();
Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
Eigen::Vector3f kd = texture_color / 255.f;
Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
auto l1 = light{{20, 20, 20}, {500, 500, 500}};
auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
std::vector<light> lights = {l1, l2};
Eigen::Vector3f amb_light_intensity{10, 10, 10};
Eigen::Vector3f eye_pos{0, 0, 10};
float p = 150;
// 而不是直接使用payload中的color
Eigen::Vector3f color = texture_color;
Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f normal = payload.normal;
Eigen::Vector3f result_color = {0, 0, 0};
for (auto &light : lights) {
// TODO: For each light source in the code, calculate what the *ambient*,
// *diffuse*, and *specular* components are. Then, accumulate that result on
// the *result_color* object.
// 模型是一样的, 只是输入不一样
Eigen::Vector3f light_dir = (light.position - point).normalized();
float r2 = (light.position - point).dot(light.position - point);
Eigen::Vector3f lightIntensity = light.intensity / r2;
// 注意符号
// 漫反射
Eigen::Vector3f diffuse = kd.cwiseProduct(lightIntensity);
diffuse *= std::max(0.0f, normal.dot(light_dir));
Eigen::Vector3f view = (eye_pos - point).normalized();
Eigen::Vector3f half_vector = (view + light_dir).normalized();
// 高光
Eigen::Vector3f specular = ks.cwiseProduct(lightIntensity);
specular *= std::pow(std::max(0.0f, normal.dot(half_vector)), p);
// 环境光
Eigen::Vector3f ambient = ka.cwiseProduct(amb_light_intensity);
// std::cout << diffuse.x() << '\t' << diffuse.y() << '\t' << diffuse.z()
// << '\n';
// std::cout << specular.x() << '\t' << specular.y() << '\t' << specular.z()
// << '\n';
// std::cout << ambient.x() << '\t' << ambient.y() << '\t' << ambient.z()
// << '\n';
result_color += (diffuse + specular + ambient);
}
return result_color * 255.f;
}
// 默认shader
Eigen::Vector3f phong_fragment_shader(const fragment_shader_payload &payload) {
Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
Eigen::Vector3f kd = payload.color;
Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
auto l1 = light{{20, 20, 20}, {500, 500, 500}};
auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
std::vector<light> lights = {l1, l2};
Eigen::Vector3f amb_light_intensity{10, 10, 10};
Eigen::Vector3f eye_pos{0, 0, 10};
float p = 150;
Eigen::Vector3f color = payload.color;
Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f normal = payload.normal;
Eigen::Vector3f result_color = {0, 0, 0};
for (auto &light : lights) {
// TODO: For each light source in the code, calculate what the *ambient*,
// *diffuse*, and *specular* components are. Then, accumulate that result on
// the *result_color* object.
Eigen::Vector3f light_dir = (light.position - point).normalized();
float r2 = (light.position - point).dot(light.position - point);
Eigen::Vector3f lightIntensity = light.intensity / r2;
// 注意符号
// 漫反射
Eigen::Vector3f diffuse = kd.cwiseProduct(lightIntensity);
diffuse *= std::max(0.0f, normal.dot(light_dir));
Eigen::Vector3f view = (eye_pos - point).normalized();
Eigen::Vector3f half_vector = (view + light_dir).normalized();
// 高光
Eigen::Vector3f specular = ks.cwiseProduct(lightIntensity);
specular *= std::pow(std::max(0.0f, normal.dot(half_vector)), p);
// 环境光
Eigen::Vector3f ambient = ka.cwiseProduct(amb_light_intensity);
// std::cout << diffuse.x() << '\t' << diffuse.y() << '\t' << diffuse.z()
// << '\n';
// std::cout << specular.x() << '\t' << specular.y() << '\t' << specular.z()
// << '\n';
// std::cout << ambient.x() << '\t' << ambient.y() << '\t' << ambient.z()
// << '\n';
result_color += (diffuse + specular + ambient);
}
return result_color * 255.f;
}
Eigen::Vector3f
displacement_fragment_shader(const fragment_shader_payload &payload) {
Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
Eigen::Vector3f kd = payload.color;
Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
auto l1 = light{{20, 20, 20}, {500, 500, 500}};
auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
std::vector<light> lights = {l1, l2};
Eigen::Vector3f amb_light_intensity{10, 10, 10};
Eigen::Vector3f eye_pos{0, 0, 10};
float p = 150;
Eigen::Vector3f color = payload.color;
Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f normal = payload.normal;
float kh = 0.2, kn = 0.1;
// TODO: Implement displacement mapping here
// Let n = normal = (x, y, z)
// Vector t = (x*y/sqrt(x*x+z*z),sqrt(x*x+z*z),z*y/sqrt(x*x+z*z))
// Vector b = n cross product t
// Matrix TBN = [t b n]
// dU = kh * kn * (h(u+1/w,v)-h(u,v))
// dV = kh * kn * (h(u,v+1/h)-h(u,v))
// Vector ln = (-dU, -dV, 1)
// Position p = p + kn * n * h(u,v)
// Normal n = normalize(TBN * ln)
Eigen::Vector3f result_color = {0, 0, 0};
for (auto &light : lights) {
// TODO: For each light source in the code, calculate what the *ambient*,
// *diffuse*, and *specular* components are. Then, accumulate that result on
// the *result_color* object.
}
return result_color * 255.f;
}
Eigen::Vector3f bump_fragment_shader(const fragment_shader_payload &payload) {
Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
Eigen::Vector3f kd = payload.color;
Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
auto l1 = light{{20, 20, 20}, {500, 500, 500}};
auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
std::vector<light> lights = {l1, l2};
Eigen::Vector3f amb_light_intensity{10, 10, 10};
Eigen::Vector3f eye_pos{0, 0, 10};
float p = 150;
Eigen::Vector3f color = payload.color;
Eigen::Vector3f point = payload.view_pos;
Eigen::Vector3f normal = payload.normal;
float kh = 0.2, kn = 0.1;
// TODO: Implement bump mapping here
// Let n = normal = (x, y, z)
// Vector t = (x*y/sqrt(x*x+z*z),sqrt(x*x+z*z),z*y/sqrt(x*x+z*z))
// Vector b = n cross product t
// Matrix TBN = [t b n]
// dU = kh * kn * (h(u+1/w,v)-h(u,v))
// dV = kh * kn * (h(u,v+1/h)-h(u,v))
// Vector ln = (-dU, -dV, 1)
// Normal n = normalize(TBN * ln)
Eigen::Vector3f result_color = {0, 0, 0};
result_color = normal;
return result_color * 255.f;
}
int main(int argc, const char **argv) {
std::vector<Triangle *> TriangleList;
float angle = 140.0;
bool command_line = false;
std::string filename = "output.png";
objl::Loader Loader;
//std::string obj_path = "../models/spot/";
// 避免bug使用绝对路径
std::string obj_path = "/home/er1c/code/games101_homework/Homework3/Assignment3/models/spot/";
// Load .obj File
bool loadout = Loader.LoadFile(obj_path + "spot_triangulated_good.obj");
for (auto mesh : Loader.LoadedMeshes) {
for (int i = 0; i < mesh.Vertices.size(); i += 3) {
Triangle *t = new Triangle();
for (int j = 0; j < 3; j++) {
t->setVertex(j, Vector4f(mesh.Vertices[i + j].Position.X,
mesh.Vertices[i + j].Position.Y,
mesh.Vertices[i + j].Position.Z, 1.0));
t->setNormal(j, Vector3f(mesh.Vertices[i + j].Normal.X,
mesh.Vertices[i + j].Normal.Y,
mesh.Vertices[i + j].Normal.Z));
t->setTexCoord(j, Vector2f(mesh.Vertices[i + j].TextureCoordinate.X,
mesh.Vertices[i + j].TextureCoordinate.Y));
}
TriangleList.push_back(t);
}
}
rst::rasterizer r(700, 700);
auto texture_path = "hmap.jpg";
r.set_texture(Texture(obj_path + texture_path));
// fragment_shader_payload是一个结构体(包括view pos、颜色、法线、texture
// coordinate、和texture信息)
std::function<Eigen::Vector3f(fragment_shader_payload)> active_shader =
phong_fragment_shader;
if (argc >= 2) {
command_line = true;
filename = std::string(argv[1]);
if (argc == 3 && std::string(argv[2]) == "texture") {
std::cout << "Rasterizing using the texture shader\n";
active_shader = texture_fragment_shader;
texture_path = "spot_texture.png";
r.set_texture(Texture(obj_path + texture_path));
} else if (argc == 3 && std::string(argv[2]) == "normal") {
std::cout << "Rasterizing using the normal shader\n";
active_shader = normal_fragment_shader;
} else if (argc == 3 && std::string(argv[2]) == "phong") {
std::cout << "Rasterizing using the phong shader\n";
active_shader = phong_fragment_shader;
} else if (argc == 3 && std::string(argv[2]) == "bump") {
std::cout << "Rasterizing using the bump shader\n";
active_shader = bump_fragment_shader;
} else if (argc == 3 && std::string(argv[2]) == "displacement") {
std::cout << "Rasterizing using the bump shader\n";
active_shader = displacement_fragment_shader;
}
}
Eigen::Vector3f eye_pos = {0, 0, 10};
r.set_vertex_shader(vertex_shader);
// 其实就是一个shader function.
r.set_fragment_shader(active_shader);
int key = 0;
int frame_count = 0;
if (command_line) {
r.clear(rst::Buffers::Color | rst::Buffers::Depth);
r.set_model(get_model_matrix(angle));
r.set_view(get_view_matrix(eye_pos));
r.set_projection(get_projection_matrix(45.0, 1, 0.1, 50));
r.draw(TriangleList);
cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
image.convertTo(image, CV_8UC3, 1.0f);
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
cv::imwrite(filename, image);
return 0;
}
while (key != 27) {
r.clear(rst::Buffers::Color | rst::Buffers::Depth);
r.set_model(get_model_matrix(angle));
r.set_view(get_view_matrix(eye_pos));
r.set_projection(get_projection_matrix(45.0, 1, 0.1, 50));
// r.draw(pos_id, ind_id, col_id, rst::Primitive::Triangle);
r.draw(TriangleList);
cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
image.convertTo(image, CV_8UC3, 1.0f);
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
cv::imshow("image", image);
cv::imwrite(filename, image);
key = cv::waitKey(10);
if (key == 'a') {
angle -= 0.1;
} else if (key == 'd') {
angle += 0.1;
}
}
return 0;
}