-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
183 lines (145 loc) · 4.11 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// This file is currently used to test the stuff I add, so it will not contain anything smart
#include "Types.h"
#include "Raytracing.h"
#include "Pathtracing.h"
#include "BMP_export.cpp"
#include <cstdio>
#include <ctime>
#include <cstring>
#include "Renderer.h"
#define PI_S 3.14159265359
using namespace pathtracing;
// MUST BE DEVISABLE BY 4
const int width = 512;
const int height = 512;
col* pixels;
inline scalar clamp(scalar x, scalar mn, scalar mx) {
if (x > mx) return mx;
if (x < mn) return mn;
return x;
}
inline col from_light(vec3 light) {
const vec3 l2 = {
clamp(light.x, 0.0, 1.0),
clamp(light.y, 0.0, 1.0),
clamp(light.z, 0.0, 1.0),
};
return {(char)(l2.x*255), (char)(l2.y*255), (char)(l2.z*255)};
}
int main() {
pixels = new col[width*height];
memset(pixels, 0, width*height*3); // This line could technically be omitted
printf("starting render...");
clock_t start, stop;
// Setting up the ray
ray r = {{-3, 0, 0},
{0, 0, 1}};
// The list of vertices
vec3 verts_1[] = {
{1, 1, 1},
{1, 1, -1},
{1, -1, 1},
{1, -1, -1},
{-1, 1, 1},
{-1, 1, -1},
{-1, -1, 1},
{-1, -1, -1}
};
// The list of Trigons
polygon::ref_trigon tris_1[] = {
{5, 7, 1},
{1, 7, 3},
{3, 0, 1},
{2, 0, 3},
{4, 0, 2},
{2, 6, 4}
};
// The list of vertices
vec3 verts_2[] = {
{1, 1, 0},
{1, -1, 0},
{-1, 1, 0},
{-1, -1, 0}
};
// The list of Trigons
polygon::ref_trigon tris_2[] = {
{2, 0, 1},
{1, 3, 2}
};
// The list of vertices
vec3 verts_3[] = {
{0.5, 0.5, 0},
{0.5, -0.5, 0},
{-0.5, 0.5, 0},
{-0.5, -0.5, 0}
};
// The list of Trigons
polygon::ref_trigon tris_3[] = {
{2, 0, 1},
{1, 3, 2}
};
// Composing the Object
polygon::Object obj_1 = {
{verts_1, 8, tris_1, 6},
{{0, 0, 0}, {0, 0, 0}}
};
obj_1.mat = {{0.9, 0.9, 0.9}, 1, 0.0, 0};
polygon::Object obj_2 = {
{verts_2, 4, tris_2, 2},
{{0, -1, 0}, {PI_S/2, 0, 0}}
};
obj_2.mat = {{0.9, 0.2, 0.2}, 1, 0.0, 0};
polygon::Object obj_3 = {
{verts_2, 4, tris_2, 2},
{{0, 1, 0}, {-(PI_S/2), 0, 0}}
};
obj_3.mat = {{0.2, 0.9, 0.2}, 1, 0.0, 0};
polygon::Object obj_4 = {
{verts_3, 4, tris_3, 2},
{{0, 0, 0.999}, {0, 0, 0}}
};
obj_4.mat = {{1, 1, 1}, 1, 1, 0};
polygon::Node* l[] = {&obj_1, &obj_2, &obj_3, &obj_4};
polygon::Group obj = {4, l};
//obj.build();
//raymarching::Object* obj_m = new raymarching::primitives::Torus(0.4, 0.15);
raymarching::Object* obj_m1 = new raymarching::primitives::Box(0.3, 0.3, 0.3, 0.04);
//raymarching::Object* obj_m = new raymarching::primitives::Sphere(0.5);
//raymarching::Object* obj_m1 = new raymarching::primitives::Mandelbulb(30, 100);
obj_m1->transform = {{0, 0, 0},
{1.0, 1.0, 1.5}, 1};
obj_m1->mat = {{0.9, 0.9, 0.9}, 0.7, 0, 1};
//obj_m->build();
raymarching::Object* obj_m2 = new raymarching::primitives::Torus(0.4, 0.15);
obj_m2->transform = {{0, 0, 0},
{1, 1, 1.5}, 1};
obj_m2->mat = {{0.2, 0.9, 0.2}, 1, 0, 0};
raymarching::Node* m[] = {obj_m1, obj_m2};
//raymarching::Merger* obj_m = new raymarching::Merger(2, m);
raymarching::Merger* obj_m = new raymarching::primitives::SmoothSubtraction(0.05);
obj_m->no_children = 2;
obj_m->children = m;
raymarching::Object* obj_b = new raymarching::primitives::Mandelbulb(100, 100);
obj_b->transform = {{0, 0, 0},
{1, 1, 1.5}, 0.6};
obj_b->mat = {{0.4, 0.4, 0.9}, 1, 0, 1};
Pathtracer pt = {&obj, (raymarching::Node*)(obj_m1)};
//pt.build();
Renderer rr = Renderer{pt, width, height, NUMBER_OF_THREADS};
rr.build();
vec3* buff = new vec3[width*height]{{0, 0, 0}};
start = clock();
rr.multisample_frame(buff);
stop = clock();
printf("\nFinished rendering the frame!");
printf("\nTonemapping... ");
rr.tonemap_frame(buff);
for (int j = 0; j < width*height; ++j) {
pixels[j] = from_light(buff[j]);
}
printf("Done!");
printf("\nBeginning writing it to a file (with my dodgey bitmap exporter)...");
export_image(pixels, width, height);
printf("\nFinished writing it to a file!");
printf("\n\nrendering took %.3f seconds!\n", (double)(stop - start)/CLOCKS_PER_SEC);
}