From 5fd66cfdfae7a3279557711fd2215c787eb71a73 Mon Sep 17 00:00:00 2001 From: Benjamin Kyd Date: Sat, 22 Apr 2023 17:50:21 +0100 Subject: [PATCH] refactor start: we are doing some epic procedural programming --- CMakeLists.txt | 3 +- src/inferno.cpp | 298 ++++++++---------------------------------------- src/inferno.hpp | 47 +++----- src/main.cpp | 12 +- src/window.cpp | 2 +- src/window.hpp | 2 +- 6 files changed, 72 insertions(+), 292 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 897b76d..4b5363a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ target_include_directories(inferno PRIVATE "src/") target_include_directories(inferno PRIVATE "src/thirdparty") # Hardware Acceleration Modules (HART) -add_subdirectory("hart/inferno-hart-cpu") +#add_subdirectory("hart/inferno-hart-cpu") #add_subdirectory("hart/inferno-hart-opencl") # Compiler arguments @@ -66,3 +66,4 @@ else() OpenGL::GL ) endif() + diff --git a/src/inferno.cpp b/src/inferno.cpp index 4f0bce9..d0eaa3e 100644 --- a/src/inferno.cpp +++ b/src/inferno.cpp @@ -1,20 +1,20 @@ #include "inferno.hpp" #include -#include "gui/layout.hpp" +//#include "gui/layout.hpp" #include "window.hpp" -#include "hart_module.hpp" -#include "hart_directory.hpp" +//#include "hart_module.hpp" +//#include "hart_directory.hpp" -#include "preview_renderer/renderer.hpp" -#include "preview_renderer/shader.hpp" -#include "renderer/dispatcher.hpp" -#include "renderer/renderer.hpp" -#include "scene/camera.hpp" -#include "scene/scene.hpp" -#include "scene/material.hpp" -#include "scene/mesh.hpp" +//#include "preview_renderer/renderer.hpp" +//#include "preview_renderer/shader.hpp" +//#include "renderer/dispatcher.hpp" +//#include "renderer/renderer.hpp" +//#include "scene/camera.hpp" +//#include "scene/scene.hpp" +//#include "scene/material.hpp" +//#include "scene/mesh.hpp" #include @@ -23,28 +23,26 @@ #include #include -using namespace inferno; +namespace inferno::core { -Inferno::Inferno() +InfernoApp* create_inferno() { // MOTD yolo::info("INFERNO HART v" INFERNO_VERSION); + InfernoApp* app = new InfernoApp(); + // Create window - mWin = &Window::GetInstance(); - mWin->init("Inferno v" INFERNO_VERSION, 1280, 720); - - mRasterRenderer = new RasterizeRenderer(); - mRayRenderer = new RenderDispatcher(); - mScene = new Scene(); + app->Win = &Window::GetInstance(); + app->Win->init("Inferno v" INFERNO_VERSION, 1280, 720); + return app; } -Inferno::~Inferno() +void cleanup_inferno(InfernoApp* app) { - } -static void HelpMarker(const char* desc) +static void gui_help_marker(const char* desc) { ImGui::TextDisabled("(?)"); if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) @@ -57,7 +55,7 @@ static void HelpMarker(const char* desc) } } -void Inferno::uiPreset() +void preset_gui(InfernoApp *app) { ImGuiID dockspace_id = ImGui::GetID("main"); @@ -74,256 +72,58 @@ void Inferno::uiPreset() yolo::info("LAYOUT SET TO DEFAULT"); } -void Inferno::moveInput() +void move_input(InfernoApp *app) { static GLFWcursor* cursor = glfwCreateStandardCursor(GLFW_HAND_CURSOR); - glfwSetCursor(mWin->getGLFWWindow(), cursor); + glfwSetCursor(app->Win->getGLFWWindow(), cursor); // KBD & MOUSE // pan only get on hold static glm::dvec2 lastMousePos; static int firstClick = 0; - if (glfwGetMouseButton(mWin->getGLFWWindow(), GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) + if (glfwGetMouseButton(app->Win->getGLFWWindow(), GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) { firstClick++; if (firstClick == 1) { - glfwGetCursorPos(mWin->getGLFWWindow(), &lastMousePos.x, &lastMousePos.y); + glfwGetCursorPos(app->Win->getGLFWWindow(), &lastMousePos.x, &lastMousePos.y); } glm::dvec2 tempMousePos = { 0.0f, 0.0f }; - glfwGetCursorPos(mWin->getGLFWWindow(), &tempMousePos.x, &tempMousePos.y); - mouseDelta = lastMousePos - tempMousePos; + glfwGetCursorPos(app->Win->getGLFWWindow(), &tempMousePos.x, &tempMousePos.y); + app->Input->MouseDelta = lastMousePos - tempMousePos; lastMousePos = tempMousePos; } else { firstClick = 0; - mouseDelta = { 0.0f, 0.0f }; + app->Input->MouseDelta = { 0.0f, 0.0f }; lastMousePos = { 0.0f, 0.0f }; } - movementDelta = 0b00000000; - if (glfwGetKey(mWin->getGLFWWindow(), GLFW_KEY_W) == GLFW_PRESS) - movementDelta |= 0b10000000; - if (glfwGetKey(mWin->getGLFWWindow(), GLFW_KEY_A) == GLFW_PRESS) - movementDelta |= 0b01000000; - if (glfwGetKey(mWin->getGLFWWindow(), GLFW_KEY_S) == GLFW_PRESS) - movementDelta |= 0b00100000; - if (glfwGetKey(mWin->getGLFWWindow(), GLFW_KEY_D) == GLFW_PRESS) - movementDelta |= 0b00010000; - if (glfwGetKey(mWin->getGLFWWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) - movementDelta |= 0b00001000; - if (glfwGetKey(mWin->getGLFWWindow(), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) - movementDelta |= 0b00000100; + app->Input->MovementDelta = 0b00000000; + if (glfwGetKey(app->Win->getGLFWWindow(), GLFW_KEY_W) == GLFW_PRESS) + app->Input->MovementDelta |= 0b10000000; + if (glfwGetKey(app->Win->getGLFWWindow(), GLFW_KEY_A) == GLFW_PRESS) + app->Input->MovementDelta |= 0b01000000; + if (glfwGetKey(app->Win->getGLFWWindow(), GLFW_KEY_S) == GLFW_PRESS) + app->Input->MovementDelta |= 0b00100000; + if (glfwGetKey(app->Win->getGLFWWindow(), GLFW_KEY_D) == GLFW_PRESS) + app->Input->MovementDelta |= 0b00010000; + if (glfwGetKey(app->Win->getGLFWWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) + app->Input->MovementDelta |= 0b00001000; + if (glfwGetKey(app->Win->getGLFWWindow(), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) + app->Input->MovementDelta |= 0b00000100; } -void Inferno::stopMoveInput() +void stop_move_input(InfernoApp *app) { - movementDelta = 0x0; - mouseDelta = { 0.0f, 0.0f }; + app->Input->MovementDelta = 0x0; + app->Input->MouseDelta = { 0.0f, 0.0f }; } -int Inferno::run() +int run(InfernoApp *app) { - Material basicMaterial("basic"); - Shader basicShader; - basicShader.load("res/shaders/basic.glsl")->link(); - basicMaterial.setGlShader(&basicShader); - - Mesh cornell; - cornell.loadOBJ("res/cornell-box.obj"); - //cornell.loadOBJ("res/sponza.obj"); - cornell.ready(); - cornell.setMaterial(&basicMaterial); - mScene->addMesh(&cornell); - - // Mesh dragon; - // dragon.loadOBJ("res/dragon-cornell-size.obj"); - // dragon.ready(); - // dragon.setMaterial(&basicMaterial); - // mScene->addMesh(&dragon); - - Camera camera; - mScene->setCamera(&camera); - - mRasterRenderer->setScene(mScene); - mRayRenderer->getRenderer()->setScene(mScene); - - while (true) - { - if (!mWin->newFrame()) { break; } - camera.newFrame(); - - // set the main window to the dockspace and then on the first launch set the preset - ImGuiID dockspace_id = ImGui::GetID("main"); - static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode; - if (ImGui::DockBuilderGetNode(dockspace_id) == NULL) { this->uiPreset(); } - ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); - - // Menu Bar - static bool showPreview = true; - static bool showRenderSettings = true; - static bool showDemoWindow = false; - if (ImGui::BeginMenuBar()) - { - if (ImGui::BeginMenu("Menu")) - { - ImGui::EndMenu(); - } - if (ImGui::BeginMenu("View")) - { - ImGui::Checkbox("Show Preview", &showPreview); - ImGui::SameLine(); HelpMarker("Show the preview window"); - ImGui::Checkbox("Show Settings", &showRenderSettings); - ImGui::SameLine(); HelpMarker("Show the Inferno HART settings window"); - ImGui::Checkbox("Show Demo", &showDemoWindow); - - ImGui::EndMenu(); - } - ImGui::EndMenuBar(); - } - - if (showPreview && ImGui::Begin("Preview", nullptr, ImGuiWindowFlags_NoScrollbar)) - { - if (ImGui::IsWindowHovered()) - { - this->moveInput(); - } else - { - this->stopMoveInput(); - } - if (glm::length(mouseDelta) > 0.0f) camera.mouseMoved(mouseDelta); - if (movementDelta != 0b00000000) camera.moveCamera(movementDelta); - - camera.setRasterViewport({ImGui::GetWindowSize().x, ImGui::GetWindowSize().y}); - mRasterRenderer->setTargetSize({ImGui::GetWindowSize().x, ImGui::GetWindowSize().y}); - mRasterRenderer->prepare(); - mRasterRenderer->draw(); - ImGui::Image((ImTextureID)mRasterRenderer->getRenderedTexture(), - { mRasterRenderer->getTargetSize().x, mRasterRenderer->getTargetSize().y }, - ImVec2(0,1), ImVec2(1,0)); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - ImGui::End(); - } - - if (ImGui::Begin("Render", nullptr, ImGuiWindowFlags_NoScrollbar)) - { - ImGui::Image((ImTextureID)mRayRenderer->getLatestTexture(), - { mRayRenderer->getRenderer()->getTargetSize().x, - mRayRenderer->getRenderer()->getTargetSize().y }, - ImVec2(0,1), ImVec2(1,0)); - glBindTexture(GL_TEXTURE_2D, 0); - ImGui::End(); - } - - if (showRenderSettings && ImGui::Begin("Inferno HART")) - { - // start/stop - bool isRenderRunning = mRayRenderer->progressionStatus(); - if (isRenderRunning) - { - ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f); - ImGui::Button("Start Render"); ImGui::SameLine(); - ImGui::PopItemFlag(); ImGui::PopStyleVar(); - if (ImGui::Button("Stop Render")) - mRayRenderer->stopProgression(); - } else { - if (ImGui::Button("Start Render")) - mRayRenderer->startProgression(); - ImGui::SameLine(); - ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f); - ImGui::Button("Stop Render"); - ImGui::PopItemFlag(); ImGui::PopStyleVar(); - } - - if (ImGui::TreeNode("Render")) - { - // modules - HHM* hhm = mRayRenderer->getTopModule(); - if (ImGui::TreeNode("Accelerator")) - { - ImGui::Button("Find Accelerator..."); - ImGui::Text("Select Accelerator:"); - if (ImGui::BeginListBox("", ImVec2(-FLT_MIN, 3 * ImGui::GetTextLineHeightWithSpacing()))) - { - std::vector moduleNames = hhm->getModuleDirectory()->getModules(); - int active = hhm->getModuleDirectory()->getActiveIndex(); - for (int n = 0; n < moduleNames.size(); n++) - { - const bool isSelected = (active == n); - if (ImGui::Selectable(moduleNames[n].c_str(), isSelected)) - hhm->getModuleDirectory()->setActiveIndex(n); - if (isSelected) - ImGui::SetItemDefaultFocus(); - } - ImGui::EndListBox(); - } - auto* activeCredit = hhm->getModuleDirectory()->getActiveCredit(); - ImGui::Text(activeCredit->ModuleName.c_str()); - ImGui::SameLine(); - ImGui::Text("v%i.%i.%i", activeCredit->VersionMajor, - activeCredit->VersionMinor, - activeCredit->VersionBuild); - ImGui::BulletText(activeCredit->ModuleDesc.c_str()); - ImGui::BulletText("Authored by %s", activeCredit->AuthorName.c_str()); - - ImGui::TreePop(); - } - ImGui::TreePop(); - } - - if (ImGui::TreeNode("Camera")) - { - ImGui::PushItemWidth(100); - ImGui::Text("Camera Position X,Y,Z"); - - bool positionUpdated = false; - ImGui::DragFloat("X", &camera.Position.x, 0.01f, -FLT_MAX, FLT_MAX, "%.2f", ImGuiSliderFlags_None); ImGui::SameLine(); if (ImGui::IsItemEdited()) positionUpdated = true; - ImGui::DragFloat("Y", &camera.Position.y, 0.01f, -FLT_MAX, FLT_MAX, "%.2f", ImGuiSliderFlags_None); ImGui::SameLine(); if (ImGui::IsItemEdited()) positionUpdated = true; - ImGui::DragFloat("Z", &camera.Position.z, 0.01f, -FLT_MAX, FLT_MAX, "%.2f", ImGuiSliderFlags_None); if (ImGui::IsItemEdited()) positionUpdated = true; - if (positionUpdated) camera.setPosition(camera.Position); - - bool viewUpdated = false; - ImGui::Text("Camera Look Yaw, Pitch, Roll"); - ImGui::DragFloat("Yaw", &camera.Yaw, 0.01f, -FLT_MAX, FLT_MAX, "%.2f", ImGuiSliderFlags_None); ImGui::SameLine(); if (ImGui::IsItemEdited()) viewUpdated = true; - ImGui::DragFloat("Pitch", &camera.Pitch, 0.01f, -FLT_MAX, FLT_MAX, "%.2f", ImGuiSliderFlags_None); ImGui::SameLine(); if (ImGui::IsItemEdited()) viewUpdated = true; - ImGui::DragFloat("Roll", &camera.Roll, 0.01f, -FLT_MAX, FLT_MAX, "%.2f", ImGuiSliderFlags_None); if (ImGui::IsItemEdited()) viewUpdated = true; - - ImGui::PopItemWidth(); - ImGui::PushItemWidth(300); - - ImGui::Text("Camera Zoom"); - ImGui::DragFloat("Zoom", &camera.FOV, -0.1f, 0.01f, 180.0f, "%.2f", ImGuiSliderFlags_None); ImGui::SameLine(); if (ImGui::IsItemEdited()) viewUpdated = true; - if (viewUpdated) camera.update(); - - ImGui::PopItemWidth(); - ImGui::TreePop(); - } - ImGui::End(); - } - - if (showDemoWindow) - { - ImGui::ShowDemoWindow(); - } - - GLenum err; - while((err = glGetError()) != GL_NO_ERROR) { - std::string error; - switch (err) { - case GL_INVALID_ENUM: error = "INVALID_ENUM"; break; - case GL_INVALID_VALUE: error = "INVALID_VALUE"; break; - case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break; - case GL_STACK_OVERFLOW: error = "STACK_OVERFLOW"; break; - case GL_STACK_UNDERFLOW: error = "STACK_UNDERFLOW"; break; - case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break; - case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break; - default: error = std::to_string((uint32_t)err); break; - } - yolo::error("[GL]: {} {}", err, error); - } - - mWin->render(); - } - - return 0; + return 1; } + +} + diff --git a/src/inferno.hpp b/src/inferno.hpp index 0eeb685..31f1e74 100644 --- a/src/inferno.hpp +++ b/src/inferno.hpp @@ -4,42 +4,27 @@ #include -namespace inferno { +namespace inferno::core { class Window; class HHM; -class RasterizeRenderer; -class RenderDispatcher; -class Scene; +typedef struct InfernoInput { + glm::vec2 MouseDelta; + uint8_t MovementDelta; +} InfernoInput; -class Inferno : public helpers::Singleton -{ -public: - Inferno(); - ~Inferno(); +typedef struct InfernoApp { + Window* Win; + InfernoInput* Input; +} InfernoApp; - void uiPreset(); - - void moveInput(); - void stopMoveInput(); - int run(); - -public: - glm::vec2 mouseDelta; - // 0b00000000 - // 0bFLBRUDxx - uint8_t movementDelta; - -private: - // need deffered init as they need an OpenGL context - // could and should be fixed with a static window - RasterizeRenderer* mRasterRenderer; - RenderDispatcher* mRayRenderer; - Scene* mScene; - -private: - Window* mWin; -}; +InfernoApp* create_inferno(); +void cleanup_inferno(InfernoApp* app); +void preset_gui(InfernoApp* app); +void move_input(InfernoApp* app); +void stop_move_input(InfernoApp* app); +int run(InfernoApp* app); } + diff --git a/src/main.cpp b/src/main.cpp index 2d7a693..d76ce3b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,13 +5,7 @@ int main(int argc, char** argv) { - try - { - return inferno::Inferno::GetInstance().run(); - } - catch (std::exception e) - { - yolo::error(e.what()); - return -1; - } + auto* inferno = inferno::core::create_inferno(); + return inferno::core::run(inferno); } + diff --git a/src/window.cpp b/src/window.cpp index 121a470..87d6151 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,7 +4,7 @@ #include "yolo/yolo.hpp" -using namespace inferno; +using namespace inferno::core; Window::Window() {} diff --git a/src/window.hpp b/src/window.hpp index 6063da9..7bb1324 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -7,7 +7,7 @@ #define WINDOW_FLAGS ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoCollapse -namespace inferno { +namespace inferno::core { typedef void (*KeyCallback)(int key, int scan, int action, int mod); typedef void (*MouseCallback)(double x, double y);