From 3dbe695305d279e89599f8d56e3ceb3598c373cb Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Wed, 15 Nov 2017 15:57:04 +0000 Subject: [PATCH] Added managers for Shader programs and Meshes. Improved Actor. Actor now can be drawn without a mesh. --- 3Engine/3Engine.vcxproj | 4 + 3Engine/3Engine.vcxproj.filters | 12 +++ 3EngineExecutable/3EngineExecutable.vcxproj | 2 + 3EngineTests/3EngineTests.vcxproj | 2 + 3EngineTests/3EngineTests.vcxproj.filters | 39 -------- CMakeLists.txt | 3 +- src/Engine/Actor.cpp | 21 ++-- src/Engine/Actor.h | 2 +- src/Engine/Shapes/Cube.cpp | 5 +- src/Engine/Shapes/MeshLoader.cpp | 12 +-- src/Engine/Shapes/MeshLoader.h | 5 +- src/Engine/Utilities/Manager.h | 39 ++++++++ src/Engine/Utilities/Managers.h | 19 ++++ src/Engine/Utilities/singleton.h | 25 ++++- .../LoaderAndScene/LoaderAndScene.cpp | 97 +++++++++++-------- src/Executable/main.cpp | 2 + 16 files changed, 183 insertions(+), 106 deletions(-) create mode 100644 src/Engine/Utilities/Manager.h create mode 100644 src/Engine/Utilities/Managers.h diff --git a/3Engine/3Engine.vcxproj b/3Engine/3Engine.vcxproj index d0740e8..44a5493 100644 --- a/3Engine/3Engine.vcxproj +++ b/3Engine/3Engine.vcxproj @@ -95,6 +95,8 @@ + + 15.0 @@ -195,6 +197,7 @@ Disabled true OS_WIN;_CRT_NO_VA_START_VALIDATION;DEBUG;%(PreprocessorDefinitions) + /std:c++latest %(AdditionalOptions) $(SolutionDir)\dependencies\freeglut-msvc\lib\x64\;$(SolutionDir)\dependencies\glew-msvc\lib\Release\x64\;%(AdditionalLibraryDirectories) @@ -247,6 +250,7 @@ xcopy "$(SolutionDir)assets" "$(OutDir)assets\" /E true true OS_WIN;_CRT_NO_VA_START_VALIDATION;RELEASE;%(PreprocessorDefinitions) + /std:c++latest %(AdditionalOptions) true diff --git a/3Engine/3Engine.vcxproj.filters b/3Engine/3Engine.vcxproj.filters index 8c25498..2570c46 100644 --- a/3Engine/3Engine.vcxproj.filters +++ b/3Engine/3Engine.vcxproj.filters @@ -123,6 +123,9 @@ Engine + + Engine + @@ -230,5 +233,14 @@ Engine + + Engine + + + Engine + + + Engine + \ No newline at end of file diff --git a/3EngineExecutable/3EngineExecutable.vcxproj b/3EngineExecutable/3EngineExecutable.vcxproj index b654627..f1e8f20 100644 --- a/3EngineExecutable/3EngineExecutable.vcxproj +++ b/3EngineExecutable/3EngineExecutable.vcxproj @@ -135,6 +135,7 @@ Disabled true OS_WIN;_CRT_NO_VA_START_VALIDATION;DEBUG;%(PreprocessorDefinitions) + /std:c++latest %(AdditionalOptions) $(SolutionDir)\dependencies\freeglut-msvc\lib\x64\;$(SolutionDir)\dependencies\glew-msvc\lib\Release\x64\;%(AdditionalLibraryDirectories) @@ -184,6 +185,7 @@ xcopy "$(SolutionDir)shaders" "$(OutDir)shaders\" /E true true OS_WIN;_CRT_NO_VA_START_VALIDATION;RELEASE;%(PreprocessorDefinitions) + /std:c++latest %(AdditionalOptions) true diff --git a/3EngineTests/3EngineTests.vcxproj b/3EngineTests/3EngineTests.vcxproj index 55ff000..0684ab7 100644 --- a/3EngineTests/3EngineTests.vcxproj +++ b/3EngineTests/3EngineTests.vcxproj @@ -128,6 +128,7 @@ Disabled true OS_WIN;_SCL_SECURE_NO_WARNINGS;_CRT_NO_VA_START_VALIDATION;%(PreprocessorDefinitions) + /std:c++latest %(AdditionalOptions) $(SolutionDir)\dependencies\freeglut-msvc\lib\x64\;$(SolutionDir)\dependencies\glew-msvc\lib\Release\x64\;%(AdditionalLibraryDirectories) @@ -175,6 +176,7 @@ copy "$(SolutionDir)config\RuntimeConfig.json" "$(OutDir)config\" true true OS_WIN;_SCL_SECURE_NO_WARNINGS;_CRT_NO_VA_START_VALIDATION;%(PreprocessorDefinitions) + /std:c++latest %(AdditionalOptions) true diff --git a/3EngineTests/3EngineTests.vcxproj.filters b/3EngineTests/3EngineTests.vcxproj.filters index 3ad9ad5..a0a60d9 100644 --- a/3EngineTests/3EngineTests.vcxproj.filters +++ b/3EngineTests/3EngineTests.vcxproj.filters @@ -27,33 +27,15 @@ Source Files - - Source Files - - - Source Files - Source Files - - Source Files - - - Source Files - - - Source Files - Source Files Source Files - - Source Files - Source Files @@ -182,39 +164,18 @@ Header Files - - Header Files - - - Header Files - Header Files Header Files - - Header Files - - - Header Files - - - Header Files - - - Header Files - Header Files Header Files - - Header Files - Header Files diff --git a/CMakeLists.txt b/CMakeLists.txt index 21c78fb..6dca090 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,8 @@ set(ENGINE_SOURCE_FILES src/Engine/Shapes/Mesh.cpp src/Engine/Shapes/Mesh.h src/Engine/Shapes/MeshLoader.cpp src/Engine/Shapes/MeshLoader.h src/Engine/Actor.cpp src/Engine/Actor.h - src/Engine/SceneManagement/SceneGraph.cpp src/Engine/SceneManagement/SceneGraph.h) + src/Engine/SceneManagement/SceneGraph.cpp src/Engine/SceneManagement/SceneGraph.h + src/Engine/Utilities/Manager.h src/Engine/Utilities/Managers.h) set(EXECUTABLE_SOURCE_FILES src/Executable/main.cpp diff --git a/src/Engine/Actor.cpp b/src/Engine/Actor.cpp index 67650e9..4d9f3b7 100644 --- a/src/Engine/Actor.cpp +++ b/src/Engine/Actor.cpp @@ -10,7 +10,7 @@ namespace ThreeEngine { Actor::Actor() - : IDrawable(), isInitiated(false), transform(), mesh(), + : IDrawable(), isInitiated(false), transform(), mesh(nullptr), shaderProgram(nullptr), parent(nullptr) { } Actor::~Actor() { @@ -29,15 +29,17 @@ namespace ThreeEngine { "shaders/Default/program.json"); } GetShaderProgram()->Init(); - mesh.Init(); + if (mesh) { + mesh->Init(); + } for (auto&& child : children) { child->Init(); } } void Actor::Draw() { - { // Actor Mesh - mesh.Bind(); + if (mesh) { // Actor Mesh + mesh->Bind(); GetShaderProgram()->Bind(); number matrixArray[16]; @@ -49,20 +51,19 @@ namespace ThreeEngine { if (preDraw) { preDraw(); } - mesh.Draw(); + mesh->Draw(); if (postDraw) { postDraw(); } GetShaderProgram()->Unbind(); - mesh.Unbind(); + mesh->Unbind(); CheckOpenGLError("Could not draw Actor."); } - { // Children - for (auto&& child : children) { - child->Draw(); - } + // Children + for (auto&& child : children) { + child->Draw(); } } diff --git a/src/Engine/Actor.h b/src/Engine/Actor.h index 36c41e9..40055df 100644 --- a/src/Engine/Actor.h +++ b/src/Engine/Actor.h @@ -32,7 +32,7 @@ namespace ThreeEngine { std::function preDraw, postDraw; - Mesh mesh; + std::shared_ptr mesh; std::shared_ptr shaderProgram; diff --git a/src/Engine/Shapes/Cube.cpp b/src/Engine/Shapes/Cube.cpp index 916579d..69a4b74 100644 --- a/src/Engine/Shapes/Cube.cpp +++ b/src/Engine/Shapes/Cube.cpp @@ -11,7 +11,8 @@ namespace ThreeEngine { Cube::Cube() : Actor(), color{.5f, .5f, .5f, 1.0f} { - mesh.Vertices = std::vector({ + mesh = std::make_shared(); + mesh->Vertices = std::vector({ {0.0f, 0.0f, 1.0f}, // 0 - FRONT {1.0f, 0.0f, 1.0f}, // 1 {1.0f, 1.0f, 1.0f}, // 2 @@ -54,7 +55,7 @@ namespace ThreeEngine { {1.0f, 0.0f, 1.0f}, // 1 {0.0f, 0.0f, 1.0f} // 0 }); - for (Vector & v : mesh.Vertices) { + for (Vector & v : mesh->Vertices) { v -= 0.5f; } preDraw = std::bind(&Cube::ColorUpdate, this); diff --git a/src/Engine/Shapes/MeshLoader.cpp b/src/Engine/Shapes/MeshLoader.cpp index 99d198f..7093b23 100644 --- a/src/Engine/Shapes/MeshLoader.cpp +++ b/src/Engine/Shapes/MeshLoader.cpp @@ -76,8 +76,8 @@ namespace ThreeEngine { return meshData; } - Mesh MeshLoader::OBJ_ProcessMeshData(MeshLoader::MeshData& meshData) { - Mesh mesh = Mesh(); + std::shared_ptr MeshLoader::OBJ_ProcessMeshData(MeshLoader::MeshData& meshData) { + auto mesh = std::make_shared(); for (unsigned int i = 0; i < meshData.vertexIdx.size(); i++) { unsigned int vi = meshData.vertexIdx[i]; if ((vi - 1) >= meshData.vertices.size()) { @@ -85,22 +85,22 @@ namespace ThreeEngine { continue; } Vector v = meshData.vertices[vi - 1]; - mesh.Vertices.push_back(v); + mesh->Vertices.push_back(v); if (!meshData.texCoords.empty()) { unsigned int ti = meshData.texCoordIdx[i]; Vector2 t = meshData.texCoords[ti - 1]; - mesh.TexCoords.push_back(t); + mesh->TexCoords.push_back(t); } if (!meshData.normals.empty()) { unsigned int ni = meshData.normalIdx[i]; Vector n = meshData.normals[ni - 1]; - mesh.Normals.push_back(n); + mesh->Normals.push_back(n); } } return mesh; } - Mesh MeshLoader::LoadFileOBJ(std::string const& filepath) { + std::shared_ptr MeshLoader::LoadFileOBJ(std::string const& filepath) { MeshData meshData = OBJ_LoadMeshData(filepath); return OBJ_ProcessMeshData(meshData); } diff --git a/src/Engine/Shapes/MeshLoader.h b/src/Engine/Shapes/MeshLoader.h index ea012ec..124cca1 100644 --- a/src/Engine/Shapes/MeshLoader.h +++ b/src/Engine/Shapes/MeshLoader.h @@ -6,6 +6,7 @@ #ifndef THREEENGINE_MESHLOADER_H #define THREEENGINE_MESHLOADER_H +#include #include "../Utilities/singleton.h" #include "Mesh.h" @@ -32,10 +33,10 @@ namespace ThreeEngine { MeshData OBJ_LoadMeshData(std::string const& filename); - Mesh OBJ_ProcessMeshData(MeshData& meshData); + std::shared_ptr OBJ_ProcessMeshData(MeshData& meshData); public: - Mesh LoadFileOBJ(std::string const& filepath); + std::shared_ptr LoadFileOBJ(std::string const& filepath); }; } // namespace ThreeEngine diff --git a/src/Engine/Utilities/Manager.h b/src/Engine/Utilities/Manager.h new file mode 100644 index 0000000..85a8836 --- /dev/null +++ b/src/Engine/Utilities/Manager.h @@ -0,0 +1,39 @@ +/* +* File Manager.h in project ThreeEngine +* +* Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved +*/ +#ifndef THREEENGINE_MANAGER_H +#define THREEENGINE_MANAGER_H + +#include +#include +#include "singleton.h" + +namespace ThreeEngine { + template + class Manager { + SINGLETON_HEADER_ONLY(Manager) + + private: + std::map objects{}; + + public: + inline void Add(std::string name, T const& object) { + objects[name] = object; + } + + inline T Get(std::string name) { + return objects[name]; + } + + inline void Clear() { + objects.clear(); + } + }; + + template + SINGLETON_HEADER_ONLY_AFTERCLASS(Manager) +} + +#endif //THREEENGINE_MANAGER_H diff --git a/src/Engine/Utilities/Managers.h b/src/Engine/Utilities/Managers.h new file mode 100644 index 0000000..ae7d410 --- /dev/null +++ b/src/Engine/Utilities/Managers.h @@ -0,0 +1,19 @@ +/* +* File Managers.h in project ThreeEngine +* +* Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved +*/ +#ifndef THREEENGINE_MANAGERS_H +#define THREEENGINE_MANAGERS_H + +#include +#include "Manager.h" +#include "../Shader/ShaderProgram.h" +#include "../Shapes/Mesh.h" + +namespace ThreeEngine { + typedef Manager> ShaderProgramManager; + typedef Manager> MeshManager; +} + +#endif //THREEENGINE_MANAGERS_H diff --git a/src/Engine/Utilities/singleton.h b/src/Engine/Utilities/singleton.h index da5d948..8e9989b 100644 --- a/src/Engine/Utilities/singleton.h +++ b/src/Engine/Utilities/singleton.h @@ -6,11 +6,12 @@ // /////////////////////////////////////////////////////////////////////// -#pragma once +#ifndef THREEENGINE_SINGLETON_H +#define THREEENGINE_SINGLETON_H #define SINGLETON_HEADER(CLASSNAME)\ - private: static CLASSNAME* _instance; CLASSNAME();\ - public: static CLASSNAME* instance(); virtual ~CLASSNAME(); void cleanup(); + private: static CLASSNAME* _instance; CLASSNAME();\ + public: static CLASSNAME* instance(); virtual ~CLASSNAME(); void cleanup(); #define SINGLETON_IMPLEMENTATION(CLASSNAME)\ CLASSNAME* CLASSNAME::_instance = nullptr;\ @@ -18,5 +19,19 @@ if (_instance == nullptr) { _instance = new CLASSNAME(); }\ return _instance; }\ CLASSNAME::CLASSNAME() = default;\ - CLASSNAME::~CLASSNAME() = default;\ - void CLASSNAME::cleanup() { if (_instance != nullptr) { delete _instance; _instance = nullptr; } } + CLASSNAME::~CLASSNAME() = default;\ + void CLASSNAME::cleanup() { if (_instance != nullptr) { delete _instance; _instance = nullptr; } } + +#define SINGLETON_HEADER_ONLY(CLASSNAME)\ + private: static CLASSNAME* _instance; CLASSNAME() = default;\ + public: static CLASSNAME* instance() {\ + if (_instance == nullptr) { _instance = new CLASSNAME(); }\ + return _instance; }\ + virtual ~CLASSNAME() = default;\ + void cleanup() { if (_instance != nullptr) { delete _instance; _instance = nullptr; } } + +#define SINGLETON_HEADER_ONLY_AFTERCLASS(CLASSNAME)\ + CLASSNAME* CLASSNAME::_instance = nullptr; + + +#endif THREEENGINE_SINGLETON_H \ No newline at end of file diff --git a/src/Executable/LoaderAndScene/LoaderAndScene.cpp b/src/Executable/LoaderAndScene/LoaderAndScene.cpp index bac2a79..069ad6c 100644 --- a/src/Executable/LoaderAndScene/LoaderAndScene.cpp +++ b/src/Executable/LoaderAndScene/LoaderAndScene.cpp @@ -8,6 +8,7 @@ #include "../../Engine/Shapes/Cube.h" #include "../../Engine/Shapes/MeshLoader.h" #include "../../Engine/Camera/Perspective.h" +#include "../../Engine/Utilities/Managers.h" #define VERTICES 0 #define COLORS 1 @@ -27,12 +28,19 @@ namespace ThreeEngine { } void LoaderAndScene::scene() { - auto defaultProgram = std::make_shared( - "shaders/Default/program.json"); - defaultProgram->Init(); - auto colorProgram = std::make_shared( - "shaders/Color3D/program.json"); - colorProgram->Init(); + { // Shaders + auto defaultProgram = std::make_shared( + "shaders/Default/program.json"); + defaultProgram->Init(); + ShaderProgramManager::instance()->Add("default", defaultProgram); + auto colorProgram = std::make_shared( + "shaders/Color3D/program.json"); + colorProgram->Init(); + ShaderProgramManager::instance()->Add("color3d", colorProgram); + } + + MeshManager::instance()->Add("Cube", + MeshLoader::instance()->LoadFileOBJ("assets/Cube-vtn.obj")); actors.push_back(reinterpret_cast(sceneGraph)); @@ -41,8 +49,9 @@ namespace ThreeEngine { number height = config["window"]["y"]; number aspect = width / height; Camera* camera = new Camera( - static_cast(colorProgram->GetUniformBlockBidingId( - "SharedMatrices")), + static_cast( + ShaderProgramManager::instance()->Get("default") + ->GetUniformBlockBidingId("SharedMatrices")), new Perspective(30, aspect, 1, 100), new Matrix(Matrix::TranslationMatrix(Vector(0, 0, -15))) // new LookAt({5, 0.5f, 0}, {0, 0.5f, 0}, {0, 1, 0}) @@ -51,42 +60,50 @@ namespace ThreeEngine { controller.camera = camera; } -// { -// auto* cube = new Actor(); -// cube->mesh = MeshLoader::instance()->LoadFileOBJ( -// "assets/Cube-vtn.obj"); -// -// sceneGraph->SetRoot(cube); -// -// auto* cube2 = new Actor(); -// cube2->mesh = cube->mesh; -// cube2->transform.translation += Vector(0, 2, 0); -// cube2->SetParent(cube); -// } + //{ + // auto* cube = new Actor(); + // cube->mesh = MeshManager::instance()->Get("Cube"); + + // sceneGraph->SetRoot(cube); + + // auto* cube2 = new Actor(); + // cube2->mesh = cube->mesh; + // cube2->transform.translation += Vector(0, 2, 0); + // cube2->SetParent(cube); + //} { // Square - auto* square = new Cube(); - square->color[0] = 1; - square->color[1] = 0; - square->color[2] = 1; - square->color[3] = 1; - square->shaderProgram = colorProgram; - square->transform.scale = Vector(1.5f); - square->transform.rotation = - Quat::FromAngleAxis(-45, Vector(0, 0, 1)); - sceneGraph->SetRoot(square); - - auto* cube2 = new Actor(); - cube2->mesh = MeshLoader::instance()->LoadFileOBJ( - "assets/Cube-vtn.obj"); - cube2->transform.translation += Vector(0, 2, 0); - cube2->shaderProgram = defaultProgram; - cube2->SetParent(square); + auto root = new Actor(); + root->shaderProgram = + ShaderProgramManager::instance()->Get("default"); + sceneGraph->SetRoot(root); + + { + auto square = new Cube(); + square->color[0] = 1; + square->color[1] = 0; + square->color[2] = 1; + square->color[3] = 1; + square->shaderProgram = ShaderProgramManager::instance()->Get("color3d"); + square->transform.scale = Vector(.5f); + square->transform.rotation = + Quat::FromAngleAxis(45, Vector(0, 0, -1)); + square->SetParent(root); + + auto* cube2 = new Actor(); + cube2->mesh = MeshManager::instance()->Get("Cube"); + cube2->transform.translation += Vector(0, 2, 0); + cube2->SetParent(square); + } + + auto* cube3 = new Actor(); + cube3->mesh = MeshManager::instance()->Get("Cube"); + cube3->transform.translation += Vector(0, 2, 0); + cube3->SetParent(root); } - for (auto& actor : actors) { - actor->Init(); - } + // Initializes all the actors in it. + sceneGraph->Init(); } void LoaderAndScene::OnReshape(int, int) { diff --git a/src/Executable/main.cpp b/src/Executable/main.cpp index 6cc7021..d4695b2 100644 --- a/src/Executable/main.cpp +++ b/src/Executable/main.cpp @@ -5,6 +5,8 @@ #include #include "LoaderAndScene/LoaderAndScene.h" +#include "../Engine/Utilities/Manager.h" + using namespace ThreeEngine; // NVIDIA Dedicated Graphics