-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverscene-2.cc
161 lines (136 loc) · 5.49 KB
/
coverscene-2.cc
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
#include <iostream>
#include <cfloat>
#include <thread>
#include "headers/hittable_list.h"
#include "headers/camera.h"
#include "headers/bvh_node.h"
#include "headers/objects/sphere.h"
#include "headers/objects/moving_sphere.h"
#include "headers/objects/xy_rect.h"
#include "headers/objects/xz_rect.h"
#include "headers/objects/yz_rect.h"
#include "headers/objects/flip_normals.h"
#include "headers/objects/box.h"
#include "headers/objects/translate.h"
#include "headers/objects/rotate_y.h"
#include "headers/objects/constant_medium.h"
#include "headers/textures/constant_texture.h"
#include "headers/textures/noise_texture.h"
#include "headers/textures/image_texture.h"
#include "headers/materials/diffuse_light.h"
#include "headers/materials/dielectric.h"
#include "headers/materials/lambertian.h"
#include "headers/materials/metal.h"
#define STB_IMAGE_IMPLEMENTATION
#include "lib/stb_image.h"
hittable *coverscene_2 () {
int nb = 20;
hittable **list = new hittable*[30];
hittable **boxlist = new hittable*[10000];
hittable **boxlist2 = new hittable*[10000];
material *white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73)));
material *ground = new lambertian(new constant_texture(vec3(0.48, 0.83, 0.53)));
int b = 0;
for (int i = 0; i < nb; i++)
for (int j = 0; j < nb; j++) {
double w = 100;
double x0 = -1000 + i * w;
double y0 = 0;
double z0 = -1000 + j * w;
double x1 = x0 + w;
double y1 = 100 * (drand48() + 0.01);
double z1 = z0 + w;
boxlist[b++] = new box(vec3(x0, y0, z0), vec3(x1, y1, z1), ground);
}
int l = 0;
list[l++] = new bvh_node(boxlist, b, 0, 1);
material *light = new diffuse_light(new constant_texture(vec3(3.7, 3.7, 3.7)));
list[l++] = new xz_rect(123, 423, 147, 412, 554, light);
vec3 centre(400, 400, 200);
list[l++] = new moving_sphere(centre, centre + vec3(30, 0, 0), 0, 1, 50, new lambertian(new constant_texture(vec3(0.7, 0.3, 0.1))));
list[l++] = new sphere(vec3(260, 150, 45), 50, new dielectric(1.5));
list[l++] = new sphere(vec3(0, 150, 145), 50, new metal(vec3(0.8, 0.8, 0.9), 10.0));
hittable *boundary = new sphere(vec3(360, 150, 145), 70, new dielectric(1.5));
list[l++] = boundary;
list[l++] = new constant_medium(boundary, 0.2, new constant_texture(vec3(0.2, 0.4, 0.9)));
boundary = new sphere(vec3(0, 0, 0), 5000, new dielectric(1.5));
list[l++] = new constant_medium(boundary, 0.0001, new constant_texture(vec3(1.0, 1.0, 1.0)));
int nx, ny, nn;
unsigned char *earth_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0);
material *earth_mat = new lambertian(new image_texture(earth_data, nx, ny));
list[l++] = new sphere(vec3(400, 200, 400), 100, earth_mat);
texture *noitext = new noise_texture(0.1);
list[l++] = new sphere(vec3(220, 280, 300), 80, new lambertian(noitext));
int ns = 1000;
for (int j = 0; j < ns; j++)
boxlist2[j] = new sphere(vec3(165 * drand48(), 165 * drand48(), 165 * drand48()), 10, white);
list[l++] = new translate(new rotate_y(new bvh_node(boxlist2, ns, 0.0, 1.0), 15), vec3(-100, 270, 395));
return new hittable_list(list, l);
}
vec3 colour (const ray& r, hittable *world, int depth) {
hit_record rec;
if (world -> hit(r, 0.001, MAXFLOAT, rec)) {
ray scattered;
vec3 attenuation;
vec3 emitted = rec.mat_ptr -> emitted(rec.u, rec.v, rec.p);
if ( (depth < 50) && (rec.mat_ptr -> scatter(r, rec, attenuation, scattered)) )
return emitted + attenuation * colour(scattered, world, depth + 1);
else return emitted;
} else return vec3(0, 0, 0);
}
void renderer (int nx, int ny, int ns, int j_max, int j_min, int i_min, int i_max, camera cam, hittable *world, int ***output_img) {
for (int j = j_max; j >= j_min; j--) {
output_img[j] = new int* [i_max - i_min + 1];
for (int i = i_min; i <= i_max; 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);
vec3 p = r.point_at_parameter(2.0);
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]);
if (ir > 255) ir = 255;
if (ig > 255) ig = 255;
if (ib > 255) ib = 255;
output_img[j][i] = new int [3];
output_img[j][i][0] = ir;
output_img[j][i][1] = ig;
output_img[j][i][2] = ib;
}
}
}
int main () {
freopen("coverscene-2.ppm", "w", stdout);
int nx = 1000;
int ny = 1000;
int ns = 10000;
int max_thread = 8; // please note: max_thread shall always be a divisor of ny
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
vec3 look_from(278, 278, -800);
vec3 look_at(278, 278, 0);
//double dist = (look_from - look_at).length();
double dist = 10.0;
double aperture = 0.0;
double fov = 40.0;
camera cam(look_from, look_at, vec3(0.0, 1.0, 0.0), fov, double(nx) / double(ny), aperture, dist, 0.0, 1.0);
double R = cos(M_PI / 4);
hittable *world = coverscene_2();
int ***output_img = new int** [ny];
int thread_interval = ny / max_thread;
std::thread threads[max_thread];
int thread_counter = 0;
for (int i = 0; i < max_thread; i++)
threads[i] = std::thread(renderer, nx, ny, ns, (i + 1) * thread_interval - 1, i * thread_interval, 0, nx - 1, cam, world, output_img);
for (int i = 0; i < max_thread; i++) threads[i].join();
for (int j = ny - 1; j >= 0; j--)
for (int i = 0; i < nx; i++)
std::cout << output_img[j][i][0] << " " << output_img[j][i][1] << " " << output_img[j][i][2] << std::endl;
fclose(stdout);
return 0;
}