mirror of
https://github.com/MartinOpat/cuda-based-raytrace.git
synced 2025-06-07 02:13:10 +02:00
implemented controls
This commit is contained in:
1 parent
b3db9a7045
commit
884b53655b
15 files changed
+169
-73
No files matched your search
+14
-10
@@ -5,6 +5,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "Shader.h"
|
||||
#include "input/Widget.h"
|
||||
|
||||
Window::Window(unsigned int w, unsigned int h) {
|
||||
this->w = w;
|
||||
@@ -28,9 +29,6 @@ int Window::init(float* data) {
|
||||
|
||||
this->window = glfwCreateWindow(this->w, this->h, "CUDA ray tracing", NULL, NULL);
|
||||
|
||||
//hide cursor // TODO: switch from this style input to something more resembling an actual gui
|
||||
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";
|
||||
@@ -53,6 +51,10 @@ int Window::init(float* data) {
|
||||
if (init_quad(data)) return -1;
|
||||
this->last_frame = std::chrono::steady_clock::now();
|
||||
|
||||
// init imGUI
|
||||
this->widget = new Widget(this->window);
|
||||
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
Window::tick();
|
||||
}
|
||||
@@ -79,8 +81,10 @@ void Window::free(float* data) {
|
||||
// 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->quad = nullptr;
|
||||
this->widget = nullptr;
|
||||
cudaFree(data);
|
||||
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
}
|
||||
@@ -95,23 +99,23 @@ void Window::tick() {
|
||||
// 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)
|
||||
// input
|
||||
this->widget->tick();
|
||||
|
||||
|
||||
// tick render
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
this->quad->render();
|
||||
if (!this->widget->paused) this->quad->render();
|
||||
this->shader->use();
|
||||
glBindVertexArray(this->quad->VAO);
|
||||
glBindTexture(GL_TEXTURE_2D, this->quad->tex);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture
|
||||
|
||||
// render ImGui context
|
||||
this->widget->render();
|
||||
|
||||
// check for events
|
||||
glfwSwapBuffers(this->window);
|
||||
glfwPollEvents();
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <chrono>
|
||||
#include "input/Widget.h"
|
||||
|
||||
|
||||
class Window {
|
||||
@@ -23,6 +24,7 @@ public:
|
||||
private:
|
||||
GLFWwindow* window;
|
||||
std::unique_ptr<Quad> quad;
|
||||
Widget* widget;
|
||||
|
||||
std::chrono::steady_clock::time_point last_frame;
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "Widget.h"
|
||||
#include "linalg/linalg.h"
|
||||
#include "consts.h"
|
||||
|
||||
|
||||
Widget::Widget(GLFWwindow* window) {
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
this->io = ImGui::GetIO();
|
||||
|
||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
|
||||
this->cameraDir = Vec3::init(0.4, 0.6, 1.0).normalize();
|
||||
this->cameraPos = Point3::init(-0.7, -1.0, -2.0);
|
||||
this->cameraUp = Vec3::init(0.0, 1.0, 0.0).normalize();
|
||||
this->lightPos = Point3::init(1.5, 2.0, -1.0);
|
||||
this->paused = true;
|
||||
this->renderOnce = false;
|
||||
};
|
||||
|
||||
void Widget::tick() {
|
||||
if (this->renderOnce) {
|
||||
this->renderOnce = false;
|
||||
this->paused = true;
|
||||
}
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// input widgets
|
||||
float min = -1, max = 1;
|
||||
|
||||
ImGui::Begin("Light Controls");
|
||||
ImGui::DragScalar("X coordinate", ImGuiDataType_Double, &this->lightPos.x, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("Y coordinate", ImGuiDataType_Double, &this->lightPos.y, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("Z coordinate", ImGuiDataType_Double, &this->lightPos.z, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Begin("Pause");
|
||||
if (ImGui::Button(this->paused ? "Unpause" : "Pause")) this->paused = !this->paused;
|
||||
if (ImGui::Button("render Once")) {
|
||||
this->paused = !this->paused;
|
||||
this->renderOnce = true;
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Begin("Camera Controls");
|
||||
ImGui::DragScalar("X position", ImGuiDataType_Double, &this->cameraPos.x, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("Y position", ImGuiDataType_Double, &this->cameraPos.y, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("Z position", ImGuiDataType_Double, &this->cameraPos.z, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("X direction", ImGuiDataType_Double, &this->cameraDir.x, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("Y direction", ImGuiDataType_Double, &this->cameraDir.y, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::DragScalar("Z direction", ImGuiDataType_Double, &this->cameraDir.z, 0.005f, &min, &max, "%.3f");
|
||||
ImGui::End();
|
||||
|
||||
copyToDevice();
|
||||
}
|
||||
|
||||
void Widget::render() {
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
|
||||
void Widget::copyToDevice() {
|
||||
cudaMemcpyToSymbol(&d_cameraPos, &this->cameraPos, sizeof(Point3));
|
||||
cudaMemcpyToSymbol(&d_cameraDir, &this->cameraDir, sizeof(Vec3));
|
||||
cudaMemcpyToSymbol(&d_cameraUp, &this->cameraUp, sizeof(Vec3));
|
||||
cudaMemcpyToSymbol(&d_lightPos, &this->lightPos, sizeof(Point3));
|
||||
}
|
||||
|
||||
Widget::~Widget() {
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef WIDGET_H
|
||||
#define WIDGET_H
|
||||
|
||||
#include "../include/imgui/imgui.h"
|
||||
#include "../include/imgui/backends/imgui_impl_glfw.h"
|
||||
#include "../include/imgui/backends/imgui_impl_opengl3.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "linalg/linalg.h"
|
||||
|
||||
class Widget {
|
||||
public:
|
||||
Point3 cameraDir;
|
||||
Vec3 cameraPos;
|
||||
Vec3 cameraUp;
|
||||
Point3 lightPos;
|
||||
bool paused;
|
||||
bool renderOnce;
|
||||
|
||||
ImGuiIO io;
|
||||
|
||||
void tick();
|
||||
void render();
|
||||
void copyToDevice();
|
||||
|
||||
Widget(GLFWwindow* window);
|
||||
~Widget();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // WIDGET_H
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RENDERERINFO_H
|
||||
#define RENDERERINFO_H
|
||||
|
||||
class rendererInfo {
|
||||
public:
|
||||
Vec3 cameraDir;
|
||||
Vec3 cameraPos;
|
||||
Vec3 lightPos;
|
||||
|
||||
RendererInfo();
|
||||
void copyToDevice();
|
||||
};
|
||||
|
||||
#endif // RENDERERINFO_H
|
||||
@@ -9,4 +9,4 @@ void main()
|
||||
{
|
||||
vec3 col = texture(screenTexture, TexCoords).rgb;
|
||||
FragColor = vec4(col, 1.0);
|
||||
}
|
||||
}
|
||||