-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
110 lines (96 loc) · 3.02 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
#include <float.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "sphere.h"
#include "hitable_list.h"
#include "camera.h"
#include "material.h"
vec3 colour(const ray& r, hitable *world, int depth) {
hit_record rec;
if (world->hit(r, 0.001, DBL_MAX, rec)) {
ray scattered;
vec3 attenuation;
if (depth < 50 && rec.mat_ptr->scatter(r, rec, attenuation, scattered)) {
return attenuation*colour(scattered, world, depth+1);
}
else {
return vec3(0,0,0);
}
}
else {
vec3 unit_direction = unit_vector(r.direction());
double t = 0.5*(unit_direction.y() + 1.0);
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
}
}
hitable *random_scene() {
int n = 500;
hitable **list = new hitable*[n+1];
list[0] = new sphere(vec3(0,-1000,0),1000,
std::make_shared<lambertian>(vec3(0.5,0.5,0.5)));
int i = 1;
for (int a = -11; a < 11; a++) {
for (int b = -11; b < 11; b++) {
double choose_mat = drand48();
vec3 center(a+0.9*drand48(),0.2,b+0.9*drand48());
if ((center-vec3(4,0.2,0)).length() > 0.9) {
if (choose_mat < 0.8) {
list[i++] = new sphere(center, 0.2,
std::make_shared<lambertian>(vec3(drand48()*drand48(),
drand48()*drand48(), drand48()*drand48())));
}
else if (choose_mat < 0.95) {
list[i++] = new sphere(center, 0.2,
std::make_shared<metal>(vec3(0.5*(1 + drand48()),
0.5*(1 + drand48()), 0.5*(1 + drand48())),
0.5*drand48()));
}
else {
list[i++] = new sphere(center, 0.2,
std::make_shared<dielectric>(1.5));
}
}
}
}
list[i++] = new sphere(vec3(4,1,0), 1.0,
std::make_shared<dielectric>(1.5));
list[i++] = new sphere(vec3(-2,1,0), 1.0,
std::make_shared<lambertian>(vec3(0.4,0.2,0.1)));
list[i++] = new sphere(vec3(0,1,0), 1.0,
std::make_shared<metal>(vec3(0.7,0.6,0.5), 0.0));
return new hitable_list(list,i);
}
int main() {
int nx = 640;
int ny = 360;
int ns = 3;
char data [3*nx*ny];
int imgPos = 0;
hitable *world = random_scene();
vec3 lookfrom(13,5,3);
vec3 lookat(0,0,0);
double dist_to_focus = 10.0; //(lookfrom-lookat).length();
double aperture = 0.1;
camera cam(lookfrom, lookat, vec3(0,1,0), 20,
double(nx)/double(ny), aperture, dist_to_focus);
for (int j = ny-1; j >= 0; j--) {
for (int i = 0; i < nx; i++) {
vec3 col(0, 0, 0);
for (int s=0; s < ns; s++) {
double u = double(i + drand48()) / double(nx);
double v = double(j + drand48()) / double(ny);
ray r = cam.get_ray(u, v);
col += colour(r, world,0);
}
col /= double(ns);
col = vec3( sqrt(col[0]), sqrt(col[1]), sqrt(col[2]) );
int ir = int(255.99*col[0]);
int ig = int(255.99*col[1]);
int ib = int(255.99*col[2]);
data[imgPos++] = ir;
data[imgPos++] = ig;
data[imgPos++] = ib;
}
}
stbi_write_png("out.png", nx, ny, 3, data, nx*3);
}