From 55f5537a678b1155af871dbabd8e62319f3a86df Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Tue, 18 Jun 2024 17:16:23 +0300 Subject: [PATCH] 3D Scene initial --- 3DScene/CMakeLists.txt | 13 +++++ 3DScene/private/Scene.cpp | 2 + 3DScene/private/SceneIO.cpp | 51 +++++++++++++++++++ 3DScene/public/Scene.hpp | 43 ++++++++++++++++ CMakeLists.txt | 3 +- RayTracer/CMakeLists.txt | 7 +-- .../{SceneLoad.cpp => SettingsLoad.cpp} | 44 +--------------- RayTracer/public/RayTracer.hpp | 31 +---------- 8 files changed, 117 insertions(+), 77 deletions(-) create mode 100644 3DScene/CMakeLists.txt create mode 100644 3DScene/private/Scene.cpp create mode 100644 3DScene/private/SceneIO.cpp create mode 100644 3DScene/public/Scene.hpp rename RayTracer/applications/{SceneLoad.cpp => SettingsLoad.cpp} (80%) diff --git a/3DScene/CMakeLists.txt b/3DScene/CMakeLists.txt new file mode 100644 index 0000000..90033fb --- /dev/null +++ b/3DScene/CMakeLists.txt @@ -0,0 +1,13 @@ +project(3DScene) + +### ---------------------- Static Library --------------------- ### +file(GLOB SOURCES "./private/*.cpp") +file(GLOB HEADERS "./public/*.hpp") + +add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) + +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 diff --git a/3DScene/private/Scene.cpp b/3DScene/private/Scene.cpp new file mode 100644 index 0000000..236c230 --- /dev/null +++ b/3DScene/private/Scene.cpp @@ -0,0 +1,2 @@ + +#include "Scene.hpp" \ No newline at end of file diff --git a/3DScene/private/SceneIO.cpp b/3DScene/private/SceneIO.cpp new file mode 100644 index 0000000..b81002f --- /dev/null +++ b/3DScene/private/SceneIO.cpp @@ -0,0 +1,51 @@ + +#include "Scene.hpp" + +extern "C" { +#include "lauxlib.h" +#include "lualib.h" +} + +#include "obj/OBJ_Loader.h" + +#include + +bool tp::Scene::load(const std::string& objetsPath) { + using namespace tp; + + objl::Loader Loader; + + if (!Loader.LoadFile(objetsPath)) { + std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n"; + return false; + } + + for (auto& curMesh : Loader.LoadedMeshes) { + mObjects.append(Object()); + + auto object = &mObjects.last(); + + for (auto& vertex : curMesh.Vertices) { + // printf("{ %f, %f, %f }, \n", vertex.Position.X, vertex.Position.Y, vertex.Position.Z); + object->mTopology.Points.append(Vec3F{ vertex.Position.X, vertex.Position.Y, vertex.Position.Z }); + object->mTopology.Normals.append(Vec3F{ vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z }); + } + + for (int j = 0; j < curMesh.Indices.size(); j += 3) { + auto idx1 = (uhalni) curMesh.Indices[j]; + auto idx2 = (uhalni) curMesh.Indices[j + 1]; + auto idx3 = (uhalni) curMesh.Indices[j + 2]; + // printf("{ %i, %i, %i },\n", idx1, idx2, idx3); + object->mTopology.Indexes.append(Vec3{ idx1, idx2, idx3 }); + } + + if (object->mTopology.Normals.size() != object->mTopology.Points.size()) { + printf("Logic error loading normals\n"); + } + + object->mCache.Source = &object->mTopology; + object->mCache.updateCache(); + } + + return mObjects.size(); +} \ No newline at end of file diff --git a/3DScene/public/Scene.hpp b/3DScene/public/Scene.hpp new file mode 100644 index 0000000..5efda2a --- /dev/null +++ b/3DScene/public/Scene.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "Topology.hpp" +#include "Buffer2D.hpp" +#include "Camera.hpp" + +#include + +namespace tp { + class GPUBuffers { + public: + GPUBuffers() = default; + virtual ~GPUBuffers() = default; + }; + + class Object { + public: + Object() = default; + + public: + Topology mTopology; + TopologyCache mCache; + GPUBuffers* mGUPBuffers = nullptr; + }; + + struct PointLight { + Vec3F pos; + halnf fallOut = 1.f; + halnf intensity = 1.f; + }; + + class Scene { + public: + Scene() = default; + + bool load(const std::string& scenePath); + + public: + Buffer mObjects; + Buffer mLights; + Camera mCamera; + }; +} diff --git a/CMakeLists.txt b/CMakeLists.txt index c211e67..a8086b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,11 +17,12 @@ add_subdirectory(Math) add_subdirectory(Externals) add_subdirectory(Connection) add_subdirectory(Graphics) -add_subdirectory(RayTracer) add_subdirectory(DataAnalysis) add_subdirectory(Objects) add_subdirectory(Widgets) add_subdirectory(LibraryViewer) add_subdirectory(RasterRender) +add_subdirectory(3DScene) +add_subdirectory(RayTracer) add_subdirectory(Sketch3D) add_subdirectory(3DEditor) diff --git a/RayTracer/CMakeLists.txt b/RayTracer/CMakeLists.txt index ae8c52b..976834c 100644 --- a/RayTracer/CMakeLists.txt +++ b/RayTracer/CMakeLists.txt @@ -4,11 +4,12 @@ project(RayTracer) file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp") file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp" "./applications/*.hpp") add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) -target_include_directories(${PROJECT_NAME} PUBLIC ./public/ ../Externals/) -target_link_libraries(${PROJECT_NAME} PUBLIC Math Connection) +target_include_directories(${PROJECT_NAME} PUBLIC ./public/) +target_link_libraries(${PROJECT_NAME} PUBLIC 3DScene Connection) ### -------------------------- Applications -------------------------- ### -add_executable(rayt ./applications/Rayt.cpp applications/SceneLoad.cpp applications/Rayt.hpp) +file(GLOB APP_SOURCES "./applications/*.cpp") +add_executable(rayt ${APP_SOURCES}) target_link_libraries(rayt ${PROJECT_NAME} Lua ImageIO) file(COPY "applications/rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") diff --git a/RayTracer/applications/SceneLoad.cpp b/RayTracer/applications/SettingsLoad.cpp similarity index 80% rename from RayTracer/applications/SceneLoad.cpp rename to RayTracer/applications/SettingsLoad.cpp index 54ea3c1..1b721bd 100644 --- a/RayTracer/applications/SceneLoad.cpp +++ b/RayTracer/applications/SettingsLoad.cpp @@ -6,50 +6,8 @@ extern "C" { #include "lualib.h" } -#include "obj/OBJ_Loader.h" - #include -bool loadMeshes(tp::Scene& scene, const std::string& objetsPath) { - using namespace tp; - - objl::Loader Loader; - - if (!Loader.LoadFile(objetsPath.c_str())) { - std::cout << "Failed to Load File. May have failed to find it or it was not an .obj file.\n"; - return false; - } - - for (auto& curMesh : Loader.LoadedMeshes) { - scene.mObjects.append(Object()); - - auto object = &scene.mObjects.last(); - - for (auto& vertex : curMesh.Vertices) { - // printf("{ %f, %f, %f }, \n", vertex.Position.X, vertex.Position.Y, vertex.Position.Z); - object->mTopology.Points.append(Vec3F{ vertex.Position.X, vertex.Position.Y, vertex.Position.Z }); - object->mTopology.Normals.append(Vec3F{ vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z }); - } - - for (int j = 0; j < curMesh.Indices.size(); j += 3) { - auto idx1 = (uhalni) curMesh.Indices[j]; - auto idx2 = (uhalni) curMesh.Indices[j + 1]; - auto idx3 = (uhalni) curMesh.Indices[j + 2]; - // printf("{ %i, %i, %i },\n", idx1, idx2, idx3); - object->mTopology.Indexes.append(Vec3{ idx1, idx2, idx3 }); - } - - if (object->mTopology.Normals.size() != object->mTopology.Points.size()) { - printf("Logic error loading normals\n"); - } - - object->mCache.Source = &object->mTopology; - object->mCache.updateCache(); - } - - return scene.mObjects.size(); -} - // Function to read a Lua table representing RenderSettings int readRenderSettings(lua_State* L, tp::RayTracer::RenderSettings& settings) { lua_getglobal(L, "RenderSettings"); @@ -152,7 +110,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re directoryPath /= meshesPath; - if (!loadMeshes(scene, directoryPath.string())) { + if (!scene.load(directoryPath.string())) { printf("No 'meshes' loaded - check ur .obj path and validate content of .obj .\n"); return; } diff --git a/RayTracer/public/RayTracer.hpp b/RayTracer/public/RayTracer.hpp index d448b06..c667ab1 100644 --- a/RayTracer/public/RayTracer.hpp +++ b/RayTracer/public/RayTracer.hpp @@ -1,42 +1,13 @@ #pragma once -#include "Buffer2D.hpp" -#include "Camera.hpp" +#include "Scene.hpp" #include "Color.hpp" #include "Module.hpp" -#include "Topology.hpp" #include "Vec.hpp" namespace tp { - extern ModuleManifest gModuleRayTracer; - - class Object { - public: - Object() = default; - - public: - Topology mTopology; - TopologyCache mCache; - }; - - struct PointLight { - Vec3F pos; - halnf fallOut = 1.f; - halnf intensity = 1.f; - }; - - class Scene { - public: - Scene() = default; - - public: - Buffer mObjects; - Buffer mLights; - Camera mCamera; - }; - class RayTracer { public: typedef Buffer2D RenderBuffer;