diff --git a/3DEditor/CMakeLists.txt b/3DEditor/CMakeLists.txt index e11e387..3f328b7 100644 --- a/3DEditor/CMakeLists.txt +++ b/3DEditor/CMakeLists.txt @@ -11,11 +11,12 @@ file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp") add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) target_include_directories(${PROJECT_NAME} PUBLIC ./public/ ${BINDINGS_INCLUDE} ./ext/) -target_link_libraries(${PROJECT_NAME} PUBLIC Graphics Connection Widgets Math RasterRender) +target_link_libraries(${PROJECT_NAME} PUBLIC Graphics Connection Widgets Math RasterRender 3DScene) target_link_libraries(${PROJECT_NAME} PUBLIC ${BINDINGS_LIBS}) ### -------------------------- Applications -------------------------- ### -add_executable(3DEditorApp ./applications/Entry.cpp ./applications/SceneLoad.cpp) +file(GLOB APP_SOURCES "./applications/*.cpp") +add_executable(3DEditorApp ${APP_SOURCES}) target_link_libraries(3DEditorApp ${PROJECT_NAME} Lua ImageIO) file(COPY "rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") diff --git a/3DEditor/applications/Entry.cpp b/3DEditor/applications/Entry.cpp index 38bce0a..ea0a130 100644 --- a/3DEditor/applications/Entry.cpp +++ b/3DEditor/applications/Entry.cpp @@ -5,18 +5,17 @@ using namespace tp; -bool loadMeshes(tp::Scene& scene, const std::string& objetsPath); class EditorGUI : public Application { public: EditorGUI() { Vec2F renderResolution = { 1000, 1000 }; auto canvas = this->mGraphics->getCanvas(); - mGui = new EditorWidget(canvas, &geometry, renderResolution); + mGui = new EditorWidget(canvas, &mScene, renderResolution); - loadMeshes(geometry, "rsc/scene.obj"); + mScene.load("rsc/scene.obj"); - geometry.mCamera.lookAtPoint({ 0, 0, 0 }, { 3, 3, 2 }, { 0, 0, 1 }); + mScene.mCamera.lookAtPoint({ 0, 0, 0 }, { 3, 3, 2 }, { 0, 0, 1 }); } ~EditorGUI() override { delete mGui; } @@ -33,7 +32,7 @@ public: void drawFrame(Canvas* canvas) override { mGui->drawWrapper(*canvas); } private: - Scene geometry; + Scene mScene; WidgetManager mWidgetManager; EditorWidget* mGui; }; diff --git a/3DEditor/applications/SceneLoad.cpp b/3DEditor/applications/SceneLoad.cpp deleted file mode 100644 index c9e9a13..0000000 --- a/3DEditor/applications/SceneLoad.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "Scene.hpp" - -#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) { - uint4 idx1 = (int) curMesh.Indices[j]; - uint4 idx2 = (int) curMesh.Indices[j + 1]; - uint4 idx3 = (int) curMesh.Indices[j + 2]; - // printf("{ %i, %i, %i },\n", idx1, idx2, idx3); - object->mTopology.Indexes.append({ idx1, idx2, idx3 }); - } - - if (object->mTopology.Normals.size() != object->mTopology.Points.size()) { - printf("Logic error loading normals\n"); - } - } - - return scene.mObjects.size(); -} diff --git a/3DEditor/private/GPUBuffers.hpp b/3DEditor/private/GPUBuffers.hpp new file mode 100644 index 0000000..f90ff4d --- /dev/null +++ b/3DEditor/private/GPUBuffers.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include "Scene.hpp" +#include "GraphicsApi.hpp" + +namespace tp { + + class ObjectBuffers : public GPUBuffers { + public: + ObjectBuffers(Object* object) { + mObject = (object); + + auto& buff = mObject->mTopology.Points; + auto& indices = mObject->mTopology.Indexes; + + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glGenBuffers(1, &EBO); + + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3F) * buff.size(), buff.getBuff(), GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Vec3) * indices.size(), indices.getBuff(), GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr); + glEnableVertexAttribArray(0); + + // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } + + Object* mObject = nullptr; + + GLuint VAO = 0; + GLuint VBO = 0; + GLuint EBO = 0; + + void drawCall() override { + auto& indices = mObject->mTopology.Indexes; + + glBindVertexArray(VAO); + + // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, nullptr); + + // glDrawArrays(GL_TRIANGLES, 0, sizeof(buffer) / (2 * sizeof(float))); + + // glBindVertexArray(0); + } + + ~ObjectBuffers() { + glDeleteBuffers(1, &VBO); + glDeleteVertexArrays(1, &VAO); + } + }; + +} \ No newline at end of file diff --git a/3DEditor/private/Render.cpp b/3DEditor/private/Render.cpp index d0e9537..c5f288e 100644 --- a/3DEditor/private/Render.cpp +++ b/3DEditor/private/Render.cpp @@ -1,62 +1,8 @@ #include "Render.hpp" - -#include "GraphicsApi.hpp" +#include "GPUBuffers.hpp" using namespace tp; -class ObjectBuffers { -public: - ObjectBuffers(Object* object) { - mObject = (object); - - auto& buff = mObject->mTopology.Points; - auto& indices = mObject->mTopology.Indexes; - - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3F) * buff.size(), buff.getBuff(), GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Vec3) * indices.size(), indices.getBuff(), GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr); - glEnableVertexAttribArray(0); - - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - - Object* mObject = nullptr; - - GLuint VAO = 0; - GLuint VBO = 0; - GLuint EBO = 0; - - void drawCall() { - auto& indices = mObject->mTopology.Indexes; - - glBindVertexArray(VAO); - - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, nullptr); - - // glDrawArrays(GL_TRIANGLES, 0, sizeof(buffer) / (2 * sizeof(float))); - - // glBindVertexArray(0); - } - - ~ObjectBuffers() { - glDeleteBuffers(1, &VBO); - glDeleteVertexArrays(1, &VAO); - } -}; - Render::Render(Vec2F renderResolution) : mRenderBuffer(renderResolution) { @@ -72,8 +18,8 @@ Vec2F Render::getBufferSize() { return mRenderBuffer.getSize(); } void Render::render(const Scene& geometry, Vec2F size) { for (auto object : geometry.mObjects) { - if (!object->mBuffers) { - object->mBuffers = std::make_shared(&object.data()); + if (!object->mGUPBuffers) { + object->mGUPBuffers = new ObjectBuffers(&object.data()); } } @@ -101,7 +47,7 @@ void Render::render(const Scene& geometry, Vec2F size) { glUniformMatrix4fv(basis, 1, false, &basisMat[0][0]); glUniformMatrix4fv(camera, 1, true, &cameraMat[0][0]); - object->mBuffers->drawCall(); + object->mGUPBuffers->drawCall(); } mDefaultShader.unbind(); diff --git a/3DEditor/public/Scene.hpp b/3DEditor/public/Scene.hpp deleted file mode 100644 index 9b995ba..0000000 --- a/3DEditor/public/Scene.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "Topology.hpp" -#include - -class ObjectBuffers; - -namespace tp { - - class Object { - public: - Object() = default; - - public: - Topology mTopology; - std::shared_ptr mBuffers; - }; - - struct PointLight { - Vec3F pos; - halnf fallOut = 1.f; - halnf intensity = 1.f; - }; - - class Scene { - public: - Scene() = default; - - public: - Buffer mObjects; - Buffer mLights; - Camera mCamera; - }; -} \ No newline at end of file diff --git a/3DScene/public/Scene.hpp b/3DScene/public/Scene.hpp index 5efda2a..4d222d4 100644 --- a/3DScene/public/Scene.hpp +++ b/3DScene/public/Scene.hpp @@ -11,12 +11,18 @@ namespace tp { public: GPUBuffers() = default; virtual ~GPUBuffers() = default; + + virtual void drawCall() = 0; }; class Object { public: Object() = default; + ~Object() { + delete mGUPBuffers; + }; + public: Topology mTopology; TopologyCache mCache;