-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (133 loc) · 5.16 KB
/
main.cpp
File metadata and controls
152 lines (133 loc) · 5.16 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
#include "Object.h"
#include "Ray.h"
#include "Camera.h"
#include "Light.h"
#include "RGB.h"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <memory>
#include <write_ppm.h>
#include <Intersection.h>
#include <PointLight.h>
#include <lambertian_reflection.h>
#include <ctime>
#include <chrono>
#include <random>
#include <WaveFrontParser.h>
#include <Plane.h>
#include <sampling.h>
#include <AreaLight.h>
#include <Sphere.h>
void initialize_scene();
using Eigen::Vector3d;
using std::max;
using std::min;
using std::vector;
using std::shared_ptr;
using Eigen::Matrix3d;
vector<shared_ptr<Object>> scene_objects;
vector<shared_ptr<Light>> lights;
shared_ptr<Sampler> sampler(new Sampler());
vector<shared_ptr<rgb>> texture;
vector<shared_ptr<Vector3d>> normal_map;
double nb_intersections = 0;
double a = 1.0;
int main(int argc, char * argv[]) {
const unsigned int h_res = 1920;
const unsigned int v_res = 1080;
const float s = 0.003125;
const unsigned plane_offset = 12;
const double gamma = 2.2;
const unsigned sample_rate = 64;
const double N = pow(sample_rate,2);
const double delta = 1.0 / sample_rate;
std::clock_t c_start = std::clock();
auto t_start = std::chrono::high_resolution_clock::now();
Camera camera = Camera(Vector3d(-0.5,3,-8),
Vector3d(-0.5,0.5,0),
plane_offset,
(double) h_res, (double) v_res,
s);
initialize_scene();
vector<unsigned char> rgb_image(3 * h_res * v_res);
vector<double> temp(3 * h_res * v_res);
int rows_done = 0;
#pragma omp parallel for schedule(dynamic, 1)
for (int r = 0; r < static_cast<int>(v_res); ++r) {
for (unsigned c = 0; c < h_res; ++c) {
rgb L = rgb(0,0,0);
rgb pigment = rgb(1, 0.98, 0.94);
for (unsigned k = 0; k < sample_rate; ++k) {
for (unsigned l = 0; l < sample_rate; ++l) {
const double dr = (sampler->random() + k) * delta;
const double dc = (sampler->random() + l) * delta;
Ray ray;
camera.shoot_ray(c + dc, r + dr, ray);
L += Lo(ray);
}
}
const rgb color = L * pigment / N;
temp[0 + 3 * (c + h_res * (v_res - r - 1))] = color.r;
temp[1 + 3 * (c + h_res * (v_res - r - 1))] = color.g;
temp[2 + 3 * (c + h_res * (v_res - r - 1))] = color.b;
}
int done;
#pragma omp atomic capture
done = ++rows_done;
#pragma omp critical
{
std::cout << "percentage: " << (static_cast<double>(done) / v_res) << "\n";
}
}
auto clamp = [](double s) { return max(min(s, 1.0), 0.0); };
const double max_radiance = *std::max_element(temp.begin(), temp.end());
const double inv_max_radiance = (max_radiance > 1e-12) ? (1.0 / max_radiance) : 0.0;
for (unsigned i = 0; i < rgb_image.size() / 3; ++i) {
rgb_image[0 + i * 3] = 255.0 * pow(clamp(temp[0 + i * 3] * inv_max_radiance), 1 / gamma);
rgb_image[1 + i * 3] = 255.0 * pow(clamp(temp[1 + i * 3] * inv_max_radiance), 1 / gamma);
rgb_image[2 + i * 3] = 255.0 * pow(clamp(temp[2 + i * 3] * inv_max_radiance), 1 / gamma);
}
std::clock_t c_end = std::clock();
auto t_end = std::chrono::high_resolution_clock::now();
std::cout << std::fixed << "CPU time used: "
<< 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"
<< "Wall clock time passed: "
<< std::chrono::duration<double, std::milli>(t_end-t_start).count()
<< " ms\n";
std::string filename = "CGDemo";
write_ppm(filename, rgb_image, h_res, v_res);
write_txt(nb_intersections);
}
void initialize_scene() {
shared_ptr<BVH> teapot;
if (!parseWaveFrontFile("../data/teapot.obj", teapot) || !teapot) {
throw std::runtime_error("Failed to load ../data/teapot.obj");
}
shared_ptr<Material> mat_tea(new Material);
teapot->material = mat_tea;
teapot->material->ks = rgb(1,1,1);
teapot->material->kt = rgb(1,1,1);
teapot->material->nu = 1.5;
scene_objects.push_back(teapot);
shared_ptr<Material> mat_white(new Material);
mat_white->kd = rgb(1,1,1);
mat_white->ks = rgb(0,0,0);
shared_ptr<Plane> floor(new Plane());
floor->point = Vector3d(0,0,0);
floor->normal = Vector3d(0,1,0);
floor->material = mat_white;
scene_objects.push_back(floor);
shared_ptr<Plane> left_wall(new Plane());
left_wall->point = Vector3d(-5,0,0);
left_wall->normal = Vector3d(1,0,0);
left_wall->material = mat_white;
scene_objects.push_back(left_wall);
shared_ptr<AreaLight> areaLight1(new AreaLight(Vector3d(5, 0*a, -1.5*a),
Vector3d(5, 3*a, -1.5*a),
Vector3d(5, 0*a, 1.5*a)));
areaLight1->material = mat_white;
scene_objects.push_back(areaLight1);
lights.push_back(areaLight1);
}