-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
332 lines (275 loc) · 12.2 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <vector>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include "Scene.h"
#include "pathtracer.h"
#include "utils.h"
auto constexpr WIDTH = 640;
auto constexpr HEIGHT = 480;
GLuint pbo;
cudaGraphicsResource_t resource;
RenderParameters renderParams;
cudaScene device_scene;
void init()
{
glClearColor(0.f, 0.f, 0.f, 0.f);
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glBufferData(GL_PIXEL_UNPACK_BUFFER, sizeof(uchar4) * WIDTH * HEIGHT, NULL, GL_DYNAMIC_COPY);
checkCudaErrors(cudaSetDevice(0));
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&resource, pbo, cudaGraphicsRegisterFlagsNone));
checkCudaErrors(cudaMalloc((void**)&(renderParams.hdr_buffer), sizeof(glm::vec3) * WIDTH * HEIGHT));
checkCudaErrors(cudaMemset((void*)renderParams.hdr_buffer, 0, sizeof(glm::vec3) * WIDTH * HEIGHT));
renderParams.exposure = 1.f;
renderParams.rayDepth = 15;
Scene host_scene;
//build scene
/*
//camera
host_scene.AddCamera(cudaCamera(glm::vec3(3.f, 0.0f, 3.0f), glm::vec3(0.f, 0.0f, 0), glm::vec3(0.f, 1.f, 0.f), 15.f));
cudaMaterial mat1;
mat1.albedo = glm::vec3(0.7f, 0.5f, 0.3f);
host_scene.AddMaterial(mat1);
//table top
host_scene.AddAABB(cudaAAB(glm::vec3(-0.5, -0.35, -0.5), glm::vec3(0.3, -0.3, 0.5), host_scene.GetLastMaterialID()));
//table legs
host_scene.AddAABB(cudaAAB(glm::vec3(-0.45, -1, -0.45), glm::vec3(-0.4, -0.35, -0.4), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.2, -1, -0.45), glm::vec3(0.25, -0.35, -0.4), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(-0.45, -1, 0.4), glm::vec3(-0.4, -0.35, 0.45), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.2, -1, 0.4), glm::vec3(0.25, -0.35, 0.45), host_scene.GetLastMaterialID()));
//chair set
host_scene.AddAABB(cudaAAB(glm::vec3(0.3, -0.6, -0.2), glm::vec3(0.7, -0.55, 0.2), host_scene.GetLastMaterialID()));
//chair legs
host_scene.AddAABB(cudaAAB(glm::vec3(0.3, -1, -0.2), glm::vec3(0.35, -0.6, -0.15), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.3, -1, 0.15), glm::vec3(0.35, -0.6, 0.2), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.65, -1, -0.2), glm::vec3(0.7, 0.1, -0.15), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.65, -1, 0.15), glm::vec3(0.7, 0.1, 0.2), host_scene.GetLastMaterialID()));
//chair back
host_scene.AddAABB(cudaAAB(glm::vec3(0.65, 0.05, -0.15), glm::vec3(0.7, 0.1, 0.15), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.65, -0.55, -0.09), glm::vec3(0.7, 0.1, -0.03), host_scene.GetLastMaterialID()));
host_scene.AddAABB(cudaAAB(glm::vec3(0.65, -0.55, 0.03), glm::vec3(0.7, 0.1, 0.09), host_scene.GetLastMaterialID()));
//sphere on the table
cudaMaterial mat2;
//mat2.albedo = glm::vec3(0.448f, 0.8f, 0.666f);
mat2.albedo = glm::vec3(1.f);
mat2.bsdf_type = BSDF_GLASS;
mat2.ior = 1.5f;
mat2.roughness = 9999.f;
host_scene.AddMaterial(mat2);
host_scene.AddSphere(cudaSphere(glm::vec3(-0.1, 0.12, 0.1), 0.20, host_scene.GetLastMaterialID()));
//host_scene.AddAABB(cudaAAB(glm::vec3(-0.3, -0.13, -0.1), glm::vec3(0.1, 0.27, 0.3), host_scene.GetLastMaterialID()));
//cudaMaterial mat7;
//mat7.albedo = glm::vec3(0.4f, 0.8f, 0.7f);
//host_scene.AddMaterial(mat7);
//host_scene.AddSphere(cudaSphere(glm::vec3(0.2, -0.099, -0.1), 0.20, host_scene.GetLastMaterialID()));
////walls
cudaMaterial mat3;
mat3.albedo = glm::vec3(1.f, 1.f, 1.f);
host_scene.AddMaterial(mat3);
//back
host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 0.f, -0.8f), normalize(glm::vec3(0.f, 0.f, 1.f)), host_scene.GetLastMaterialID()));
//front
host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 0.f, 1.2f), normalize(glm::vec3(0.f, 0.f, -1.f)), host_scene.GetLastMaterialID()));
//bottom
host_scene.AddPlane(cudaPlane(glm::vec3(0.f, -1.f, 0.f), normalize(glm::vec3(0.f, 1.f, 0.f)), host_scene.GetLastMaterialID()));
//top
host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 0.8f, 0.f), normalize(glm::vec3(0.f, -1.f, 0.f)), host_scene.GetLastMaterialID()));
//left
cudaMaterial mat4;
mat4.albedo = glm::vec3(0.1f, 0.5f, 1.f);
host_scene.AddMaterial(mat4);
host_scene.AddPlane(cudaPlane(glm::vec3(-0.8f, 0.f, 0.f), normalize(glm::vec3(1.f, 0.f, 0.f)), host_scene.GetLastMaterialID()));
//right
cudaMaterial mat5;
mat5.albedo = glm::vec3(1.0f, 0.9f, 0.1f);
host_scene.AddMaterial(mat5);
host_scene.AddPlane(cudaPlane(glm::vec3(0.8f, 0.f, 0.f), normalize(glm::vec3(-1.f, 0.f, 0.f)), host_scene.GetLastMaterialID()));
//light
cudaMaterial mat6;
mat6.albedo = glm::vec3(1.f, 1.f, 1.f);
mat6.emition = glm::vec3(2.f, 2.f, 2.f);
host_scene.AddMaterial(mat6);
host_scene.AddAABB(cudaAAB(glm::vec3(-0.38, 0.78, -0.25), glm::vec3(0.32, 0.8, 0.25), host_scene.GetLastMaterialID()));
*/
/*
//cornell box
//camera
host_scene.AddCamera(cudaCamera(glm::vec3(50.f, 52.f, 335.6f), glm::vec3(50.f, 52.f - 0.042612f, 335.6f - 1.f), glm::vec3(0.f, 1.f, 0.f), 25.f));
//left
cudaMaterial mat1;
mat1.albedo = glm::vec3(0.75f, 0.0f, 0.0f);
host_scene.AddMaterial(mat1);
host_scene.AddSphere(cudaSphere(glm::vec3(1e5f + 1.0f, 40.8f, 81.6f), 1e5f, host_scene.GetLastMaterialID()));
//right
cudaMaterial mat2;
mat2.albedo = glm::vec3(.0f, .75f, .0f);
host_scene.AddMaterial(mat2);
host_scene.AddSphere(cudaSphere(glm::vec3(-1e5f + 99.0f, 40.8f, 81.6f), 1e5f, host_scene.GetLastMaterialID()));
//back
cudaMaterial mat3;
mat3.albedo = glm::vec3(.75f, .75f, .75f);
host_scene.AddMaterial(mat3);
host_scene.AddSphere(cudaSphere(glm::vec3(50.0f, 40.8f, 1e5f), 1e5f, host_scene.GetLastMaterialID()));
//front
cudaMaterial mat4;
mat4.albedo = glm::vec3(1.00f, 1.00f, 1.00f);
host_scene.AddMaterial(mat4);
host_scene.AddSphere(cudaSphere(glm::vec3(50.0f, 40.8f, -1e5f + 600.0f), 1e5f, host_scene.GetLastMaterialID()));
//top
cudaMaterial mat6;
mat6.albedo = glm::vec3(.75f, .75f, .75f);
host_scene.AddMaterial(mat6);
host_scene.AddSphere(cudaSphere(glm::vec3(50.0f, -1e5f + 81.6f, 81.6f), 1e5f, host_scene.GetLastMaterialID()));
//bottom
cudaMaterial mat5;
mat5.albedo = glm::vec3(.75f, .75f, .75f);
host_scene.AddMaterial(mat5);
host_scene.AddSphere(cudaSphere(glm::vec3(50.0f, 1e5f, 81.6f), 1e5f, host_scene.GetLastMaterialID()));
//light
cudaMaterial mat7;
mat7.albedo = glm::vec3(1.00f, 1.00f, 1.00f);
mat7.emition = glm::vec3(2.f);
host_scene.AddMaterial(mat7);
host_scene.AddSphere(cudaSphere(glm::vec3(50.0f, 681.6f - .77f, 81.6f), 600.0f, host_scene.GetLastMaterialID()));
//glass sphere
cudaMaterial mat8;
mat8.albedo = glm::vec3(1.00f, 1.00f, 1.00f);
mat8.ior = 1.5;
mat8.bsdf_type = BSDF_GLASS;
mat8.roughness = 10000;
host_scene.AddMaterial(mat8);
host_scene.AddSphere(cudaSphere(glm::vec3(73.0f, 26.5f, 88.0f), 16.5f, host_scene.GetLastMaterialID()));
*/
//triangle mesh
host_scene.AddCamera(cudaCamera(glm::vec3(0.f, 40.0f, 140.0f), glm::vec3(0.f, 15.f, 0.f), glm::vec3(0.f, 1.f, 0.f), 20.f));
host_scene.AddEnvLight(cudaEnvironmentLight(create_environment_light_texture("LA_Downtown_Helipad_GoldenHour_Env.hdr")));
//ground
cudaMaterial mat1;
mat1.albedo = glm::vec3(0.3f, 0.6f, 0.2f);
mat1.bsdf_type = BSDF_PLASTIC;
host_scene.AddMaterial(mat1);
//host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 1.f, 0.f), host_scene.GetLastMaterialID()));
host_scene.AddAAB(cudaAAB(glm::vec3(-50.f, -2.f, 50.f), glm::vec3(50.f, 0.f, -50.f), host_scene.GetLastMaterialID()));
//back
cudaMaterial mat4;
//mat4.albedo = glm::vec3(0.9f, 0.3f, 0.3f);
mat4.albedo = glm::vec3(0.5f);
host_scene.AddMaterial(mat4);
//host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 0.f, -50.f), glm::vec3(0.f, 0.f, 1.f), host_scene.GetLastMaterialID()));
//front
cudaMaterial mat8;
mat8.albedo = glm::vec3(0.8f);
host_scene.AddMaterial(mat8);
//host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 0.f, 160.f), glm::vec3(0.f, 0.f, -1.f), host_scene.GetLastMaterialID()));
//top
//host_scene.AddPlane(cudaPlane(glm::vec3(0.f, 100.f, 0.f), glm::vec3(0.f, -1.f, 0.f), host_scene.GetLastMaterialID()));
//left
cudaMaterial mat5;
mat5.albedo = glm::vec3(0.1f, 0.5f, 1.f);
//mat5.albedo = glm::vec3(0.5f);
host_scene.AddMaterial(mat5);
//host_scene.AddPlane(cudaPlane(glm::vec3(-50.f, 0.f, 0.f), glm::vec3(1.f, 0.f, 0.f), host_scene.GetLastMaterialID()));
//right
cudaMaterial mat6;
mat6.albedo = glm::vec3(1.0f, .9f, .1f);
//mat6.albedo = glm::vec3(0.5f);
host_scene.AddMaterial(mat6);
//host_scene.AddPlane(cudaPlane(glm::vec3(50.f, 0.f, 0.f), glm::vec3(-1.f, 0.f, 0.f), host_scene.GetLastMaterialID()));
//ball1
cudaMaterial mat7;
mat7.bsdf_type = BSDF_GLOSSY;
mat7.ior = 1.5f;
//mat7.albedo = glm::vec3(0.9f, 0.4f, 0.7f);
mat7.albedo = glm::vec3(1.f);
mat7.roughness = 99999.f;
host_scene.AddMaterial(mat7);
host_scene.AddSphere(cudaSphere(glm::vec3(-25.f, 10.f, -6.f), 10.f, host_scene.GetLastMaterialID()));
//light
cudaMaterial mat2;
mat2.albedo = glm::vec3(1.f);
mat2.emition = glm::vec3(2.f);
host_scene.AddMaterial(mat2);
//host_scene.AddSphere(cudaSphere(glm::vec3(0.f, 70.f, 30.f), 20.f, host_scene.GetLastMaterialID()));
//objmesh
ObjMesh mesh("wineglass.obj");
glm::mat4 m = glm::mat4(1.f);
auto scale = glm::scale(m, 50.f / glm::vec3(glm::length(mesh.vmax - mesh.vmin)));
scale = glm::rotate(scale, float(20.f / 180.f * M_PI), glm::vec3(0.f, 1.f, 0.f));
mesh.ApplyTransform(scale);
auto translate = glm::translate(m, glm::vec3(0.f, 0.5f * (mesh.vmax.y - mesh.vmin.y), 10.f));
mesh.ApplyTransform(translate);
BVH bvh(mesh);
export_linear_bvh(bvh, "bvh.bvh");
cudaMaterial mat3;
//mat3.albedo = glm::vec3(0.8f, 0.331f, 0.065f);
//mat3.albedo = glm::vec3(0.f, 0.9f, 0.f);
mat3.albedo = glm::vec3(1.f);
mat3.bsdf_type = BSDF_GLASS;
mat3.ior = 1.6f;
mat3.roughness = 99999.f;
host_scene.AddMaterial(mat3);
host_scene.AddMesh(cudaMesh(bvh, host_scene.GetLastMaterialID()));
//copy host scene to device scene
host_scene.BuildSceneForGPU(device_scene);
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::u8vec4* img;
size_t size;
checkCudaErrors(cudaGraphicsMapResources(1, &resource));
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void**)&img, &size, resource));
//todo: add rendering function call
cudaEvent_t start, end;
cudaEventCreate(&start);
cudaEventCreate(&end);
cudaEventRecord(start);
for(auto i = 0; i < 5; ++i)
{
test(img, device_scene, renderParams);
renderParams.iteration_count++;
}
cudaEventRecord(end);
cudaEventSynchronize(end);
float ellapsed = 0;
cudaEventElapsedTime(&ellapsed, start, end);
std::cout<<"IPS: "<<1000.f / (ellapsed / 5.f)<<std::endl;
checkCudaErrors(cudaGraphicsUnmapResources(1, &resource));
glDrawPixels(WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(int argc, char** argv)
{
GLFWwindow* window;
if(!glfwInit())
return -1;
window = glfwCreateWindow(WIDTH, HEIGHT, "SunPathTracer", NULL, NULL);
if(!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
init();
while(!glfwWindowShouldClose(window))
{
render();
glfwSwapBuffers(window);
glfwPollEvents();
if(renderParams.iteration_count % 100 == 0)
std::cout<<"iterations: "<<renderParams.iteration_count<<std::endl;
}
glfwDestroyWindow(window);
glfwTerminate();
cudaDeviceReset();
return 0;
}