-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTracing.cpp
More file actions
395 lines (321 loc) · 14 KB
/
RayTracing.cpp
File metadata and controls
395 lines (321 loc) · 14 KB
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
/* ppm 格式测试
#include <iostream>
int main() {
// Image
const int image_width = 256;
const int image_height = 256;
// Render
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
for (int j = image_height - 1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush; // 输出进度
for (int i = 0; i < image_width; ++i) {
auto r = double(i) / (image_width - 1);
auto g = double(j) / (image_height - 1);
auto b = 0.25;
int ir = static_cast<int>(255.999 * r);
int ig = static_cast<int>(255.999 * g);
int ib = static_cast<int>(255.999 * b);
std::cout << ir << ' ' << ig << ' ' << ib << '\n';
}
}
}
*/
/*
#include "vec3.h"
#include "color.h"
int main(void)
{
// image_size
const int width = 256;
const int height = 256;
// render
std::cout << "P3\n" << width << ' ' << height << "\n255\n"; // ppm文件头
for (int j = height - 1; j >= 0; j--)
{
std::cerr << "\rScanlines remaining" << j << ' ' << std::flush;
for (int i = 0; i < width; i++)
{
color pixel_color(static_cast<double>(i) / (width - 1), static_cast<double>(j) / (height - 1), 0.25);
write_color(std::cout, pixel_color);
}
}
std::cerr << "\nDone.\n";
return 0;
}
*/
/* 初步光追:检测到击中物体就返回红色
#include "color.h"
#include "ray.h"
#include "vec3.h"
#include <iostream>
bool hit_sphere(const point3 ¢er, double radius, const ray &r) {
vec3 oc = r.origin() - center;
auto a = dot(r.direction(), r.direction());
auto b = 2.0 * dot(oc, r.direction());
auto c = dot(oc, oc) - radius * radius;
auto discriminant = b * b - 4 * a * c;
return (discriminant > 0);
}
color ray_color(const ray &r) {
if (hit_sphere(point3(0, 0, -1), 0.5, r)) // 检查是否击中圆。圆就位于视口的中间
return color(1, 0, 0);
vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0); // 如无击中物体,返回光线默认颜色
}
int main() {
// Image
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400; // 图像的宽
const int image_height = static_cast<int>(image_width / aspect_ratio);
// Camera
auto viewport_height = 2.0; // 视口的高
auto viewport_width = aspect_ratio * viewport_height;
auto focal_length = 1.0; // 视口的深度
auto origin = point3(0, 0, 0);
auto horizontal = vec3(viewport_width, 0, 0);
auto vertical = vec3(0, viewport_height, 0);
auto lower_left_corner = origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length); // 计算左下角坐标
// Render
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
for (int j = image_height - 1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for (int i = 0; i < image_width; ++i) {
auto u = double(i) / (image_width - 1); // 计算原图像中该点相对于整个图像宽高的比例
auto v = double(j) / (image_height - 1);
ray r(origin, lower_left_corner + u * horizontal + v * vertical - origin); // 用origin和direction定义光线。 暂时是一条无限长的光线。
color pixel_color = ray_color(r);
write_color(std::cout, pixel_color); // 输出整幅图的图像
}
}
std::cerr << "\nDone.\n";
}
*/
/* 初步实现光追和返回法线颜色
#include "color.h"
#include "ray.h"
#include "vec3.h"
#include <iostream>
//double hit_sphere(const point3 ¢er, double radius, const ray &r)
//{
// vec3 oc = r.origin() - center;
// double a = r.direction().length_square();
// double b = 2 * dot(oc, r.direction());
// double c = dot(oc, oc) - std::pow(radius, 2);
// double discriminate = b * b - 4 * a * c;
// if (discriminate < 0)
// return -1.0;
// else
// return (-b - std::sqrt(discriminate)) / (2 * a);
//}
// 计算的优化
double hit_sphere(const point3 ¢er, double radius, const ray &r) {
vec3 oc = r.origin() - center;
auto a = r.direction().length_square();
auto half_b = dot(oc, r.direction());
auto c = oc.length_square() - radius * radius;
auto discriminant = half_b * half_b - a * c;
if (discriminant < 0) {
return -1.0;
}
else {
return (-half_b - sqrt(discriminant)) / a;
}
}
color ray_color(ray r)
{
double t = hit_sphere(point3(0, 0, -1), 0.5, r); // 获得第一个击中点
if (t > 0)
{
vec3 normal = unit_vector(r.at(t) - vec3(0, 0, -1)); // normal // 从球心往击中点指的向量,返回后用颜色代表
return 0.5 * color(normal.x() + 1.0, normal.y() + 1.0, normal.z() + 1.0); // x, y, z归一化 // 与球的半径有关
}
vec3 unit_dir = unit_vector(r.direction());
t = 0.5 * (unit_dir.y() + 1.0); // 归一化
color rayColor = (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0); // 插值
return rayColor;
}
int main() {
// Image
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400; // 图像的宽
const int image_height = static_cast<int>(image_width / aspect_ratio);
// Camera
auto viewport_height = 2.0; // 视口的高
auto viewport_width = aspect_ratio * viewport_height;
auto focal_length = 1.0; // 视口的深度
auto origin = point3(0, 0, 0);
auto horizontal = vec3(viewport_width, 0, 0);
auto vertical = vec3(0, viewport_height, 0);
auto lower_left_corner = origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length); // 计算左下角坐标
// Render
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
for (int j = image_height - 1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for (int i = 0; i < image_width; ++i) {
auto u = double(i) / (image_width - 1); // 计算原图像中该点相对于整个图像宽高的比例
auto v = double(j) / (image_height - 1);
ray r(origin, lower_left_corner + u * horizontal + v * vertical - origin); // 用origin和direction定义光线。 暂时是一条无限长的光线。
color pixel_color = ray_color(r);
write_color(std::cout, pixel_color); // 输出整幅图的图像
}
}
std::cerr << "\nDone.\n";
}
*/
//
//// 第一次阶段结束,代码已规范
//// https://blog.csdn.net/qq_43419761/article/details/127965241
//#include "rtweekend.h"
//#include "color.h"
//#include "hittable_list.h"
//#include "sphere.h"
//
//#include <iostream>
//color ray_color(const ray &r, const hittable &world) { // 更新的地方
// hit_record rec;
// if (world.hit(r, 0, infinity, rec)) {
// return 0.5 * (rec.normal + color(1, 1, 1)); // 返回法线
// // return color(1, 1, 1);
// }
// vec3 unit_direction = unit_vector(r.direction());
// auto t = 0.5 * (unit_direction.y() + 1.0);
// return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
//}
//
//int main() {
//
// // Image
//
// const auto aspect_ratio = 16.0 / 9.0;
// const int image_width = 400;
// const int image_height = static_cast<int>(image_width / aspect_ratio);
//
// // World 更新的地方
// hittable_list world; // hittable_list 构建出世界上所有物体
// world.add(make_shared<sphere>(point3(0, 0, -1), 0.5));
// world.add(make_shared<sphere>(point3(0, -100.5, -1), 100));
//
// // Camera
//
// auto viewport_height = 2.0;
// auto viewport_width = aspect_ratio * viewport_height;
// auto focal_length = 1.0;
//
// auto origin = point3(0, 0, 0);
// auto horizontal = vec3(viewport_width, 0, 0);
// auto vertical = vec3(0, viewport_height, 0);
// auto lower_left_corner = origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length);
//
// // Render
//
// std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
//
// for (int j = image_height - 1; j >= 0; --j) {
// std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
// for (int i = 0; i < image_width; ++i) {
// auto u = double(i) / (image_width - 1);
// auto v = double(j) / (image_height - 1);
// ray r(origin, lower_left_corner + u * horizontal + v * vertical);
// color pixel_color = ray_color(r, world); // 更新的地方
// write_color(std::cout, pixel_color);
// }
// }
//
// std::cerr << "\nDone.\n";
//}
#include "camera.h"
#include "hittable_list.h"
#include "sphere.h"
#include "color.h"
#include "rtweekend.h"
#include "material.h"
#include <iostream>
// 对每条射线执行递归
color ray_color(const ray &r, const hittable &world, int depth) {
hit_record rec;
// If we've exceeded the ray bounce limit, no more light is gathered. // 反射次数设置
if (depth <= 0)
return color(0, 0, 0);
if (world.hit(r, 0.001, infinity, rec)) { //修正错误的阴影
// point3 target = rec.p + rec.normal + random_unit_vector(); // 得到球上一点(兰伯特反射)
//// 反射,每次反射的光只剩下一半 // 阴影增强
//if (depth < 50) {
// return 0.2 * ray_color(ray(rec.p, target - rec.p), world, depth - 1); // target - rec.p 为漫反射后方向
//}
// 每次反射衰减0.5
//return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth - 1); // target - rec.p 为漫反射后方向
ray scattered;
color attenuation;
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered)) // attenuation 衰减因子(与物体颜色相关), scattered 反射光线
return attenuation * ray_color(scattered, world, depth - 1); // 无发光
// return attenuation * ray_color(scattered, world, depth - 1) + 0.3 * color(1,1,1); // 模拟自发光,并且反射得越多的地方越光(简陋版)
return color(0, 0, 0);
}
// 不再碰撞物体,返回环境光
vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0); // 把变量t控制在0-1的变化范围
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0); // 这个相当于全环境光源
// return 0.5 * ((1.0 - t) * color(1.0, 1.0, 1.0) + t * color(1.0, 0.7, 1.0)); // 自己改变总环境光
// return color(0, 0, 0); //全黑
}
int main() {
// Image
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400;
const int image_height = static_cast<int>(image_width / aspect_ratio);
// 单个像素发射的光线数
const int samples_per_pixel = 100;
// 光线的最大长度
const int max_depth = 50;
// World
hittable_list world;
//auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0)); // 这里修改,添加了物体和材质,参数是颜色
//auto material_center = make_shared<lambertian>(color(0.7, 0.3, 0.3));
//auto material_left = make_shared<metal>(color(0.8, 0.8, 0.8), 0.2);
//auto material_right = make_shared<metal>(color(0.8, 0.6, 0.2), 0.8);
//auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
//auto material_center = make_shared<dielectric>(1.5);
//auto material_left = make_shared<dielectric>(1.5);
//auto material_right = make_shared<metal>(color(0.8, 0.6, 0.2), 1.0);
auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0)); // 地面的球
auto material_center = make_shared<lambertian>(color(0.1, 0.2, 0.5)); // 中间的球——漫反射
auto material_left = make_shared<dielectric>(1.5); // 左边的球——折射
auto material_right = make_shared<metal>(color(0.8, 0.6, 0.2), 0.0); // 右边的球——镜面反射
//world.add(make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
//world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
//world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
//world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
world.add(make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), -0.4, material_left)); // 此处是中空玻璃球
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
// Camera
// camera cam; 旧版
// camera cam(90.0, aspect_ratio); 旧版2
// 定义摄像机,参数的含义分别是lookfrom,lookat,vup(基本固定),fov,宽高比
// camera cam(point3(-2, 2, 1), point3(0, 0, -1), vec3(0, 1, 0), 20, aspect_ratio);
point3 lookfrom(3, 3, 2);
point3 lookat(0, 0, -1); // 这个位置是中间球的位置
vec3 vup(0, 1, 0);
auto dist_to_focus = (lookfrom - lookat).length(); // 在这个距离下最清晰
auto aperture = 0.1; // 光圈大小(默认为2,效果过强)
camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
// Render
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
for (int j = image_height - 1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for (int i = 0; i < image_width; ++i) {
color pixel_color(0, 0, 0);
for (int s = 0; s < samples_per_pixel; ++s) {
auto u = (i + random_double()) / (image_width - 1); // 视口大小没变,是视口内的像素变多了
auto v = (j + random_double()) / (image_height - 1);
ray r = cam.get_ray(u, v); // 需要发射的射线
pixel_color += ray_color(r, world, max_depth);
}
write_color(std::cout, pixel_color, samples_per_pixel);
}
}
std::cerr << "\nDone.\n";
}