Skip to content

Commit

Permalink
Added managers for Shader programs and Meshes. Improved Actor.
Browse files Browse the repository at this point in the history
Actor now can be drawn without a mesh.
  • Loading branch information
RicardoEPRodrigues committed Nov 15, 2017
1 parent 83c5ea5 commit 3dbe695
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 106 deletions.
4 changes: 4 additions & 0 deletions 3Engine/3Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
<ClInclude Include="..\src\Engine\Utilities\json.hpp" />
<ClInclude Include="..\src\Engine\Utilities\singleton.h" />
<ClInclude Include="..\src\Engine\SceneManagement\SceneGraph.h" />
<ClInclude Include="..\src\Engine\Utilities\Manager.h" />
<ClInclude Include="..\src\Engine\Utilities\Managers.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
Expand Down Expand Up @@ -195,6 +197,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>OS_WIN;_CRT_NO_VA_START_VALIDATION;DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/std:c++latest %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(SolutionDir)\dependencies\freeglut-msvc\lib\x64\;$(SolutionDir)\dependencies\glew-msvc\lib\Release\x64\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down Expand Up @@ -247,6 +250,7 @@ xcopy "$(SolutionDir)assets" "$(OutDir)assets\" /E</Command>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>OS_WIN;_CRT_NO_VA_START_VALIDATION;RELEASE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/std:c++latest %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
12 changes: 12 additions & 0 deletions 3Engine/3Engine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<ClCompile Include="..\src\Engine\Time\Time.cpp">
<Filter>Engine</Filter>
</ClCompile>
<ClCompile Include="..\src\Engine\SceneManagement\SceneGraph.cpp">
<Filter>Engine</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\Engine\Actor.h">
Expand Down Expand Up @@ -230,5 +233,14 @@
<ClInclude Include="..\src\Engine\Utilities\singleton.h">
<Filter>Engine</Filter>
</ClInclude>
<ClInclude Include="..\src\Engine\Utilities\Managers.h">
<Filter>Engine</Filter>
</ClInclude>
<ClInclude Include="..\src\Engine\Utilities\Manager.h">
<Filter>Engine</Filter>
</ClInclude>
<ClInclude Include="..\src\Engine\SceneManagement\SceneGraph.h">
<Filter>Engine</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions 3EngineExecutable/3EngineExecutable.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>OS_WIN;_CRT_NO_VA_START_VALIDATION;DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/std:c++latest %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(SolutionDir)\dependencies\freeglut-msvc\lib\x64\;$(SolutionDir)\dependencies\glew-msvc\lib\Release\x64\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down Expand Up @@ -184,6 +185,7 @@ xcopy "$(SolutionDir)shaders" "$(OutDir)shaders\" /E</Command>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>OS_WIN;_CRT_NO_VA_START_VALIDATION;RELEASE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/std:c++latest %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
2 changes: 2 additions & 0 deletions 3EngineTests/3EngineTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>OS_WIN;_SCL_SECURE_NO_WARNINGS;_CRT_NO_VA_START_VALIDATION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/std:c++latest %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(SolutionDir)\dependencies\freeglut-msvc\lib\x64\;$(SolutionDir)\dependencies\glew-msvc\lib\Release\x64\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down Expand Up @@ -175,6 +176,7 @@ copy "$(SolutionDir)config\RuntimeConfig.json" "$(OutDir)config\"</Command>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>OS_WIN;_SCL_SECURE_NO_WARNINGS;_CRT_NO_VA_START_VALIDATION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/std:c++latest %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
39 changes: 0 additions & 39 deletions 3EngineTests/3EngineTests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,15 @@
<ClCompile Include="..\src\Tests\tests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Vector.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Vector2.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Tests\tests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Matrix2.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Matrix3.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Vector.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Vector2.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Math\Vector4.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\Tests\tests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -182,39 +164,18 @@
<ClInclude Include="..\src\Tests\VectorTests.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Vector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Vector2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Tests\MatrixTests.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Tests\VectorTests.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Matrix.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Matrix2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Matrix3.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\TMatrix.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Vector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Vector2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Math\Vector4.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Tests\MatrixTests.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions src/Engine/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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];
Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ThreeEngine {

std::function<void()> preDraw, postDraw;

Mesh mesh;
std::shared_ptr<Mesh> mesh;

std::shared_ptr<ShaderProgram> shaderProgram;

Expand Down
5 changes: 3 additions & 2 deletions src/Engine/Shapes/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace ThreeEngine {

Cube::Cube() : Actor(), color{.5f, .5f, .5f, 1.0f} {
mesh.Vertices = std::vector<Vector>({
mesh = std::make_shared<Mesh>();
mesh->Vertices = std::vector<Vector>({
{0.0f, 0.0f, 1.0f}, // 0 - FRONT
{1.0f, 0.0f, 1.0f}, // 1
{1.0f, 1.0f, 1.0f}, // 2
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/Engine/Shapes/MeshLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,31 @@ namespace ThreeEngine {
return meshData;
}

Mesh MeshLoader::OBJ_ProcessMeshData(MeshLoader::MeshData& meshData) {
Mesh mesh = Mesh();
std::shared_ptr<Mesh> MeshLoader::OBJ_ProcessMeshData(MeshLoader::MeshData& meshData) {
auto mesh = std::make_shared<Mesh>();
for (unsigned int i = 0; i < meshData.vertexIdx.size(); i++) {
unsigned int vi = meshData.vertexIdx[i];
if ((vi - 1) >= meshData.vertices.size()) {
DebugBreakpoint();
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<Mesh> MeshLoader::LoadFileOBJ(std::string const& filepath) {
MeshData meshData = OBJ_LoadMeshData(filepath);
return OBJ_ProcessMeshData(meshData);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Engine/Shapes/MeshLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef THREEENGINE_MESHLOADER_H
#define THREEENGINE_MESHLOADER_H

#include <memory>
#include "../Utilities/singleton.h"
#include "Mesh.h"

Expand All @@ -32,10 +33,10 @@ namespace ThreeEngine {

MeshData OBJ_LoadMeshData(std::string const& filename);

Mesh OBJ_ProcessMeshData(MeshData& meshData);
std::shared_ptr<Mesh> OBJ_ProcessMeshData(MeshData& meshData);

public:
Mesh LoadFileOBJ(std::string const& filepath);
std::shared_ptr<Mesh> LoadFileOBJ(std::string const& filepath);
};
} // namespace ThreeEngine

Expand Down
39 changes: 39 additions & 0 deletions src/Engine/Utilities/Manager.h
Original file line number Diff line number Diff line change
@@ -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 <map>
#include <string>
#include "singleton.h"

namespace ThreeEngine {
template<typename T>
class Manager {
SINGLETON_HEADER_ONLY(Manager)

private:
std::map<std::string, T> 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<typename T>
SINGLETON_HEADER_ONLY_AFTERCLASS(Manager<T>)
}

#endif //THREEENGINE_MANAGER_H
19 changes: 19 additions & 0 deletions src/Engine/Utilities/Managers.h
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include "Manager.h"
#include "../Shader/ShaderProgram.h"
#include "../Shapes/Mesh.h"

namespace ThreeEngine {
typedef Manager<std::shared_ptr<ShaderProgram>> ShaderProgramManager;
typedef Manager<std::shared_ptr<Mesh>> MeshManager;
}

#endif //THREEENGINE_MANAGERS_H
Loading

0 comments on commit 3dbe695

Please sign in to comment.