forked from DanielQ-51/cpp-path-tracer-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
102 lines (79 loc) · 4.15 KB
/
Copy pathmain.cpp
File metadata and controls
102 lines (79 loc) · 4.15 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
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "settings.h"
#include <iostream>
#include <vector>
#include <chrono>
#include "utils.h"
#include "render.h"
#include "camera.h"
#include "imageUtils.h"
#include "softwareBVH.h"
int main() {
const int w = WIDTH;
const int h = HEIGHT;
// ------------------------------- Setting up --------------------------------------------
std::vector<unsigned char> outImageBuffer(w * h * 3);
std::vector<float3> image(w * h);
Camera camera = Camera::Pinhole(
f3(CAMERA_X_POS, CAMERA_Y_POS, CAMERA_Z_POS), // camera origin/position
w, h, // width and height of the image
CAMERA_X_ROT, CAMERA_Y_ROT, CAMERA_Z_ROT, // xyz rotations of the camera
30.0f, // FOV (degrees)
1.0f // subpixel jitter (in terms of pixels)
);
std::vector<Triangle> mesh;
std::vector<float3> vertex_positions;
std::vector<float3> vertex_normals;
// ----------------------------------------------------------------------------------------------------------------------
// Use this section to customize your scene with models exported from Maya
// ----------------------------------------------------------------------------------------------------------------------
readObjSimple("meshes/cornell_box_no_walls.obj", vertex_positions, vertex_normals, mesh, f3(0.9f, 0.9f, 0.9f), f3(0.0f), TYPE_DIFFUSE);
readObjSimple("meshes/cornell_box_left_wall.obj", vertex_positions, vertex_normals, mesh, f3(0.9f, 0.1f, 0.1f), f3(0.0f), TYPE_DIFFUSE);
readObjSimple("meshes/cornell_box_right_wall.obj", vertex_positions, vertex_normals, mesh, f3(0.1f, 0.9f, 0.1f), f3(0.0f), TYPE_DIFFUSE);
readObjSimple("meshes/cornell_box_left_box.obj", vertex_positions, vertex_normals, mesh, f3(0.9f, 0.9f, 0.9f), f3(0.0f), TYPE_DIFFUSE);
readObjSimple("meshes/cornell_box_right_box.obj", vertex_positions, vertex_normals, mesh, f3(0.9f, 0.9f, 0.9f), f3(0.0f), TYPE_DIFFUSE);
readObjSimple("meshes/cornell_box_large_light.obj", vertex_positions, vertex_normals, mesh, f3(0.8f, 0.8f, 0.8f), f3(18.0f, 18.0f, 12.0f), TYPE_DIFFUSE);
//readObjSimple("meshes/cornell_box_small_light.obj", vertex_positions, vertex_normals, mesh, f3(0.8f, 0.8f, 0.8f), f3(240.0f, 240.0f, 160.0f), TYPE_DIFFUSE);
// ----------------------------------------------------------------------------------------------------------------------
printf("The scene has %d triangles\n", mesh.size());
// ---------------------------------- Building Acceleration Structure (Ignore this for now!) --------------------------------------------
BVH bvh;
#if USE_BVH == 1
std::vector<float3> centroids;
std::vector<float3> aabbMins;
std::vector<float3> aabbMaxes;
computeInfoForBVH(vertex_positions, mesh, centroids, aabbMins, aabbMaxes, bvh.indices);
buildBVH(bvh, centroids, aabbMins, aabbMaxes, 0, bvh.indices.size(), 3);
#endif
auto renderStartTime = std::chrono::steady_clock::now();
printf("Starting Render\n");
for (int sample = 0; sample < NUM_SAMPLE; sample++) {
#pragma omp parallel for collapse(2) schedule(dynamic, 16)
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
renderPixel(
sample,
x, y,
w, h,
BOUNCE_DEPTH,
camera,
vertex_positions,
vertex_normals,
mesh,
image,
bvh // dont worry about this one for now
);
}
}
printf("\rProgress: [Sample %d / %d]", sample + 1, NUM_SAMPLE);
fflush(stdout);
if ((sample + 1) % SAVE_INTERVAL == 0 || sample == NUM_SAMPLE - 1) {
postProcessImage(image, outImageBuffer, w, h, sample);
saveImage(outImageBuffer, w, h, "render.png");
}
}
auto currentTime = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> elapsed = currentTime - renderStartTime;
printf("\nRender finished in %f ms\n", elapsed.count());
return 0;
}