-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cc
71 lines (56 loc) · 1.92 KB
/
camera.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
#include <iostream>
#include <cfloat>
#include "sphere.h"
#include "hittablelist.h"
#include "camera.h"
#include "lambertian.h"
#include "metal.h"
#include "dielectric.h"
vec3 colour (const ray& r, hittable *world, int depth) {
hit_record rec;
if (world -> hit(r, 0.001, MAXFLOAT, 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);
}
}
int main () {
freopen("camera.ppm", "w", stdout);
int nx = 2000;
int ny = 1000;
int ns = 100;
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
camera cam(vec3(-2, 2, 1), vec3(0, 0, -1), vec3(0, 1, 0), 90, double(nx) / double(ny));
double R = cos(M_PI / 4);
hittable *list[4];
list[0] = new sphere(vec3(0, 0, -1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5)));
list[1] = new sphere(vec3(0, -100.5, -1), 100, new lambertian(vec3(0.8, 0.8, 0.0)));
list[2] = new sphere(vec3(1, 0, -1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.2));
list[3] = new sphere(vec3(-1, 0, -1), 0.5, new dielectric(1.5));
hittable *world = new hittable_list(list, 4);
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);
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]);
std::cout << ir << " " << ig << " " << ib << "\n";
}
fclose(stdout);
return 0;
}