-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (82 loc) · 2.75 KB
/
main.cpp
File metadata and controls
114 lines (82 loc) · 2.75 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
#include <iostream>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "pathTracer.h"
#define WIDTH 1400
#define HEIGHT 1400
#define SAMPLES 2000
GLuint pbo;
using namespace std;
int main() {
if (!glfwInit()) return -1;
if (atexit(glfwTerminate)) {
glfwTerminate();
return -1;
}
GLFWwindow* window;
window = glfwCreateWindow(WIDTH, HEIGHT, "gl-cuda-test", NULL, NULL);
if (!window) return -1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); //turns on vertical sync, buffers swap to new every frame
if (glewInit() != GLEW_OK) return -1;
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * sizeof(GLubyte) * WIDTH * HEIGHT, NULL, GL_DYNAMIC_DRAW);
kernelBindPbo(pbo); //bind pbo to cuda
allocateMemory(WIDTH, HEIGHT); //allocate memory for cuda
//set up main viewpoint
setCamera(50.0f, 52.0f, 350.0f, 0.0f, 0.0f, -1.0f, 0.5f);
glfwSetTime(0.0);
int iteration = 0;
int samples = 0;
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
if (samples < SAMPLES) {
iteration++;
samples++;
/*if (iteration == 59) {
changeCamera(0.0f, 0.0f, 0.0f, 5.0f);
iteration = 0;
}*/
renderKernel(WIDTH, HEIGHT, iteration, SAMPLES);
// Measure speed
if (iteration == SAMPLES - 1) {
cout << "Done" << endl;
}
if (glfwGetTime() >= 1.0 && iteration < SAMPLES - 1) {
cout << samples << " samples/second" << endl;
samples = 0;
glfwSetTime(0.0);
}
glfwPollEvents();
}
glDrawPixels(WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glfwSwapBuffers(window);
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
kernelExit(pbo);
deallocateMemory(); //deallocate cuda memory
glDeleteBuffers(1, &pbo);
//getchar();
return 0;
}
//void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods){
// if (action == GLFW_PRESS) {
// switch (key) {
// case GLFW_KEY_ESCAPE:
// glfwSetWindowShouldClose(window, GL_TRUE);
// break;
// case GLFW_KEY_DOWN: camChanged = true; theta = -0.1f; break;
// case GLFW_KEY_UP: camChanged = true; theta = +0.1f; break;
// case GLFW_KEY_RIGHT: camChanged = true; phi = -0.1f; break;
// case GLFW_KEY_LEFT: camChanged = true; phi = +0.1f; break;
// case GLFW_KEY_A: camChanged = true; x -= 0.1f; break;
// case GLFW_KEY_D: camChanged = true; x += 0.1f; break;
// case GLFW_KEY_W: camChanged = true; y += 0.1f; break;
// case GLFW_KEY_S: camChanged = true; y -= 0.1f; break;
// case GLFW_KEY_R: camChanged = true; z += 0.1f; break;
// case GLFW_KEY_F: camChanged = true; z -= 0.1f; break;
// }
// }
//}