From e32e5e77d1979500a1101966fe7ec98caca828cd Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Tue, 18 Jun 2024 17:57:42 +0300 Subject: [PATCH] 3D scene stable --- 3DEditor/applications/Entry.cpp | 2 +- 3DEditor/rsc/scene/meshes.mtl | 20 ++++++++ 3DEditor/rsc/{scene.obj => scene/meshes.obj} | 0 3DEditor/rsc/scene/script.lua | 25 ++++++++++ 3DScene/CMakeLists.txt | 2 +- .../private/LuaFormat.cpp | 46 ++++++++++--------- .../private/{SceneIO.cpp => OBJFormat.cpp} | 2 +- 3DScene/private/Scene.cpp | 6 ++- 3DScene/public/Scene.hpp | 12 +++++ RayTracer/CMakeLists.txt | 2 +- RayTracer/applications/Rayt.cpp | 8 ++-- RayTracer/applications/Rayt.hpp | 7 --- RayTracer/public/RayTracer.hpp | 7 --- RayTracer/tests/Test.cpp | 2 +- 14 files changed, 96 insertions(+), 45 deletions(-) create mode 100644 3DEditor/rsc/scene/meshes.mtl rename 3DEditor/rsc/{scene.obj => scene/meshes.obj} (100%) create mode 100644 3DEditor/rsc/scene/script.lua rename RayTracer/applications/SettingsLoad.cpp => 3DScene/private/LuaFormat.cpp (87%) rename 3DScene/private/{SceneIO.cpp => OBJFormat.cpp} (95%) delete mode 100644 RayTracer/applications/Rayt.hpp diff --git a/3DEditor/applications/Entry.cpp b/3DEditor/applications/Entry.cpp index ea0a130..a1f2bc2 100644 --- a/3DEditor/applications/Entry.cpp +++ b/3DEditor/applications/Entry.cpp @@ -13,7 +13,7 @@ public: auto canvas = this->mGraphics->getCanvas(); mGui = new EditorWidget(canvas, &mScene, renderResolution); - mScene.load("rsc/scene.obj"); + mScene.load("rsc/scene/script.lua"); mScene.mCamera.lookAtPoint({ 0, 0, 0 }, { 3, 3, 2 }, { 0, 0, 1 }); } diff --git a/3DEditor/rsc/scene/meshes.mtl b/3DEditor/rsc/scene/meshes.mtl new file mode 100644 index 0000000..b2d7e64 --- /dev/null +++ b/3DEditor/rsc/scene/meshes.mtl @@ -0,0 +1,20 @@ +# Blender MTL File: 'scene.blend' +# Material Count: 2 + +newmtl Material +Ns 323.999994 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl None +Ns 500 +Ka 0.8 0.8 0.8 +Kd 0.8 0.8 0.8 +Ks 0.8 0.8 0.8 +d 1 +illum 2 diff --git a/3DEditor/rsc/scene.obj b/3DEditor/rsc/scene/meshes.obj similarity index 100% rename from 3DEditor/rsc/scene.obj rename to 3DEditor/rsc/scene/meshes.obj diff --git a/3DEditor/rsc/scene/script.lua b/3DEditor/rsc/scene/script.lua new file mode 100644 index 0000000..f0146f9 --- /dev/null +++ b/3DEditor/rsc/scene/script.lua @@ -0,0 +1,25 @@ + +Meshes = "meshes.obj" + +Camera = { + pos = { 0.5, 4.5, 0.2 }, + size_x = 600, + size_y = 800, +} + +Lights = { + { + pos = { -0.5, 3.5, 1 }, + intensity = 1 + }, + { + pos = { 0, 0, 1 }, + intensity = 0.5 + }, +} + +RenderSettings = { + depth = 1, + spray = 3, + multisampling = 16, +} \ No newline at end of file diff --git a/3DScene/CMakeLists.txt b/3DScene/CMakeLists.txt index 90033fb..f4bf146 100644 --- a/3DScene/CMakeLists.txt +++ b/3DScene/CMakeLists.txt @@ -10,4 +10,4 @@ target_include_directories(${PROJECT_NAME} PUBLIC ./public/) target_include_directories(${PROJECT_NAME} PRIVATE ../Externals/) target_link_libraries(${PROJECT_NAME} PUBLIC Math) -target_link_libraries(${PROJECT_NAME} PUBLIC ${BINDINGS_LIBS}) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} PRIVATE Lua) \ No newline at end of file diff --git a/RayTracer/applications/SettingsLoad.cpp b/3DScene/private/LuaFormat.cpp similarity index 87% rename from RayTracer/applications/SettingsLoad.cpp rename to 3DScene/private/LuaFormat.cpp index 1b721bd..3d61aad 100644 --- a/RayTracer/applications/SettingsLoad.cpp +++ b/3DScene/private/LuaFormat.cpp @@ -1,5 +1,5 @@ -#include "Rayt.hpp" +#include "Scene.hpp" extern "C" { #include "lauxlib.h" @@ -9,7 +9,7 @@ extern "C" { #include // Function to read a Lua table representing RenderSettings -int readRenderSettings(lua_State* L, tp::RayTracer::RenderSettings& settings) { +int readRenderSettings(lua_State* L, tp::RenderSettings& settings) { lua_getglobal(L, "RenderSettings"); if (!lua_istable(L, -1)) { printf("RenderSettings is not a table.\n"); @@ -83,7 +83,7 @@ int readLight(lua_State* L, tp::PointLight* light) { return 1; // Success } -void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::RenderSettings& settings) { +bool tp::Scene::loadLuaFormat(const std::string& scenePath) { lua_State* L = luaL_newstate(); luaL_openlibs(L); @@ -100,7 +100,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (luaL_dofile(L, scenePath.c_str()) != 0) { lua_close(L); printf("Cant open scene script.\n"); - return; + return false; } lua_getglobal(L, "Meshes"); @@ -110,14 +110,14 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re directoryPath /= meshesPath; - if (!scene.load(directoryPath.string())) { + if (!loadOBJFormat(directoryPath.string())) { printf("No 'meshes' loaded - check ur .obj path and validate content of .obj .\n"); - return; + return false; } } else { printf("No 'meshes' path given.\n"); - return; + return false; } // --- camera @@ -127,7 +127,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (!lua_istable(L, -1)) { printf("Camera is not a table.\n"); lua_close(L); - return; + return false; } // Verify you are inside the "Camera" table @@ -138,7 +138,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (!lua_istable(L, -1) || lua_rawlen(L, -1) != 3) { printf("Invalid 'pos' field in Camera table.\n"); lua_close(L); - return; + return false; } // Read the values from the table @@ -150,7 +150,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re } else { printf("Invalid 'pos' field value at index %d.\n", i + 1); lua_close(L); - return; + return false; } lua_pop(L, 1); } @@ -160,7 +160,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (!lua_isnumber(L, -1)) { printf("Invalid or missing 'size_x' field in Camera table.\n"); lua_close(L); - return; + return false; } int size_x = lua_tointeger(L, -1); lua_pop(L, 1); // Pop the 'size_x' value from the stack @@ -170,16 +170,16 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (!lua_isnumber(L, -1)) { printf("Invalid or missing 'size_y' field in Camera table.\n"); lua_close(L); - return; + return false; } int size_y = lua_tointeger(L, -1); - settings.size = { (tp::halnf) size_x, (tp::halnf) size_y }; + mRenderSettings.size = { (tp::halnf) size_x, (tp::halnf) size_y }; - scene.mCamera.lookAtPoint({ 0, 0, 0 }, { pos[0], pos[1], pos[2] }, { 0, 0, 1 }); - scene.mCamera.setFOV(3.14 / 4); - scene.mCamera.setFar(100); - scene.mCamera.setRatio((tp::halnf) size_y / (tp::halnf) size_x); + mCamera.lookAtPoint({ 0, 0, 0 }, { pos[0], pos[1], pos[2] }, { 0, 0, 1 }); + mCamera.setFOV(3.14 / 4); + mCamera.setFar(100); + mCamera.setRatio((tp::halnf) size_y / (tp::halnf) size_x); // ---------- LIGHTS { @@ -187,7 +187,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (!lua_istable(L, -1)) { printf("Lights is not a table.\n"); lua_close(L); - return; // Error + return false; // Error } // Read and process each light in the "Lights" table @@ -199,20 +199,22 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re if (!readLight(L, &light)) { printf("Cant read lights data\n"); lua_close(L); - return; // Error + return false; // Error } - scene.mLights.append(light); + mLights.append(light); } lua_pop(L, 1); // Pop the i-th light table } } // ----------- settings -------------- - if (!readRenderSettings(L, settings)) { + if (!readRenderSettings(L, mRenderSettings)) { printf("Cant Read Render Settings"); lua_close(L); - return; // Error + return false; // Error } lua_close(L); + + return true; } diff --git a/3DScene/private/SceneIO.cpp b/3DScene/private/OBJFormat.cpp similarity index 95% rename from 3DScene/private/SceneIO.cpp rename to 3DScene/private/OBJFormat.cpp index b81002f..3fcf8ad 100644 --- a/3DScene/private/SceneIO.cpp +++ b/3DScene/private/OBJFormat.cpp @@ -10,7 +10,7 @@ extern "C" { #include -bool tp::Scene::load(const std::string& objetsPath) { +bool tp::Scene::loadOBJFormat(const std::string& objetsPath) { using namespace tp; objl::Loader Loader; diff --git a/3DScene/private/Scene.cpp b/3DScene/private/Scene.cpp index 236c230..f1927b2 100644 --- a/3DScene/private/Scene.cpp +++ b/3DScene/private/Scene.cpp @@ -1,2 +1,6 @@ -#include "Scene.hpp" \ No newline at end of file +#include "Scene.hpp" + +bool tp::Scene::load(const std::string& scenePath) { + return loadLuaFormat(scenePath); +} \ No newline at end of file diff --git a/3DScene/public/Scene.hpp b/3DScene/public/Scene.hpp index 4d222d4..f7aa56f 100644 --- a/3DScene/public/Scene.hpp +++ b/3DScene/public/Scene.hpp @@ -7,6 +7,13 @@ #include namespace tp { + struct RenderSettings { + uhalni depth = 2; + uhalni spray = 1; + ualni multisampling = 1; + Vec2 size; + }; + class GPUBuffers { public: GPUBuffers() = default; @@ -41,9 +48,14 @@ namespace tp { bool load(const std::string& scenePath); + bool loadLuaFormat(const std::string& scenePath); + bool loadOBJFormat(const std::string& objectsPath); + public: Buffer mObjects; Buffer mLights; Camera mCamera; + + RenderSettings mRenderSettings; }; } diff --git a/RayTracer/CMakeLists.txt b/RayTracer/CMakeLists.txt index bbf72ea..5e4d34d 100644 --- a/RayTracer/CMakeLists.txt +++ b/RayTracer/CMakeLists.txt @@ -10,7 +10,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC 3DScene Connection) ### -------------------------- Applications -------------------------- ### file(GLOB APP_SOURCES "./applications/*.cpp") add_executable(rayt ${APP_SOURCES}) -target_link_libraries(rayt ${PROJECT_NAME} Lua ImageIO) +target_link_libraries(rayt ${PROJECT_NAME} ImageIO) file(COPY "rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") ### -------------------------- Tests -------------------------- ### diff --git a/RayTracer/applications/Rayt.cpp b/RayTracer/applications/Rayt.cpp index eeb1ed2..a5c40a2 100644 --- a/RayTracer/applications/Rayt.cpp +++ b/RayTracer/applications/Rayt.cpp @@ -1,7 +1,7 @@ // #include "NewPlacement.hpp" -#include "Rayt.hpp" +#include "RayTracer.hpp" #include "Timing.hpp" @@ -53,9 +53,11 @@ void printStatus(const halnf* percentage) { void renderCommand(const std::string& scenePath) { Scene scene; - RayTracer::RenderSettings settings; + RenderSettings& settings = scene.mRenderSettings; - loadScene(scene, scenePath, settings); + if (!scene.load(scenePath)) { + return; + } RayTracer::OutputBuffers output; RayTracer rayt; diff --git a/RayTracer/applications/Rayt.hpp b/RayTracer/applications/Rayt.hpp deleted file mode 100644 index ad43ea5..0000000 --- a/RayTracer/applications/Rayt.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include "RayTracer.hpp" - -#include - -void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::RenderSettings& settings); diff --git a/RayTracer/public/RayTracer.hpp b/RayTracer/public/RayTracer.hpp index c667ab1..76680c0 100644 --- a/RayTracer/public/RayTracer.hpp +++ b/RayTracer/public/RayTracer.hpp @@ -16,13 +16,6 @@ namespace tp { halnf percentage = 0.f; } mProgress; - struct RenderSettings { - uhalni depth = 2; - uhalni spray = 1; - ualni multisampling = 1; - Vec2 size; - }; - struct OutputBuffers { RenderBuffer normals; RenderBuffer color; diff --git a/RayTracer/tests/Test.cpp b/RayTracer/tests/Test.cpp index 170ef1c..be65e18 100644 --- a/RayTracer/tests/Test.cpp +++ b/RayTracer/tests/Test.cpp @@ -80,7 +80,7 @@ SUITE(RayTracer) { object.mCache.Source = &object.mTopology; object.mCache.updateCache(); - RayTracer::RenderSettings settings = { + RenderSettings settings = { 0, 0, 1,