diff --git a/CMakeLists.txt b/CMakeLists.txt index 82dc8ca..f70a9b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) + src/Executable/LoaderAndScene/ColoredActor.cpp src/Executable/LoaderAndScene/ColoredActor.h src/Executable/LoaderAndScene/MoveController.cpp src/Executable/LoaderAndScene/MoveController.h src/Executable/LoaderAndScene/TangramAnimation.cpp src/Executable/LoaderAndScene/TangramAnimation.h) set(TESTS_SOURCE_FILES src/Tests/catch.hpp src/Tests/tests.cpp src/Tests/VectorTests.h src/Tests/MatrixTests.h) diff --git a/src/Engine/Actor.cpp b/src/Engine/Actor.cpp index 1d4ecd0..7add609 100644 --- a/src/Engine/Actor.cpp +++ b/src/Engine/Actor.cpp @@ -68,7 +68,9 @@ namespace ThreeEngine { } Matrix Actor::GetModelMatrix() { - return GetParentModelMatrix() * GetLocalModelMatrix(); + auto local = GetLocalModelMatrix(); + auto parent = GetParentModelMatrix(); + return parent * local; } Matrix Actor::GetLocalModelMatrix() { @@ -81,7 +83,7 @@ namespace ThreeEngine { if (!parent) { return Matrix::IdentityMatrix(); } - return parent->GetParentModelMatrix() * parent->GetModelMatrix(); + return parent->GetModelMatrix(); } void Actor::setShaderProgram( diff --git a/src/Engine/Controllers/SphereCameraController.cpp b/src/Engine/Controllers/SphereCameraController.cpp index f80ce29..2727be8 100644 --- a/src/Engine/Controllers/SphereCameraController.cpp +++ b/src/Engine/Controllers/SphereCameraController.cpp @@ -28,7 +28,7 @@ namespace ThreeEngine { // Toggle between Ortho and Perspective if (engine->input.Click('p')) { if (inPerspective) { - camera->SetProjection(new Ortho(-2, 2, -2, 2, 1, 100)); + camera->SetProjection(new Ortho(-10, 10, -10, 10, 1, 100)); inPerspective = false; } else { number width = engine->config["window"]["x"]; diff --git a/src/Engine/Math/Vector.cpp b/src/Engine/Math/Vector.cpp index 013ad2e..5b53089 100644 --- a/src/Engine/Math/Vector.cpp +++ b/src/Engine/Math/Vector.cpp @@ -263,4 +263,9 @@ namespace ThreeEngine { Vector Vector::ProjectOnTo(const Vector& A) const { return A * (((*this) | A) / (A | A)); } + + + Vector Vector::Lerp(const Vector& v0, const Vector& v1, number time) { + return ((1 - time) * v0) + (time * v1); + } } /* namespace Divisaction */ \ No newline at end of file diff --git a/src/Engine/Math/Vector.h b/src/Engine/Math/Vector.h index 5043d60..db25274 100644 --- a/src/Engine/Math/Vector.h +++ b/src/Engine/Math/Vector.h @@ -359,6 +359,7 @@ namespace ThreeEngine { */ Vector ProjectOnTo(const Vector& A) const; + static Vector Lerp(const Vector& v0, const Vector& v1, number time); }; } /* namespace Divisaction */ diff --git a/src/Executable/LoaderAndScene/LoaderAndScene.cpp b/src/Executable/LoaderAndScene/LoaderAndScene.cpp index 05731c8..adac306 100644 --- a/src/Executable/LoaderAndScene/LoaderAndScene.cpp +++ b/src/Executable/LoaderAndScene/LoaderAndScene.cpp @@ -12,6 +12,7 @@ #include "../../Engine/Utilities/Simulation.h" #include "ColoredActor.h" #include "MoveController.h" +#include "TangramAnimation.h" #define VERTICES 0 #define COLORS 1 @@ -93,6 +94,9 @@ namespace ThreeEngine { const number side = sqrt(16.0f); const number hypotenuse = sqrt(side * side * 2); + auto tangram = new Actor(); + tangram->SetParent(root); + { { // Big Top Triangle auto* pyramid = new ColoredActor(); @@ -101,7 +105,7 @@ namespace ThreeEngine { pyramid->transform.scale = Vector(2); pyramid->color = Vector4(.8f, .8f, .5f, 1); pyramid->mesh = MeshManager::instance()->Get("Pyramid"); - pyramid->SetParent(root); + pyramid->SetParent(tangram); } { // Big Left Triangle auto* pyramid = new ColoredActor(); @@ -110,7 +114,7 @@ namespace ThreeEngine { pyramid->transform.scale = Vector(2); pyramid->color = Vector4(.8f, .5f, .5f, 1); pyramid->mesh = MeshManager::instance()->Get("Pyramid"); - pyramid->SetParent(root); + pyramid->SetParent(tangram); } { // Small Top Right Triangle auto* pyramid = new ColoredActor(); @@ -120,7 +124,7 @@ namespace ThreeEngine { -hypotenuse * .25f); pyramid->color = Vector4(.8f, .5f, .5f, 1); pyramid->mesh = MeshManager::instance()->Get("Pyramid"); - pyramid->SetParent(root); + pyramid->SetParent(tangram); } { // Square auto* cube = new ColoredActor(); @@ -128,7 +132,7 @@ namespace ThreeEngine { Quat::FromAngleAxis(-45, Vector(0, 1, 0)); cube->color = Vector4(.2f, .8f, .5f, 1); cube->mesh = MeshManager::instance()->Get("Cube"); - cube->SetParent(root); + cube->SetParent(tangram); } { // Small Center Triangle auto* pyramid = new ColoredActor(); @@ -136,11 +140,12 @@ namespace ThreeEngine { Quat::FromAngleAxis(-45 - 90, Vector(0, 1, 0)); pyramid->color = Vector4(.6f, .5f, .4f, 1); pyramid->mesh = MeshManager::instance()->Get("Pyramid"); - pyramid->SetParent(root); + pyramid->SetParent(tangram); } { // Parallelogram auto* parallelogram = new ColoredActor(); - parallelogram->transform.scale *= Vector(hypotenuse * .25f, hypotenuse * .25f, 1); + parallelogram->transform.scale *= Vector(hypotenuse * .25f, + hypotenuse * .25f, 1); parallelogram->transform.rotation = Quat::FromAngleAxis(-90, Vector(1, 0, 0)); parallelogram->transform.translation = @@ -148,7 +153,7 @@ namespace ThreeEngine { parallelogram->color = Vector4(.2f, .5f, .8f, 1); parallelogram->mesh = MeshManager::instance()->Get( "Parallelogram"); - parallelogram->SetParent(root); + parallelogram->SetParent(tangram); } { // Medium Triangle auto* pyramid = new ColoredActor(); @@ -157,9 +162,9 @@ namespace ThreeEngine { Quat::FromAngleAxis(90, Vector(0, 1, 0)); pyramid->transform.translation = Vector(hypotenuse * .5f, 0, hypotenuse * .5f); - pyramid->color = Vector4(.6f, .5f, .4f, 1); + pyramid->color = Vector4(.6f, .9f, .4f, 1); pyramid->mesh = MeshManager::instance()->Get("Pyramid"); - pyramid->SetParent(root); + pyramid->SetParent(tangram); } } @@ -169,6 +174,55 @@ namespace ThreeEngine { auto moveController = new MoveController(); moveController->actor = root; Simulation::instance()->Add(moveController); + + auto animation = new TangramAnimation(); + animation->actors = tangram->children; + animation->Init(); + // FISH + { // Big Top Triangle + animation->finalTransform[0].translation = Vector(2, 10, 0); + animation->finalTransform[0].rotation = + Quat::FromAngleAxis(90, Vector(1, 0, 0)) * + Quat::FromAngleAxis(180, Vector(0, 1, 0)); + } + { // Big Left Triangle + animation->finalTransform[1].translation = Vector(2, 2, 0); + animation->finalTransform[1].rotation = + Quat::FromAngleAxis(90, Vector(1, 0, 0)) * + Quat::FromAngleAxis(90, Vector(0, 1, 0)); + } + { // Small Top Right Triangle + animation->finalTransform[2].translation = Vector(-3, 6, 0); + animation->finalTransform[2].rotation = + Quat::FromAngleAxis(90, Vector(1, 0, 0)) * + Quat::FromAngleAxis(-90, Vector(0, 1, 0)); + } + { // Square + animation->finalTransform[3].translation = Vector(2, 6, 0); + animation->finalTransform[3].rotation = + Quat::FromAngleAxis(180, Vector(0, 1, 0)) * + Quat::FromAngleAxis(90, Vector(1, 0, 0)) * + Quat::FromAngleAxis(-45, Vector(0, 1, 0)); + } + { // Small Center Triangle + animation->finalTransform[4].translation = Vector(-3, 4, 0); + animation->finalTransform[4].rotation = + Quat::FromAngleAxis(90, Vector(1, 0, 0)) * + Quat::FromAngleAxis(90, Vector(0, 1, 0)); + } + { // Parallelogram + animation->finalTransform[5].translation = Vector(-1, 6, 0); + animation->finalTransform[5].rotation = + Quat::FromAngleAxis(90 + 45, Vector(0, 0, 1)); + } + { // Medium Triangle + animation->finalTransform[6].translation = + Vector(2 + side/2.0f, 6, 0); + animation->finalTransform[6].rotation = + Quat::FromAngleAxis(90, Vector(1, 0, 0)) * + Quat::FromAngleAxis(-180 - 45, Vector(0, 1, 0)); + } + Simulation::instance()->Add(animation); } void LoaderAndScene::OnReshape(int, int) { diff --git a/src/Executable/LoaderAndScene/TangramAnimation.cpp b/src/Executable/LoaderAndScene/TangramAnimation.cpp new file mode 100644 index 0000000..d8000d7 --- /dev/null +++ b/src/Executable/LoaderAndScene/TangramAnimation.cpp @@ -0,0 +1,49 @@ +/* + * File TangramAnimation.cpp in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#include "../../Engine/Engine.h" +#include "TangramAnimation.h" + +namespace ThreeEngine { + + TangramAnimation::TangramAnimation() = default; + + TangramAnimation::~TangramAnimation() = default; + + void TangramAnimation::Init() { + for (auto&& actor : actors) { + initialTransform.push_back(actor->transform); + finalTransform.push_back(actor->transform); + } + } + + void TangramAnimation::Update(milliseconds delta) { + auto&& input = Engine::Instance()->input; + + if (input.Click('z')) { + toogle = !toogle; + } + + const number lstep = delta * speed; + + auto* transforms = &initialTransform; + if (toogle) { + transforms = &finalTransform; + } + + for (unsigned int i = 0; i < actors.size(); ++i) { + actors[i]->transform.translation = + Vector::Lerp(actors[i]->transform.translation, + (*transforms)[i].translation, lstep); + actors[i]->transform.scale = + Vector::Lerp(actors[i]->transform.scale, + (*transforms)[i].scale, lstep); + actors[i]->transform.rotation = + Quat::Lerp(actors[i]->transform.rotation, + (*transforms)[i].rotation, lstep); + } + } + +} /* namespace ThreeEngine */ \ No newline at end of file diff --git a/src/Executable/LoaderAndScene/TangramAnimation.h b/src/Executable/LoaderAndScene/TangramAnimation.h new file mode 100644 index 0000000..f533285 --- /dev/null +++ b/src/Executable/LoaderAndScene/TangramAnimation.h @@ -0,0 +1,36 @@ +/* + * File TangramAnimation.h in project ThreeEngine + * + * Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved + */ +#ifndef THREEENGINE_TANGRAMANIMATION_H +#define THREEENGINE_TANGRAMANIMATION_H + +#include "../../Engine/Actor.h" +#include "../../Engine/Utilities/Simulation.h" + +namespace ThreeEngine { + + class TangramAnimation : public IUpdatable { + private: + bool toogle{false}; + std::vector initialTransform; + + public: + number speed = 0.005f; + + std::vector actors; + std::vector finalTransform; + + TangramAnimation(); + + virtual ~TangramAnimation(); + + void Init(); + + void Update(milliseconds delta) override; + }; + +} /* namespace ThreeEngine */ + +#endif //THREEENGINE_TANGRAMANIMATION_H