-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
409 lines (346 loc) · 10.9 KB
/
main.cpp
File metadata and controls
409 lines (346 loc) · 10.9 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* Copyright (C) 2014 MightyCid
*
* This file is part of CUDA-pathtracer <github.com/mightycid/CUDA-pathtracer>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "globals.h"
#include "pathtracer.h"
#include "cutil.h"
#include <cuda_gl_interop.h>
enum MouseMovementType {
MOUSE_NONE, MOUSE_ROTATION, MOUSE_TRANSLATION_XY, MOUSE_TRANSLATION_XZ
};
bool InitGL(int *argc, char** argv);
void InitPBO();
bool InitPathtracer();
void RunPathtracer();
void ReleasePathtracer();
void Display();
void Idle();
void Keyboard(unsigned char key, int /*x*/, int /*y*/);
void PressKey(int key, int /*x*/, int /*y*/);
void Mouse(int button, int state, int x, int y);
void Motion(int x, int y);
void GetFPS();
Camera camera;
float timer = 0.0f;
uint32_t frameCount = 0, timeBase = 0;
uint32_t width, height;
GLuint pbo;
GLuint textureId;
// mouse movement variables
int lastX = -1, lastY = -1;
MouseMovementType mmType = MOUSE_NONE;
Pathtracer *pathtracer = NULL;
bool InitGL(int argc, char** argv) {
glutInit(&argc, argv);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("Pathtracer");
glutDisplayFunc(Display);
glutIdleFunc(Idle);
glutKeyboardFunc(Keyboard);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutSpecialFunc(PressKey);
glewInit();
if (!glewIsSupported("GL_VERSION_2_0 ")) {
fprintf(stderr, "ERROR: Support for necessary OpenGL extensions missing.");
fflush(stderr);
return false;
}
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
InitPBO();
return true;
}
/**
* Generates the pixel buffer object for direct rendering on the GPU
*/
void InitPBO() {
uint32_t numTexels = width * height;
uint32_t sizeTexData = sizeof(GLfloat) * numTexels * 3;
void *data = malloc(sizeTexData*sizeof(float));
glGenBuffers(1, &pbo);
glBindBuffer(GL_ARRAY_BUFFER, pbo);
glBufferData(GL_ARRAY_BUFFER, sizeTexData, data, GL_DYNAMIC_DRAW);
CudaSafeCall(cudaGLRegisterBufferObject(pbo));
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&textureId);
glBindTexture( GL_TEXTURE_2D, textureId);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_FLOAT, NULL);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
/**
* Initializes the scene
* creates camera, materials, primitives and lights
* TODO: configuration file parsing
*/
void InitScene(Camera *camera, std::vector<Material> *mv, std::vector<Primitive> *pv, std::vector<Light> *lv) {
*camera = Camera(Point(0,45,79.5), Point(0,35,0), Vec(0,1,0), width, height, 0.f, 0.f, 60.f);
*mv = std::vector<Material>();
mv->push_back(CreateDiffuseMaterial(Color(1.f, 1.f, 1.f), 0.f)); // default
mv->push_back(CreateDiffuseMaterial(Color(0.75f, 0.25f, 0.25f), 0.f)); // 1
mv->push_back(CreateDiffuseMaterial(Color(0.25f, 0.25f, 0.75f), 0.f)); // 2
mv->push_back(CreateDiffuseMaterial(Color(0.75f, 0.75f, 0.75f), 0.f)); // 3
mv->push_back(CreateSpecularMaterial(Color(0.999f, 0.999f, 0.999f), 1.f)); // 4
mv->push_back(CreateTransmissiveMaterial(Color(0.999f, 0.999f, 0.999f), 1.5f)); // 5
*pv = std::vector<Primitive>();
//scene 1
/*pv->push_back(Primitive(Point(0.f,-1E+5f-1.f,0.f), 1E+5f, 1)); //floor
pv->push_back(Primitive(Point(0.f,1E+5f+3.f,0.f), 1E+5f, 1)); //ceiling
pv->push_back(Primitive(Point(0.f,0.f,-1E+5f-7.f), 1E+5f, 1)); //back
pv->push_back(Primitive(Point(0.f,0.f,1E+5f+7.f), 1E+5f, 1)); //front
pv->push_back(Primitive(Point(-1E+5f-4.f,0.f,0.f), 1E+5f, 2)); //left
pv->push_back(Primitive(Point(1E+5f+4.f,0.f,0.f), 1E+5f, 3)); //right
pv->push_back(Primitive(Point(-1.5f,0.f,0.f), 1.f, 4));
pv->push_back(Primitive(Point(1.5f,0.f,0.f), 1.f, 5));
pv->push_back(Primitive(Point(0.f,2.f,0.f), 0.5f, 1, 0));*/
//scene 2
pv->push_back(Primitive(Point( 1e5+50, 40, 0), 1e5, 2)); //left
pv->push_back(Primitive(Point(-1e5-50, 40, 0), 1e5, 1)); //right
pv->push_back(Primitive(Point( 0, 40,-1e5-80), 1e5, 3)); //back
pv->push_back(Primitive(Point( 0, 40, 1e5+80), 1e5, 3)); //front
pv->push_back(Primitive(Point( 0, -1e5, 0), 1e5, 3)); //bottom
pv->push_back(Primitive(Point( 0, 1e5+80, 0), 1e5, 3)); //top
pv->push_back(Primitive(Point( -25, 16.5, -50), 16.5, 4));
pv->push_back(Primitive(Point( 25, 16.5, -25), 16.5, 5));
pv->push_back(Primitive(Point( 0, 579.6, -40), 500, 1, 0));
*lv = std::vector<Light>();
lv->push_back(Light(8, Color(12.f,12.f,12.f)));
//lv->push_back(Light(Point(50,20,40.f), Color(150.f,150.f,150.f)));
}
/**
* Initialize pathtracer
*/
bool InitPathtracer() {
std::vector<Material> materials;
std::vector<Primitive> primitives;
std::vector<Light> lights;
InitScene(&camera, &materials, &primitives, &lights);
pathtracer = new Pathtracer(&camera, 10);
if(!pathtracer) {
fprintf(stderr, "ERROR: Could not create Pathtracer instance.");
fflush(stderr);
return false;
}
if(!pathtracer->Init(materials, primitives, lights)) {
fprintf(stderr, "ERROR: Could not initialize Pathtracer.");
fflush(stderr);
return false;
}
return true;
}
/**
* Release resources
*/
void ReleasePathtracer() {
pathtracer->Release();
delete pathtracer;
}
/**
* default display method
* maps the pbo to the device buffer and calls the kernel before displaying
* the result as a texture in OpenGL
*/
void Display() {
//Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
if(camera.IsUpdated()) pathtracer->Reset();
float* devBuffer = NULL;
CudaSafeCall(cudaGLMapBufferObject((void**)&devBuffer, pbo));
// call kernel
pathtracer->Run(devBuffer);
CudaSafeCall(cudaGLUnmapBufferObject(pbo));
// render result as texure
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_FLOAT, NULL);
glBegin(GL_QUADS); {
glTexCoord2f(0, 0); glVertex3f(-1, 1, 0);
glTexCoord2f(1, 0); glVertex3f(1, 1, 0);
glTexCoord2f(1, 1); glVertex3f(1, -1, 0);
glTexCoord2f(0, 1); glVertex3f(-1, -1, 0);
} glEnd();
glutSwapBuffers();
GetFPS();
}
void Idle() {
glutPostRedisplay();
}
void Keyboard(unsigned char key, int /*x*/, int /*y*/) {
switch (key) {
case (27):
exit(EXIT_SUCCESS);
break;
case 'r':
pathtracer->Reset();
break;
}
}
void PressKey(int key, int /*x*/, int /*y*/) {
const int keyMod = glutGetModifiers();
const float camTranslateDelta = (keyMod & GLUT_ACTIVE_SHIFT)
? CAM_TRANSLATE_DELTA * 10 : CAM_TRANSLATE_DELTA;
const float camRotateDelta = (keyMod & GLUT_ACTIVE_SHIFT)
? CAM_ROTATE_DELTA * 10 : CAM_ROTATE_DELTA;
// ctrl-key pressed
if(keyMod & GLUT_ACTIVE_CTRL) {
// crtl-key + alt-key pressed
if(keyMod & GLUT_ACTIVE_ALT) {
switch(key) {
case GLUT_KEY_UP:
camera.Translate(Vec(0.f, camTranslateDelta, 0.f));
break;
case GLUT_KEY_RIGHT:
camera.Translate(Vec(camTranslateDelta, 0.f, 0.f));
break;
case GLUT_KEY_DOWN:
camera.Translate(Vec(0.f, -camTranslateDelta, 0.f));
break;
case GLUT_KEY_LEFT:
camera.Translate(Vec(-camTranslateDelta, 0.f, 0.f));
break;
}
} else {
switch(key) {
case GLUT_KEY_UP:
camera.Translate(Vec(0.f, 0.f, camTranslateDelta));
break;
case GLUT_KEY_RIGHT:
camera.Translate(Vec(camTranslateDelta, 0.f, 0.f));
break;
case GLUT_KEY_DOWN:
camera.Translate(Vec(0.f, 0.f, -camTranslateDelta));
break;
case GLUT_KEY_LEFT:
camera.Translate(Vec(-camTranslateDelta, 0.f, 0.f));
break;
}
}
// no modifier key active
} else {
switch(key) {
case GLUT_KEY_UP:
camera.Rotate(Vec(0.f, camRotateDelta, 0.f));
break;
case GLUT_KEY_RIGHT:
camera.Rotate(Vec(-camRotateDelta, 0.f, 0.f));
break;
case GLUT_KEY_DOWN:
camera.Rotate(Vec(0.f, -camRotateDelta, 0.f));
break;
case GLUT_KEY_LEFT:
camera.Rotate(Vec(camRotateDelta, 0.f, 0.f));
break;
}
}
}
void Mouse(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON) {
// mouse button pressed
if(state == GLUT_DOWN) {
mmType = MOUSE_ROTATION;
lastX = x;
lastY = y;
}
else if(state == GLUT_UP)
mmType = MOUSE_NONE;
}
else if(button == GLUT_MIDDLE_BUTTON) {
// mouse button pressed
if(state == GLUT_DOWN) {
mmType = MOUSE_TRANSLATION_XZ;
lastX = x;
lastY = y;
}
else if(state == GLUT_UP)
mmType = MOUSE_NONE;
}
else if(button == GLUT_RIGHT_BUTTON) {
// mouse button pressed
if(state == GLUT_DOWN) {
mmType = MOUSE_TRANSLATION_XY;
lastX = x;
lastY = y;
}
else if(state == GLUT_UP)
mmType = MOUSE_NONE;
}
}
void Motion(int x, int y) {
if(mmType != MOUSE_NONE) {
const float deltaX = (float)(x - lastX);
const float deltaY = (float)(y - lastY);
if(deltaX != 0.f || deltaY != 0.f) {
if(mmType == MOUSE_ROTATION)
camera.Rotate(Vec(-deltaX, -deltaY, 0.f) * 0.005f);
else if(mmType == MOUSE_TRANSLATION_XY)
camera.Translate(Vec(deltaX, -deltaY, 0.f)*0.25);
else if(mmType == MOUSE_TRANSLATION_XZ)
camera.Translate(Vec(deltaX, 0.f, -deltaY)*0.25);
}
lastX = x;
lastY = y;
}
}
void GetFPS() {
++frameCount;
uint32_t currTime = glutGet(GLUT_ELAPSED_TIME);
uint32_t elapsed = currTime - timeBase;
if (elapsed > 1000) {
float fps = frameCount*1000.0f/elapsed;
timeBase = currTime;
frameCount = 0;
char buffer[32];
sprintf(buffer, "Pathtracer (%.4f sps : %u)", fps, pathtracer->GetIteration());
glutSetWindowTitle(buffer);
}
}
/**
* Main method
*/
int main(int argc, char** argv) {
Scene *scene = NULL;
width = 640;
height = 480;
printf("Initializing OpenGL...");
if(!InitGL(argc, argv)) {
fprintf(stderr, "ERROR: Could not initialize Pathtracer.");
fflush(stderr);
exit(-1);
}
printf("done!\n");
printf("Initializing Pathtracer...");
if(!InitPathtracer()) {
fprintf(stderr, "ERROR: Could not initialize Pathtracer.");
fflush(stderr);
exit(-1);
}
printf("done!\n");
printf("Starting main loop\n");
glutMainLoop();
ReleasePathtracer();
}