-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
178 lines (123 loc) · 5.06 KB
/
main.cu
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
#include <filesystem>
#include "src/utility/vector.h"
#include "src/scene/scene.h"
#include "src/scene/sceneLoader.h"
static void glfw_error_callback(int error, const char* description){
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
int main(int argc, char **argv){
std::cout << "Parsing obj...\n";
if(argc != 2){
throw std::runtime_error("Please add a file as argument.");
}
const std::filesystem::path filePath = argv[1];
assert(filePath.extension() == ".xml");
std::cout << "Starting rendering...\n";
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()){
std::cerr << "Error while initializing glfw. Exiting.\n";
exit(1);
}
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
const auto monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
GLFWwindow* window = glfwCreateWindow(mode->width, mode->height, "Dear ImGui GLFW+OpenGL3 example", monitor, nullptr);
if (window == nullptr){
std::cerr << "Window creation failed. Exiting.\n";
exit(1);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
//ImGui Setup
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
if(glewInit() != GLEW_OK){
std::cerr << "Glew not correctly initialized. Exiting.\n";
exit(1);
}
Scene scene(SceneRepresentation(filePath), Device::CPU);
bool needsRender = true;
while (!glfwWindowShouldClose(window)){
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Debug Info");
ImGui::Text("Progress: %f percent", scene.getPercentage());
const Vector3f cameraPos = scene.getCameraPosition();
ImGui::Text("Camera Position (%f, %f, %f)", cameraPos[0], cameraPos[1], cameraPos[2]);
ImGui::ColorEdit3("clear color", (float*)&clear_color);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
{
ImGui::Begin("CUDA GPU Path Tracing");
if(needsRender){
scene.step(1.f/CustomRenderer::min(ImGui::GetIO().Framerate, 1000.f));
//TODO add correct tonemapping for live preview
if(!scene.render()){
needsRender = false;
scene.denoise();
scene.saveOutput();
}
const auto availableSize = ImVec2{
ImGui::GetWindowContentRegionMax().x - ImGui::GetWindowContentRegionMin().x,
ImGui::GetWindowContentRegionMax().y - ImGui::GetWindowContentRegionMin().y,
};
ImGui::Image((void *) (intptr_t)scene.hostImageTexture, availableSize);
}else{
const auto availableSize = ImVec2{
ImGui::GetWindowContentRegionMax().x - ImGui::GetWindowContentRegionMin().x,
ImGui::GetWindowContentRegionMax().y - ImGui::GetWindowContentRegionMin().y,
};
ImGui::Image((void *) (intptr_t)scene.hostImageTexture, availableSize);
}
Vector3f vCamera{0.f};
if(ImGui::IsKeyDown(ImGuiKey_W))
vCamera[2] += 1.f;
if(ImGui::IsKeyDown(ImGuiKey_S))
vCamera[2] -= 1.f;
if(ImGui::IsKeyDown(ImGuiKey_D))
vCamera[0] += 1.f;
if(ImGui::IsKeyDown(ImGuiKey_A))
vCamera[0] -= 1.f;
if(ImGui::IsKeyDown(ImGuiKey_Space))
vCamera[1] += 1.f;
if(ImGui::IsKeyDown(ImGuiKey_LeftShift))
vCamera[1] -= 1.f;
scene.setCameraVelocity(vCamera);
if(vCamera.squaredNorm() != 0.f)
scene.reset();
// scene.denoise();
ImGui::End();
}
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
scene.saveOutput();
std::cout << "Drew image to file\n";
return EXIT_SUCCESS;
}