From 49bc86433f433c1292881338af732cf74b284af9 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Fri, 31 Jan 2025 12:19:27 +0100 Subject: [PATCH] ~ | Imgui and other --- includes/RT/BVH.hpp | 7 +- includes/RT/Scene.hpp | 2 +- shaders/compute.glsl | 2 +- srcs/RT.cpp | 11 +-- srcs/class/BVH.cpp | 65 +++++++++++------ srcs/class/ObjParser.cpp | 4 +- srcs/class/Renderer.cpp | 16 +++-- srcs/class/Scene.cpp | 16 ++++- srcs/class/Window.cpp | 151 +++++++++++++++++++++------------------ 9 files changed, 156 insertions(+), 118 deletions(-) diff --git a/includes/RT/BVH.hpp b/includes/RT/BVH.hpp index 9e25938..1fcda73 100644 --- a/includes/RT/BVH.hpp +++ b/includes/RT/BVH.hpp @@ -40,8 +40,11 @@ struct BVHStats int min_triangles; int max_triangles; float average_triangles; + int min_depth; + int max_depth; + float average_depth; + int total_leaves; // Helper to calculate average depth }; - class BVH { public: @@ -55,7 +58,7 @@ class BVH int getSize(); int getLeaves(); - BVHStats analyzeBVHLeaves(BVH* root); + BVHStats analyzeBVHLeaves(BVH* root, int current_depth); void flatten(std::vector &bvhs, int ¤tIndex); GPUBvh toGPUBvh(); diff --git a/includes/RT/Scene.hpp b/includes/RT/Scene.hpp index 9467bbe..97173d6 100644 --- a/includes/RT/Scene.hpp +++ b/includes/RT/Scene.hpp @@ -113,7 +113,7 @@ class Scene void addMaterial(Material *material); void addTexture(std::string path); - void loadTextures(); + bool loadTextures(); void updateLightAndObjects(int mat_id); std::set getGPULights(); diff --git a/shaders/compute.glsl b/shaders/compute.glsl index bcde226..cc68830 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -176,7 +176,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state) { hitInfo hit = traceRay(ray); - #if 1 + #if 0 float t_scatter = 0.0; bool scatter_valid = bool(volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state)); if (scatter_valid) diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 679c94f..3bf92e1 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -99,16 +99,9 @@ int main(int argc, char **argv) glBindBuffer(GL_UNIFORM_BUFFER, debugUBO); glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUDebug), nullptr, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 2, debugUBO); - - try - { - scene.loadTextures(); - } - catch(const std::exception& e) - { - std::cerr << e.what() << std::endl; + + if (!scene.loadTextures()) return (1); - } shader.attach(); diff --git a/srcs/class/BVH.cpp b/srcs/class/BVH.cpp index 718fc3a..be3036e 100644 --- a/srcs/class/BVH.cpp +++ b/srcs/class/BVH.cpp @@ -78,7 +78,7 @@ void BVH::subdivide(std::vector &triangles) if (_primitive_count <= 4) return ; - const int num_test_per_axis = 5; + const int num_test_per_axis = 10; int best_axis = 0; float best_pos = 0; @@ -207,37 +207,56 @@ int BVH::getLeaves() return (count); } -//get tri per leaf min max and average -BVHStats BVH::analyzeBVHLeaves(BVH *root) +BVHStats BVH::analyzeBVHLeaves(BVH *root, int current_depth) { if (!root) - return {0, 0, 0.0f}; - // If this is a leaf node, return its stats + return {0, 0, 0.0f, INT_MAX, 0, 0.0f, 0}; + // If this is a leaf node, return its stats if (root->_is_leaf) return { - root->_primitive_count, // min - root->_primitive_count, // max - (float)root->_primitive_count // average + root->_primitive_count, // min triangles + root->_primitive_count, // max triangles + (float)root->_primitive_count, // average triangles + current_depth, // min depth + current_depth, // max depth + (float)current_depth, // depth sum (will be averaged later) + 1 // this leaf counts as 1 towards total leaves }; // Get stats from left and right subtrees - BVHStats left = analyzeBVHLeaves(root->_left); - BVHStats right = analyzeBVHLeaves(root->_right); + BVHStats left = analyzeBVHLeaves(root->_left, current_depth + 1); + BVHStats right = analyzeBVHLeaves(root->_right, current_depth + 1); // Combine the results - int min_count = std::min(left.min_triangles, right.min_triangles); - int max_count = std::max(left.max_triangles, right.max_triangles); + BVHStats combined; - // Calculate weighted average based on number of leaves in each subtree - float left_leaf_count = (left.average_triangles > 0) ? 1.0f : 0.0f; - float right_leaf_count = (right.average_triangles > 0) ? 1.0f : 0.0f; - float total_leaf_count = left_leaf_count + right_leaf_count; - - float avg_count = 0.0f; - if (total_leaf_count > 0) - avg_count = (left.average_triangles * left_leaf_count + - right.average_triangles * right_leaf_count) / total_leaf_count; + // Triangle statistics + combined.min_triangles = std::min(left.min_triangles, right.min_triangles); + combined.max_triangles = std::max(left.max_triangles, right.max_triangles); - return {min_count, max_count, avg_count}; -} + // Depth statistics + combined.min_depth = std::min(left.min_depth, right.min_depth); + combined.max_depth = std::max(left.max_depth, right.max_depth); + + // Count total leaves + combined.total_leaves = left.total_leaves + right.total_leaves; + + // Calculate weighted averages + if (combined.total_leaves > 0) { + // Average triangles + combined.average_triangles = + (left.average_triangles * left.total_leaves + + right.average_triangles * right.total_leaves) / combined.total_leaves; + + // Average depth + combined.average_depth = + (left.average_depth * left.total_leaves + + right.average_depth * right.total_leaves) / combined.total_leaves; + } else { + combined.average_triangles = 0.0f; + combined.average_depth = 0.0f; + } + + return combined; +} \ No newline at end of file diff --git a/srcs/class/ObjParser.cpp b/srcs/class/ObjParser.cpp index 022d998..3b3b487 100644 --- a/srcs/class/ObjParser.cpp +++ b/srcs/class/ObjParser.cpp @@ -284,7 +284,9 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) float x, y, z; if(!(lineStream >> x >> y >> z)) throw std::runtime_error("OBJ: syntax error while getting material emission"); - mat->emission = (x + y + z) / 3; + mat->emission = (x + y + z); + // if (mat->emission > 0.0) + // mat->color = glm::vec3(x, y, z); } else if(identifier == "Ni") { diff --git a/srcs/class/Renderer.cpp b/srcs/class/Renderer.cpp index 2fe09d7..d90692e 100644 --- a/srcs/class/Renderer.cpp +++ b/srcs/class/Renderer.cpp @@ -621,11 +621,13 @@ void Renderer::imguiRenderInfo(void) void Renderer::renderImgui(void) { - ImGui::Begin("Renderer"); - - if(rendering()) - imguiRenderInfo(); - else - imguiPathCreation(); - ImGui::End(); + // ImGui::Begin("Renderer"); + if (ImGui::CollapsingHeader("Renderer")) + { + if(rendering()) + imguiRenderInfo(); + else + imguiPathCreation(); + } + // ImGui::End(); } diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index dee7026..7dccb13 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -194,10 +194,15 @@ void Scene::addBvh(std::vector &triangles, glm::vec3 offset, float sc std::cout << "\tBVH size: " << bvh->getSize() << std::endl; std::cout << "\tBVH leaves: " << bvh->getLeaves() << std::endl << std::endl; - BVHStats stats = bvh->analyzeBVHLeaves(bvh); + BVHStats stats = bvh->analyzeBVHLeaves(bvh, 0); std::cout << "\tMin triangles per leaf: " << stats.min_triangles << std::endl; std::cout << "\tMax triangles per leaf: " << stats.max_triangles << std::endl; std::cout << "\tAverage triangles per leaf: " << stats.average_triangles << std::endl << std::endl; + + std::cout << "\n\tMin depth: " << stats.min_depth << std::endl; + std::cout << "\tMax depth: " << stats.max_depth << std::endl; + std::cout << "\tAverage depth: " << stats.average_depth << std::endl; + } @@ -220,7 +225,7 @@ void Scene::addTexture(std::string path) _textures.push_back(path); } -void Scene::loadTextures() +bool Scene::loadTextures() { for (std::string &path : _textures) { @@ -228,7 +233,10 @@ void Scene::loadTextures() unsigned char* image = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb_alpha); if (!image) - throw std::runtime_error("Failed to load texture " + path); + { + std::cout << "Failed to load texture " << path << std::endl; + return (false); + } std::cout << "Loaded texture: " << path << " (" << width << "x" << height << ")" << std::endl; @@ -248,6 +256,8 @@ void Scene::loadTextures() stbi_image_free(image); } + + return (true); } void Scene::updateLightAndObjects(int mat_id) diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 28b4f74..bfc3fa1 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -180,94 +180,103 @@ void Window::imGuiRender() { bool has_changed = false; - ImGui::Begin("Camera"); + ImGui::Begin("Settings"); ImGui::Text("Fps: %d", int(_fps)); ImGui::Text("Frame: %d", _frameCount); ImGui::Text("Objects: %lu", _scene->getObjectData().size() + _scene->getTriangleData().size()); - ImGui::Separator(); - if (ImGui::Checkbox("Accumulate", &accumulate)) - _frameCount = 0; + ImGui::Spacing(); - has_changed |= ImGui::SliderInt("Bounce", &_scene->getCamera()->getBounce(), 0, 20); - has_changed |= ImGui::SliderFloat("FOV", &_scene->getCamera()->getFov(), 1.0f, 180.0f); - has_changed |= ImGui::SliderFloat("Aperture", &_scene->getCamera()->getAperture(), 0.0f, 1.0f); - has_changed |= ImGui::SliderFloat("Focus", &_scene->getCamera()->getFocus(), 0.0f, 150.0f); - - ImGui::End(); - - ImGui::Begin("Material"); - - for (unsigned int i = 0; i < _scene->getMaterialData().size(); i++) + if (ImGui::CollapsingHeader("Camera")) { - GPUMaterial &mat = _scene->getMaterialData()[i]; - ImGui::PushID(i); - - ImGui::Text("Material %d", i); - has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]); - if (ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f)) - { - has_changed = 1; - _scene->updateLightAndObjects(i); - } - - if (mat.type == 0) - { - has_changed |= ImGui::SliderFloat("Roughness", &mat.roughness, 0.0f, 1.0f); - has_changed |= ImGui::SliderFloat("Metallic", &mat.metallic, 0.0f, 1.0f); - } - else if (mat.type == 1) - has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 5.0f); - else if (mat.type == 2) - { - has_changed |= ImGui::SliderFloat("Transparency", &mat.roughness, 0.0f, 1.0f); - has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 2.0f); - has_changed |= ImGui::SliderFloat("Proba", &mat.metallic, 0., 1.); - } - has_changed |= ImGui::SliderInt("Type", &mat.type, 0, 2); + if (ImGui::Checkbox("Accumulate", &accumulate)) + _frameCount = 0; - ImGui::PopID(); + has_changed |= ImGui::SliderInt("Bounce", &_scene->getCamera()->getBounce(), 0, 20); + has_changed |= ImGui::SliderFloat("FOV", &_scene->getCamera()->getFov(), 1.0f, 180.0f); + has_changed |= ImGui::SliderFloat("Aperture", &_scene->getCamera()->getAperture(), 0.0f, 1.0f); + has_changed |= ImGui::SliderFloat("Focus", &_scene->getCamera()->getFocus(), 0.0f, 150.0f); + } + + if (ImGui::CollapsingHeader("Material")) + { + + ImGui::BeginChild("Header", ImVec2(0, 400), true, 0); + + for (unsigned int i = 0; i < _scene->getMaterialData().size(); i++) + { + GPUMaterial &mat = _scene->getMaterialData()[i]; + + ImGui::PushID(i); + + ImGui::Text("Material %d", i); + has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]); + if (ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f)) + { + has_changed = 1; + _scene->updateLightAndObjects(i); + } + + if (mat.type == 0) + { + has_changed |= ImGui::SliderFloat("Roughness", &mat.roughness, 0.0f, 1.0f); + has_changed |= ImGui::SliderFloat("Metallic", &mat.metallic, 0.0f, 1.0f); + } + else if (mat.type == 1) + has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 5.0f); + else if (mat.type == 2) + { + has_changed |= ImGui::SliderFloat("Transparency", &mat.roughness, 0.0f, 1.0f); + has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 2.0f); + has_changed |= ImGui::SliderFloat("Proba", &mat.metallic, 0., 1.); + } + has_changed |= ImGui::SliderInt("Type", &mat.type, 0, 2); + + ImGui::PopID(); + + ImGui::Separator(); + } + ImGui::EndChild(); + + } + + if (ImGui::CollapsingHeader("Fog")) + { + has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getVolume().enabled)); ImGui::Separator(); + + if (ImGui::SliderFloat("Absorption", &_scene->getVolume().sigma_a.x, 0., 0.1)) + { + _scene->getVolume().sigma_a = glm::vec3(_scene->getVolume().sigma_a.x); + _scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s; + has_changed = true; + } + if (ImGui::SliderFloat("Scattering", &_scene->getVolume().sigma_s.x, 0., 0.5)) + { + _scene->getVolume().sigma_s = glm::vec3(_scene->getVolume().sigma_s.x); + _scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s; + has_changed = true; + } + if (ImGui::SliderFloat("G", &_scene->getVolume().g, 0., 1.)) + has_changed = true; } - - ImGui::End(); - ImGui::Begin("Fog settings"); - - has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getVolume().enabled)); - ImGui::Separator(); - - if (ImGui::SliderFloat("Absorption", &_scene->getVolume().sigma_a.x, 0., 0.1)) + + if (ImGui::CollapsingHeader("Debug")) { - _scene->getVolume().sigma_a = glm::vec3(_scene->getVolume().sigma_a.x); - _scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s; - has_changed = true; + has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getDebug().enabled)); + ImGui::Separator(); + has_changed |= ImGui::SliderInt("Debug mode", &_scene->getDebug().mode, 0, 2); + has_changed |= ImGui::SliderInt("Box treshold", &_scene->getDebug().box_treshold, 1, 2000); + has_changed |= ImGui::SliderInt("Triangle treshold", &_scene->getDebug().triangle_treshold, 1, 2000); } - if (ImGui::SliderFloat("Scattering", &_scene->getVolume().sigma_s.x, 0., 0.5)) - { - _scene->getVolume().sigma_s = glm::vec3(_scene->getVolume().sigma_s.x); - _scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s; - has_changed = true; - } - if (ImGui::SliderFloat("G", &_scene->getVolume().g, 0., 1.)) - has_changed = true; - - ImGui::End(); - - ImGui::Begin("Debug BVH"); - - has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getDebug().enabled)); - ImGui::Separator(); - has_changed |= ImGui::SliderInt("Debug mode", &_scene->getDebug().mode, 0, 2); - has_changed |= ImGui::SliderInt("Box treshold", &_scene->getDebug().box_treshold, 1, 2000); - has_changed |= ImGui::SliderInt("Triangle treshold", &_scene->getDebug().triangle_treshold, 1, 2000); - - ImGui::End(); _renderer->renderImgui();; + + ImGui::End(); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());