From 00f806d7a494158348c26be6c0e8b33edb28aeca Mon Sep 17 00:00:00 2001 From: knightcrawler25 Date: Sat, 1 Jan 2022 19:03:31 +0530 Subject: [PATCH] Picklist for environment maps --- src/Main.cpp | 47 ++++++++++++++++++++++++++++++++++++++++------ src/core/Scene.cpp | 7 ++++--- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index e9a673d..d7205ed 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -53,16 +53,19 @@ Scene* scene = nullptr; Renderer* renderer = nullptr; std::vector sceneFiles; +std::vector envMaps; float mouseSensitivity = 0.01f; bool keyPressed = false; -int sampleSceneIndex = 0; +int sampleSceneIdx = 0; int selectedInstance = 0; double lastTime = SDL_GetTicks(); +int envMapIdx = 0; bool done = false; std::string shadersDir = "../src/shaders/"; std::string assetsDir = "../assets/"; +std::string envMapDir = "../assets/HDR/"; RenderOptions renderOptions; @@ -93,6 +96,27 @@ void GetSceneFiles() tinydir_close(&dir); } +void GetEnvMaps() +{ + tinydir_dir dir; + int i; + tinydir_open_sorted(&dir, envMapDir.c_str()); + + for (i = 0; i < dir.n_files; i++) + { + tinydir_file file; + tinydir_readfile_n(&dir, &file, i); + + std::string ext = std::string(file.extension); + if (ext == "hdr") + { + envMaps.push_back(envMapDir + std::string(file.name)); + } + } + + tinydir_close(&dir); +} + void LoadScene(std::string sceneName) { delete scene; @@ -304,20 +328,30 @@ void MainLoop(void* arg) SaveFrame("./img_" + to_string(renderer->GetSampleCount()) + ".png"); } + // Scenes std::vector scenes; for (int i = 0; i < sceneFiles.size(); ++i) - { scenes.push_back(sceneFiles[i].c_str()); - } - if (ImGui::Combo("Scene", &sampleSceneIndex, scenes.data(), scenes.size())) + if (ImGui::Combo("Scene", &sampleSceneIdx, scenes.data(), scenes.size())) { - LoadScene(sceneFiles[sampleSceneIndex]); + LoadScene(sceneFiles[sampleSceneIdx]); SDL_RestoreWindow(loopdata.mWindow); SDL_SetWindowSize(loopdata.mWindow, renderOptions.windowResolution.x, renderOptions.windowResolution.y); InitRenderer(); } + // Environment maps + std::vector envMapsList; + for (int i = 0; i < envMaps.size(); ++i) + envMapsList.push_back(envMaps[i].c_str()); + + if (ImGui::Combo("EnvMaps", &envMapIdx, envMapsList.data(), envMapsList.size())) + { + scene->AddHDR(envMaps[envMapIdx]); + InitRenderer(); + } + bool optionsChanged = false; optionsChanged |= ImGui::SliderFloat("Mouse Sensitivity", &mouseSensitivity, 0.001f, 1.0f); @@ -498,7 +532,8 @@ int main(int argc, char** argv) else { GetSceneFiles(); - LoadScene(sceneFiles[sampleSceneIndex]); + GetEnvMaps(); + LoadScene(sceneFiles[sampleSceneIdx]); } // Setup SDL diff --git a/src/core/Scene.cpp b/src/core/Scene.cpp index f294a0c..c0c65ef 100644 --- a/src/core/Scene.cpp +++ b/src/core/Scene.cpp @@ -224,7 +224,8 @@ namespace GLSLPT printf("Copying Mesh Data\n"); for (int i = 0; i < meshes.size(); i++) { - // Copy indices from BVH and not from Mesh + // Copy indices from BVH and not from Mesh. + // Required if splitBVH is used as a triangle can be shared by leaf nodes int numIndices = meshes[i]->bvh->GetNumIndices(); const int * triIndices = meshes[i]->bvh->GetIndices(); @@ -276,9 +277,9 @@ namespace GLSLPT if (!camera) { RadeonRays::bbox bounds = sceneBvh->Bounds(); - Vec3 extents = bounds.extents() * 5.0f; + Vec3 extents = bounds.extents(); Vec3 center = bounds.center(); - AddCamera(Vec3(0, center.y, extents.x), center, 45.0f); + AddCamera(Vec3(center.x, center.y, center.z + Vec3::Length(extents) * 2.0f), center, 45.0f); } initialized = true;