feat (wip): real-time gui with rendering in opengl

This commit is contained in:
2025-01-03 19:57:10 +01:00
parent c92d0bc2a9
commit c23a33ebef
22 changed files with 656 additions and 62 deletions

125
src/gui/MainWindow.cpp Normal file
View File

@@ -0,0 +1,125 @@
#include "MainWindow.h"
#include <iostream>
#include <memory>
Window::Window(unsigned int w, unsigned int h) {
Window::w = w;
Window::h = h;
}
void framebuffer_size_callback(GLFWwindow* window, int w, int h) {
// This function is called by glfw when the window is reized.
glViewport(0 , 0, w, h);
Window* newWin = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
newWin->resize(w, h);
}
int Window::init() {
// init glfw
glfwInit();
// requesting context version 1.0 makes glfw try to provide the latest version if possible
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
this->window = glfwCreateWindow(this->w, this->h, "CUDA ray tracing", NULL, NULL);
//hide cursor
glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetWindowUserPointer(this->window, reinterpret_cast<void*>(this));
if (this->window == NULL) {
std::cout << "Failed to create window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(this->window);
// init glad(opengl)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD\n";
return -1;
}
// init framebuffer
glViewport(0, 0, this->w, this->h);
if (glfwSetFramebufferSizeCallback(this->window, framebuffer_size_callback) != 0) return -1;
if (init_quad()) return -1;
this->last_frame = std::chrono::steady_clock::now();
while (!glfwWindowShouldClose(window)) {
Window::tick();
}
Window::free();
return 0;
}
int Window::init_quad() {
this->current_quad = std::make_unique<Quad>(this->w, this->h);
this->current_quad->cuda_init();
// TODO: default shaders
return 0;
}
void Window::free() {
// To preserve the proper destruction order we forcefully set the quads to null (calling their destructor in the process)
// Not strictly necessary, but i saw some weird errors on exit without this so best to keep it in.
this->current_quad = nullptr;
glfwDestroyWindow(window);
glfwTerminate();
}
void Window::tick() {
// manually track time diff
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
float diff = (float) std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_frame).count();
this->last_frame = now;
// TODO: remove debug line at some point
std::cout << 1000.0/diff << " fps\n";
// TODO: code input logic and class/struct and stuff
// ticking input probably involves 4? steps:
// * check if window needs to be closed (escape/q pressed)
// * check if camera moved (wasd/hjkl pressed)
// (phase 3/do later): check if we switched from realtime tracing to that other option - maybe a pause function? (p pressed?)
// * if moved -> update camera (raytracing will involve some logic here too? see when i get there)
// tick render
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// render frame
glBindFramebuffer(GL_FRAMEBUFFER, this->current_quad->fb);
this->current_quad->render();
glBindVertexArray(this->current_quad->VAO);
glBindTexture(GL_TEXTURE_2D, this->current_quad->tex);
glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture
// check for events
// + swap buffers; TODO: check if necessary?
glfwSwapBuffers(this->window);
glfwPollEvents();
}
void Window::resize(unsigned int w, unsigned int h) {
this->w = w;
this->h = h;
this->current_quad->resize(w, h);
}