From b4a70d48e0ff13253dd0e0d0dba04a24f0ada3e4 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Thu, 16 Nov 2017 19:37:37 +0000 Subject: [PATCH] Added Tangram initial form and floor movement! --- CMakeLists.txt | 6 +- assets/Cube-vtn.obj | 36 ++-- assets/Parallelogram-vtn.obj | 50 +++++ assets/Pyramid-vtn.obj | 20 ++ shaders/Color3D/fragment.glsl | 5 +- shaders/Color3D/vertex.glsl | 9 - src/Engine/Actor.cpp | 9 +- src/Engine/Actor.h | 12 +- src/Engine/Camera/Perspective.h | 2 +- .../Controllers/SphereCameraController.cpp | 23 +-- .../Controllers/SphereCameraController.h | 2 +- src/Engine/Engine.cpp | 4 +- src/Engine/Engine.h | 5 + src/Engine/Shapes/Cube.cpp | 2 +- src/Engine/Utilities/Simulation.cpp | 20 ++ src/Engine/Utilities/Simulation.h | 34 +++ src/Engine/Utilities/singleton.h | 2 +- .../LoaderAndScene/ColoredActor.cpp | 19 ++ src/Executable/LoaderAndScene/ColoredActor.h | 25 +++ .../LoaderAndScene/LoaderAndScene.cpp | 193 ++++++++++++------ .../LoaderAndScene/LoaderAndScene.h | 8 + .../LoaderAndScene/MoveController.cpp | 59 ++++++ .../LoaderAndScene/MoveController.h | 30 +++ 23 files changed, 456 insertions(+), 119 deletions(-) create mode 100644 assets/Parallelogram-vtn.obj create mode 100644 assets/Pyramid-vtn.obj create mode 100644 src/Engine/Utilities/Simulation.cpp create mode 100644 src/Engine/Utilities/Simulation.h create mode 100644 src/Executable/LoaderAndScene/ColoredActor.cpp create mode 100644 src/Executable/LoaderAndScene/ColoredActor.h create mode 100644 src/Executable/LoaderAndScene/MoveController.cpp create mode 100644 src/Executable/LoaderAndScene/MoveController.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6dca090..82dc8ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,7 +101,7 @@ set(ENGINE_SOURCE_FILES 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/Utilities/Manager.h src/Engine/Utilities/Managers.h) + src/Engine/Utilities/Manager.h src/Engine/Utilities/Managers.h src/Engine/Utilities/Simulation.cpp src/Engine/Utilities/Simulation.h) set(EXECUTABLE_SOURCE_FILES src/Executable/main.cpp @@ -109,7 +109,7 @@ set(EXECUTABLE_SOURCE_FILES # src/Executable/Tangram3D/Tangram3D.cpp src/Executable/Tangram3D/Tangram3D.h # src/Executable/Quaternion/Quaternion.cpp src/Executable/Quaternion/Quaternion.h src/Executable/LoaderAndScene/LoaderAndScene.cpp src/Executable/LoaderAndScene/LoaderAndScene.h - ) + src/Executable/LoaderAndScene/ColoredActor.cpp src/Executable/LoaderAndScene/ColoredActor.h src/Executable/LoaderAndScene/MoveController.cpp src/Executable/LoaderAndScene/MoveController.h) set(TESTS_SOURCE_FILES src/Tests/catch.hpp src/Tests/tests.cpp src/Tests/VectorTests.h src/Tests/MatrixTests.h) @@ -142,7 +142,7 @@ if (WIN32) # WINDOWS post build ) endif () -add_custom_command(TARGET ThreeEngine POST_BUILD +add_custom_command(TARGET ThreeEngineExecutable POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo Copying Config files COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/config ${EXECUTABLE_OUTPUT_PATH}/config/ COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders ${EXECUTABLE_OUTPUT_PATH}/shaders/ diff --git a/assets/Cube-vtn.obj b/assets/Cube-vtn.obj index d46b58e..81ffa26 100644 --- a/assets/Cube-vtn.obj +++ b/assets/Cube-vtn.obj @@ -1,17 +1,17 @@ # Blender v2.79 (sub 0) OBJ File: 'Cube.blend' # www.blender.org o Cube -v 1.000000 -1.000000 -1.000000 -v 1.000000 -1.000000 1.000000 -v -1.000000 -1.000000 1.000000 -v -1.000000 -1.000000 -1.000000 -v 1.000000 1.000000 -0.999999 -v 0.999999 1.000000 1.000001 -v -1.000000 1.000000 1.000000 -v -1.000000 1.000000 -1.000000 -vt 1.000000 0.000000 -vt 0.000000 1.000000 +v 2.000000 0.000000 -2.000000 +v 2.000000 0.000000 0.000000 +v 0.000000 0.000000 -0.000000 +v 0.000000 0.000000 -2.000000 +v 2.000000 2.000000 -2.000000 +v 1.999999 2.000000 0.000000 +v -0.000000 2.000000 -0.000000 +v 0.000000 2.000000 -2.000000 vt 0.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 0.000000 0.000000 @@ -20,31 +20,31 @@ vt 0.000000 1.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 0.000000 0.000000 +vt 1.000000 0.000000 vt 0.000000 0.000000 -vt 1.000000 1.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 +vt 1.000000 0.000000 vt 1.000000 1.000000 vt 1.000000 1.000000 vt 1.000000 1.000000 -vt 1.000000 0.000000 vt 1.000000 1.000000 vn 0.0000 -1.0000 0.0000 vn 0.0000 1.0000 0.0000 vn 1.0000 -0.0000 0.0000 -vn 0.0000 -0.0000 1.0000 +vn -0.0000 -0.0000 1.0000 vn -1.0000 -0.0000 -0.0000 vn 0.0000 0.0000 -1.0000 s off -f 2/1/1 4/2/1 1/3/1 +f 1/1/1 3/2/1 4/3/1 f 8/4/2 6/5/2 5/6/2 -f 5/7/3 2/8/3 1/3/3 +f 5/7/3 2/8/3 1/1/3 f 6/9/4 3/10/4 2/11/4 -f 3/12/5 8/13/5 4/2/5 +f 7/12/5 4/3/5 3/13/5 f 1/14/6 8/15/6 5/6/6 -f 2/1/1 3/16/1 4/2/1 +f 1/1/1 2/16/1 3/2/1 f 8/4/2 7/17/2 6/5/2 f 5/7/3 6/18/3 2/8/3 f 6/9/4 7/17/4 3/10/4 -f 3/12/5 7/19/5 8/13/5 +f 7/12/5 8/19/5 4/3/5 f 1/14/6 4/20/6 8/15/6 diff --git a/assets/Parallelogram-vtn.obj b/assets/Parallelogram-vtn.obj new file mode 100644 index 0000000..d914dc1 --- /dev/null +++ b/assets/Parallelogram-vtn.obj @@ -0,0 +1,50 @@ +# Blender v2.79 (sub 0) OBJ File: 'Parallelogram.blend' +# www.blender.org +o Cube +v 1.989430 0.000000 0.005215 +v -0.010458 0.000000 -0.015868 +v 0.010625 0.000000 -2.015757 +v 2.010514 0.000000 -1.994673 +v 2.989375 1.000000 0.015758 +v 0.989486 1.000000 -0.005327 +v 1.010570 1.000000 -2.005216 +v 3.010458 1.000000 -1.984132 +vt 0.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 1.000000 1.000000 +vn 0.0000 -1.0000 0.0000 +vn 0.0000 1.0000 0.0000 +vn -0.0105 0.0000 0.9999 +vn -0.7071 0.7071 -0.0075 +vn 0.0105 -0.0000 -0.9999 +vn 0.7071 -0.7071 0.0075 +s off +f 1/1/1 3/2/1 4/3/1 +f 8/4/2 6/5/2 5/6/2 +f 1/1/3 6/7/3 2/8/3 +f 6/9/4 3/10/4 2/11/4 +f 7/12/5 4/3/5 3/13/5 +f 1/14/6 8/15/6 5/6/6 +f 1/1/1 2/16/1 3/2/1 +f 8/4/2 7/17/2 6/5/2 +f 1/1/3 5/18/3 6/7/3 +f 6/9/4 7/17/4 3/10/4 +f 7/12/5 8/19/5 4/3/5 +f 1/14/6 4/20/6 8/15/6 diff --git a/assets/Pyramid-vtn.obj b/assets/Pyramid-vtn.obj new file mode 100644 index 0000000..f376d10 --- /dev/null +++ b/assets/Pyramid-vtn.obj @@ -0,0 +1,20 @@ +# Blender v2.79 (sub 0) OBJ File: 'Pyramid.blend' +# www.blender.org +o Piramid_Cube +v 0.017666 0.000000 -2.005773 +v 1.994019 0.000000 0.017597 +v -0.005843 0.000000 -0.005912 +v -0.005842 2.000000 -0.005912 +vt 0.000000 1.000000 +vt 1.000000 1.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vn -0.0118 -0.0000 0.9999 +vn 0.0000 -1.0000 0.0000 +vn -0.9999 0.0000 -0.0118 +vn 0.5841 0.5774 -0.5705 +s off +f 3/1/1 2/2/1 4/3/1 +f 3/1/2 1/4/2 2/2/2 +f 3/1/3 4/3/3 1/4/3 +f 2/2/4 1/4/4 4/3/4 diff --git a/shaders/Color3D/fragment.glsl b/shaders/Color3D/fragment.glsl index c54a42f..ca2047a 100644 --- a/shaders/Color3D/fragment.glsl +++ b/shaders/Color3D/fragment.glsl @@ -1,9 +1,10 @@ #version 330 core -in vec4 ex_Color; out vec4 out_Color; +uniform vec4 ModelColor; + void main(void) { - out_Color = ex_Color; + out_Color = ModelColor; } \ No newline at end of file diff --git a/shaders/Color3D/vertex.glsl b/shaders/Color3D/vertex.glsl index 1130a20..4fd9b43 100644 --- a/shaders/Color3D/vertex.glsl +++ b/shaders/Color3D/vertex.glsl @@ -2,10 +2,8 @@ in vec4 in_Position; in vec4 in_Color; -out vec4 ex_Color; uniform mat4 ModelMatrix; -uniform vec4 ModelColor = vec4(0.5, 0.5, 0.5, 1.0); uniform SharedMatrices { mat4 ViewMatrix; @@ -15,11 +13,4 @@ uniform SharedMatrices { void main(void) { gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * in_Position; - - float zMixer = clamp((ModelMatrix * in_Position).z, 0.3, 1.0); - float xMixer = clamp((ModelMatrix * in_Position).x, 0.3, 0.5); - ex_Color = vec4(ModelColor.x * zMixer + xMixer, - ModelColor.y * zMixer + xMixer, - ModelColor.z * zMixer + xMixer, - ModelColor.w); } \ No newline at end of file diff --git a/src/Engine/Actor.cpp b/src/Engine/Actor.cpp index 4d9f3b7..1d4ecd0 100644 --- a/src/Engine/Actor.cpp +++ b/src/Engine/Actor.cpp @@ -10,8 +10,8 @@ namespace ThreeEngine { Actor::Actor() - : IDrawable(), isInitiated(false), transform(), mesh(nullptr), - shaderProgram(nullptr), parent(nullptr) { } + : IDrawable(), shaderProgram(nullptr), isInitiated(false), + transform(), mesh(nullptr), parent(nullptr) { } Actor::~Actor() { for (auto&& child : children) { @@ -84,6 +84,11 @@ namespace ThreeEngine { return parent->GetParentModelMatrix() * parent->GetModelMatrix(); } + void Actor::setShaderProgram( + const std::shared_ptr& shaderProgram) { + Actor::shaderProgram = shaderProgram; + } + std::shared_ptr Actor::GetShaderProgram() { if (shaderProgram) { return shaderProgram; diff --git a/src/Engine/Actor.h b/src/Engine/Actor.h index 40055df..d6ce402 100644 --- a/src/Engine/Actor.h +++ b/src/Engine/Actor.h @@ -14,13 +14,20 @@ namespace ThreeEngine { class Actor : public IDrawable { private: + + std::shared_ptr shaderProgram; + + protected: bool isInitiated; Matrix GetModelMatrix(); + Matrix GetLocalModelMatrix(); + Matrix GetParentModelMatrix(); std::shared_ptr GetShaderProgram(); + public: struct Transform { Vector translation{0}; @@ -34,8 +41,6 @@ namespace ThreeEngine { std::shared_ptr mesh; - std::shared_ptr shaderProgram; - Actor* parent; std::vector children; @@ -49,6 +54,9 @@ namespace ThreeEngine { void Draw() override; void SetParent(Actor* parent); + + void setShaderProgram( + const std::shared_ptr& shaderProgram); }; } // namespace ThreeEngine diff --git a/src/Engine/Camera/Perspective.h b/src/Engine/Camera/Perspective.h index ce5a93b..a5b88bd 100644 --- a/src/Engine/Camera/Perspective.h +++ b/src/Engine/Camera/Perspective.h @@ -10,7 +10,7 @@ namespace ThreeEngine { - class Perspective : public Matrix{ + class Perspective : public Matrix { public: number angle, d; Perspective(number FOVy, number aspect, number zNear, number zFar); diff --git a/src/Engine/Controllers/SphereCameraController.cpp b/src/Engine/Controllers/SphereCameraController.cpp index 3c89b88..f80ce29 100644 --- a/src/Engine/Controllers/SphereCameraController.cpp +++ b/src/Engine/Controllers/SphereCameraController.cpp @@ -12,7 +12,7 @@ namespace ThreeEngine { SphereCameraController::SphereCameraController() : engine(Engine::Instance()), inPerspective(true), - useQuat(true), previousMouseLocation(), + previousMouseLocation(), yawPitch(), translation(Matrix::IdentityMatrix()), rotation(Matrix::IdentityMatrix()), @@ -38,10 +38,6 @@ namespace ThreeEngine { inPerspective = true; } } - // Toggle between using Quaternions or Euler Angles for rotations - if (engine->input.Click('g')) { - useQuat = !useQuat; - } // Check if mouse was clicked if (engine->input[MouseKeys::LEFT] || engine->input[MouseKeys::RIGHT]) { @@ -57,17 +53,8 @@ namespace ThreeEngine { Quat::FromAngleAxis(-delta.Y, Vector(1, 0, 0))).Normalize() * quat; - if (useQuat) { - camera->SetView(Matrix::TranslationMatrix(Vector(0, 0, -15)) * - quat.ToMatrix()); - } else { - - Matrix rotation = Matrix(Matrix3::RotationMatrix(Maths::Axis::Y, - -delta.X)) * - Matrix(Matrix3::RotationMatrix(Maths::Axis::X, - -delta.Y)); - camera->SetView(*camera->GetView() * rotation); - } + camera->SetView(Matrix::TranslationMatrix(Vector(0, 0, -30)) * + quat.ToMatrix()); } else { UnlockMouse(); } @@ -84,10 +71,10 @@ namespace ThreeEngine { // check if mouse is at window borders bool mouseXUpdate = (mouse.X <= 0) || - (mouse.X >= (int) engine->config["window"]["x"]); + (mouse.X >= (int) engine->config["window"]["x"] - 1); bool mouseYUpdate = (mouse.Y <= 0) || - (mouse.Y >= (int) engine->config["window"]["y"]); + (mouse.Y >= (int) engine->config["window"]["y"] - 1); if (mouseXUpdate || mouseYUpdate) { Vector2 newMouseLoc = mouse; diff --git a/src/Engine/Controllers/SphereCameraController.h b/src/Engine/Controllers/SphereCameraController.h index e30f7c5..552734f 100644 --- a/src/Engine/Controllers/SphereCameraController.h +++ b/src/Engine/Controllers/SphereCameraController.h @@ -16,7 +16,7 @@ namespace ThreeEngine { private: Engine* engine; - bool inPerspective, useQuat; + bool inPerspective; Vector2 previousMouseLocation, yawPitch; diff --git a/src/Engine/Engine.cpp b/src/Engine/Engine.cpp index b2b2bc3..8d0054b 100644 --- a/src/Engine/Engine.cpp +++ b/src/Engine/Engine.cpp @@ -187,7 +187,6 @@ namespace ThreeEngine { void Engine::Display() { ++instance->FrameCount; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - Time::Update(); instance->PreDraw(); for (auto& actor : instance->actors) { actor->Draw(); @@ -197,7 +196,8 @@ namespace ThreeEngine { } void Engine::Idle() { - // TODO Insert Updates here + Time::Update(); + instance->Update(); instance->input.Update(); glutPostRedisplay(); } diff --git a/src/Engine/Engine.h b/src/Engine/Engine.h index abf5c37..6aa1510 100644 --- a/src/Engine/Engine.h +++ b/src/Engine/Engine.h @@ -79,6 +79,11 @@ namespace ThreeEngine { */ virtual void OnInit() { } + /** + * Called when before when no rendering is being done. + */ + virtual void Update() { } + /** * Called when cleaning OpenGL. Any OpenGL related cleaning needs to be done here. * This is called before deleting Actors diff --git a/src/Engine/Shapes/Cube.cpp b/src/Engine/Shapes/Cube.cpp index 69a4b74..a1e0162 100644 --- a/src/Engine/Shapes/Cube.cpp +++ b/src/Engine/Shapes/Cube.cpp @@ -64,7 +64,7 @@ namespace ThreeEngine { Cube::~Cube() = default; void Cube::ColorUpdate() { - glUniform4f(shaderProgram->GetUniformLocationId("ModelColor"), color[0], + glUniform4f(GetShaderProgram()->GetUniformLocationId("ModelColor"), color[0], color[1], color[2], color[3]); } diff --git a/src/Engine/Utilities/Simulation.cpp b/src/Engine/Utilities/Simulation.cpp new file mode 100644 index 0000000..202e362 --- /dev/null +++ b/src/Engine/Utilities/Simulation.cpp @@ -0,0 +1,20 @@ +/* + * File Simulation.cpp in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#include "Simulation.h" + +namespace ThreeEngine { + SINGLETON_IMPLEMENTATION(Simulation) + + void Simulation::Add(IUpdatable* updatable) { + updatables.push_back(std::unique_ptr(updatable)); + } + + void Simulation::Update() { + for (std::unique_ptr& updatable : updatables) { + updatable->Update(Time::Delta()); + } + } +} /* namespace ThreeEngine */ \ No newline at end of file diff --git a/src/Engine/Utilities/Simulation.h b/src/Engine/Utilities/Simulation.h new file mode 100644 index 0000000..931a7b2 --- /dev/null +++ b/src/Engine/Utilities/Simulation.h @@ -0,0 +1,34 @@ +/* + * File Simulation.h in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#ifndef THREEENGINE_SIMULATION_H +#define THREEENGINE_SIMULATION_H + +#include +#include +#include +#include "singleton.h" + +namespace ThreeEngine { + + class IUpdatable { + public: + virtual void Update(milliseconds delta) = 0; + }; + + class Simulation { + SINGLETON_HEADER(Simulation) + + private: + std::vector> updatables{}; + public: + void Add(IUpdatable* updatable); + + void Update(); + }; + +} /* namespace ThreeEngine */ + +#endif //THREEENGINE_SIMULATION_H diff --git a/src/Engine/Utilities/singleton.h b/src/Engine/Utilities/singleton.h index 8e9989b..d0a8495 100644 --- a/src/Engine/Utilities/singleton.h +++ b/src/Engine/Utilities/singleton.h @@ -34,4 +34,4 @@ CLASSNAME* CLASSNAME::_instance = nullptr; -#endif THREEENGINE_SINGLETON_H \ No newline at end of file +#endif // THREEENGINE_SINGLETON_H \ No newline at end of file diff --git a/src/Executable/LoaderAndScene/ColoredActor.cpp b/src/Executable/LoaderAndScene/ColoredActor.cpp new file mode 100644 index 0000000..5fbbd43 --- /dev/null +++ b/src/Executable/LoaderAndScene/ColoredActor.cpp @@ -0,0 +1,19 @@ +/* + * File ColoredActor.cpp in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#include "ColoredActor.h" + +namespace ThreeEngine { + + ColoredActor::ColoredActor() : Actor(), color(.5f) { + preDraw = std::bind(&ColoredActor::ColorUpdate, this); + } + + void ColoredActor::ColorUpdate() { + glUniform4f(GetShaderProgram()->GetUniformLocationId("ModelColor"), + color[0], color[1], color[2], color[3]); + } + +} /* namespace ThreeEngine */ \ No newline at end of file diff --git a/src/Executable/LoaderAndScene/ColoredActor.h b/src/Executable/LoaderAndScene/ColoredActor.h new file mode 100644 index 0000000..cdfb510 --- /dev/null +++ b/src/Executable/LoaderAndScene/ColoredActor.h @@ -0,0 +1,25 @@ +/* + * File ColoredActor.h in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#ifndef THREEENGINE_COLOREDACTOR_H +#define THREEENGINE_COLOREDACTOR_H + +#include "../../Engine/Actor.h" + +namespace ThreeEngine { + + class ColoredActor : public Actor { + private: + void ColorUpdate(); + + public: + Vector4 color; + + ColoredActor(); + }; + +} /* namespace ThreeEngine */ + +#endif //THREEENGINE_COLOREDACTOR_H diff --git a/src/Executable/LoaderAndScene/LoaderAndScene.cpp b/src/Executable/LoaderAndScene/LoaderAndScene.cpp index 069ad6c..05731c8 100644 --- a/src/Executable/LoaderAndScene/LoaderAndScene.cpp +++ b/src/Executable/LoaderAndScene/LoaderAndScene.cpp @@ -3,18 +3,23 @@ * * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved */ +#include #include "LoaderAndScene.h" -#include "../../Engine/Actor.h" -#include "../../Engine/Shapes/Cube.h" + #include "../../Engine/Shapes/MeshLoader.h" #include "../../Engine/Camera/Perspective.h" #include "../../Engine/Utilities/Managers.h" +#include "../../Engine/Utilities/Simulation.h" +#include "ColoredActor.h" +#include "MoveController.h" #define VERTICES 0 #define COLORS 1 using json = nlohmann::json; +using namespace std; + namespace ThreeEngine { @@ -24,24 +29,35 @@ namespace ThreeEngine { LoaderAndScene::~LoaderAndScene() = default; void LoaderAndScene::OnInit() { + setupShaders(); + setupMeshes(); scene(); } - void LoaderAndScene::scene() { - { // 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); - } + void LoaderAndScene::setupShaders() { + 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")); + void LoaderAndScene::setupMeshes() { + MeshManager::instance()->Add("Cube", + MeshLoader::instance()->LoadFileOBJ( + "assets/Cube-vtn.obj")); + MeshManager::instance()->Add("Pyramid", + MeshLoader::instance()->LoadFileOBJ( + "assets/Pyramid-vtn.obj")); + MeshManager::instance()->Add("Parallelogram", + MeshLoader::instance()->LoadFileOBJ( + "assets/Parallelogram-vtn.obj")); + } + void LoaderAndScene::scene() { actors.push_back(reinterpret_cast(sceneGraph)); { // Camera handling @@ -50,60 +66,109 @@ namespace ThreeEngine { number aspect = width / height; Camera* camera = new Camera( static_cast( - ShaderProgramManager::instance()->Get("default") - ->GetUniformBlockBidingId("SharedMatrices")), + ShaderProgramManager::instance()->Get( + "default")->GetUniformBlockBidingId( + "SharedMatrices")), new Perspective(30, aspect, 1, 100), - new Matrix(Matrix::TranslationMatrix(Vector(0, 0, -15))) + new Matrix(Matrix::TranslationMatrix(Vector(0, 0, -30))) // new LookAt({5, 0.5f, 0}, {0, 0.5f, 0}, {0, 1, 0}) ); sceneGraph->SetCamera(camera); controller.camera = camera; } - //{ - // 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 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 root = new Actor(); + root->setShaderProgram( + ShaderProgramManager::instance()->Get("color3d")); + root->transform.translation = Vector(0, -5, 0); + sceneGraph->SetRoot(root); + + auto floor = new ColoredActor(); + floor->color = Vector4(.3f, .3f, .3f, 1); + floor->mesh = MeshManager::instance()->Get("Cube"); + floor->transform.scale = Vector(3, .1f, 3); + floor->transform.translation = Vector(-3, -.1f, 3); + floor->SetParent(root); + + const number side = sqrt(16.0f); + const number hypotenuse = sqrt(side * side * 2); + + { + { // Big Top Triangle + auto* pyramid = new ColoredActor(); + pyramid->transform.rotation = + Quat::FromAngleAxis(45, Vector(0, 1, 0)); + pyramid->transform.scale = Vector(2); + pyramid->color = Vector4(.8f, .8f, .5f, 1); + pyramid->mesh = MeshManager::instance()->Get("Pyramid"); + pyramid->SetParent(root); + } + { // Big Left Triangle + auto* pyramid = new ColoredActor(); + pyramid->transform.rotation = + Quat::FromAngleAxis(45 + 90, Vector(0, 1, 0)); + pyramid->transform.scale = Vector(2); + pyramid->color = Vector4(.8f, .5f, .5f, 1); + pyramid->mesh = MeshManager::instance()->Get("Pyramid"); + pyramid->SetParent(root); + } + { // Small Top Right Triangle + auto* pyramid = new ColoredActor(); + pyramid->transform.rotation = + Quat::FromAngleAxis(-45, Vector(0, 1, 0)); + pyramid->transform.translation = Vector(hypotenuse * .25f, 0, + -hypotenuse * .25f); + pyramid->color = Vector4(.8f, .5f, .5f, 1); + pyramid->mesh = MeshManager::instance()->Get("Pyramid"); + pyramid->SetParent(root); + } + { // Square + auto* cube = new ColoredActor(); + cube->transform.rotation = + Quat::FromAngleAxis(-45, Vector(0, 1, 0)); + cube->color = Vector4(.2f, .8f, .5f, 1); + cube->mesh = MeshManager::instance()->Get("Cube"); + cube->SetParent(root); + } + { // Small Center Triangle + auto* pyramid = new ColoredActor(); + pyramid->transform.rotation = + Quat::FromAngleAxis(-45 - 90, Vector(0, 1, 0)); + pyramid->color = Vector4(.6f, .5f, .4f, 1); + pyramid->mesh = MeshManager::instance()->Get("Pyramid"); + pyramid->SetParent(root); + } + { // Parallelogram + auto* parallelogram = new ColoredActor(); + parallelogram->transform.scale *= Vector(hypotenuse * .25f, hypotenuse * .25f, 1); + parallelogram->transform.rotation = + Quat::FromAngleAxis(-90, Vector(1, 0, 0)); + parallelogram->transform.translation = + Vector(-hypotenuse * .5f, 2, hypotenuse * .5f); + parallelogram->color = Vector4(.2f, .5f, .8f, 1); + parallelogram->mesh = MeshManager::instance()->Get( + "Parallelogram"); + parallelogram->SetParent(root); + } + { // Medium Triangle + auto* pyramid = new ColoredActor(); + pyramid->transform.scale *= Vector(hypotenuse * .25f); + pyramid->transform.rotation = + Quat::FromAngleAxis(90, Vector(0, 1, 0)); + pyramid->transform.translation = + Vector(hypotenuse * .5f, 0, hypotenuse * .5f); + pyramid->color = Vector4(.6f, .5f, .4f, 1); + pyramid->mesh = MeshManager::instance()->Get("Pyramid"); + pyramid->SetParent(root); } - - auto* cube3 = new Actor(); - cube3->mesh = MeshManager::instance()->Get("Cube"); - cube3->transform.translation += Vector(0, 2, 0); - cube3->SetParent(root); } // Initializes all the actors in it. sceneGraph->Init(); + + auto moveController = new MoveController(); + moveController->actor = root; + Simulation::instance()->Add(moveController); } void LoaderAndScene::OnReshape(int, int) { @@ -114,10 +179,20 @@ namespace ThreeEngine { } void LoaderAndScene::PreDraw() { - if (input.Click(27)) { + controller.Update(); + } + + void LoaderAndScene::Update() { + if (input.Click(27)) { // ESC Exit(); } - controller.Update(); + Simulation::instance()->Update(); + } + + void LoaderAndScene::OnCleanup() { + ShaderProgramManager::instance()->cleanup(); + MeshManager::instance()->cleanup(); + Simulation::instance()->cleanup(); } } /* namespace Divisaction */ \ No newline at end of file diff --git a/src/Executable/LoaderAndScene/LoaderAndScene.h b/src/Executable/LoaderAndScene/LoaderAndScene.h index efacd9a..5d66221 100644 --- a/src/Executable/LoaderAndScene/LoaderAndScene.h +++ b/src/Executable/LoaderAndScene/LoaderAndScene.h @@ -33,6 +33,14 @@ namespace ThreeEngine { void OnReshape(int w, int h) override; void PreDraw() override; + + void Update() override; + + void setupShaders(); + + void setupMeshes(); + + void OnCleanup() override; }; } /* namespace Divisaction */ diff --git a/src/Executable/LoaderAndScene/MoveController.cpp b/src/Executable/LoaderAndScene/MoveController.cpp new file mode 100644 index 0000000..9b72375 --- /dev/null +++ b/src/Executable/LoaderAndScene/MoveController.cpp @@ -0,0 +1,59 @@ +/* + * File MoveController.cpp in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#include "../../Engine/Engine.h" +#include "MoveController.h" + +namespace ThreeEngine { + + MoveController::MoveController() : angle(0), front(), actor(nullptr) { } + + MoveController::~MoveController() = default; + + void MoveController::Update(milliseconds delta) { + if (!actor) { + return; + } + Input& input = Engine::Instance()->input; + + number astep = delta * 0.05f; + number vstep = delta * .005f; + + if (input['a']) { + actor->transform.rotation = + Quat::FromAngleAxis(astep, Vector(0, 1, 0)) * + actor->transform.rotation; + angle += astep; + } + if (input['d']) { + actor->transform.rotation = + Quat::FromAngleAxis(-astep, Vector(0, 1, 0)) * + actor->transform.rotation; + angle -= astep; + } + if (input['q']) { + actor->transform.rotation = + Quat::FromAngleAxis(astep, Vector(0, 0, 1)) * + actor->transform.rotation; + } + if (input['e']) { + actor->transform.rotation = + Quat::FromAngleAxis(-astep, Vector(0, 0, 1)) * + actor->transform.rotation; + } + if (input['w']) { + actor->transform.translation += front * vstep; + } + if (input['s']) { + actor->transform.translation -= front * vstep; + } + + + Quat qr = Quat::FromAngleAxis(angle, Vector(0, 1, 0)); + Quat qi(0, 0, 0, -1); + Quat qf = qr * qi * qr.Inverse(); + front = Vector(qf.X, qf.Y, qf.Z); + } +} /* namespace ThreeEngine */ \ No newline at end of file diff --git a/src/Executable/LoaderAndScene/MoveController.h b/src/Executable/LoaderAndScene/MoveController.h new file mode 100644 index 0000000..08a280e --- /dev/null +++ b/src/Executable/LoaderAndScene/MoveController.h @@ -0,0 +1,30 @@ +/* + * File MoveController.h in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#ifndef THREEENGINE_MOVECONTROLLER_H +#define THREEENGINE_MOVECONTROLLER_H + +#include "../../Engine/Actor.h" +#include "../../Engine/Utilities/Simulation.h" + +namespace ThreeEngine { + + class MoveController : public IUpdatable { + public: + number angle; + Vector front; + + Actor* actor; + + MoveController(); + + virtual ~MoveController(); + + void Update(milliseconds delta) override; + }; + +} /* namespace ThreeEngine */ + +#endif //THREEENGINE_MOVECONTROLLER_H