SimpleAnimationController, MovingCubeApp

This commit is contained in:
Vladyslav Baranovskyi
2024-06-05 21:51:04 +03:00
parent 463f632432
commit 7111e46954
5 changed files with 193 additions and 1 deletions

View File

@@ -0,0 +1,89 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "MovingCubeApp.hpp"
#include "Math/Math.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Material.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/SimpleDrawable.hpp"
#include "Scene/Camera.hpp"
#include "Scene/SimpleAnimationController.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
namespace OpenVulkano
{
class MovingCubeAppImpl final : public MovingCubeApp
{
OpenVulkano::Scene::Scene m_scene;
OpenVulkano::Scene::PerspectiveCamera m_camera;
OpenVulkano::FreeCamCameraController m_cameraControl;
OpenVulkano::Scene::Material m_material;
OpenVulkano::Scene::Shader m_shader;
OpenVulkano::Scene::Geometry m_geometry;
OpenVulkano::Scene::SimpleDrawable m_drawable;
OpenVulkano::Scene::Node m_node;
std::unique_ptr<OpenVulkano::Scene::SimpleAnimationController> m_animationController;
public:
void Init() override
{
auto engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
m_camera.Init(70, 16, 9, 0.1, 100);
// m_camera.SetMatrix(OpenVulkano::Math::Utils::translate(OpenVulkano::Math::Matrix4f(1), OpenVulkano::Math::Vector3f_SIMD(0, 0, -50)));
m_scene.Init();
m_scene.SetCamera(&m_camera);
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/basic");
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basic");
m_shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
m_geometry.InitCube();
m_drawable.Init(&m_shader, &m_geometry, &m_material);
m_node.Init();
m_scene.GetRoot()->AddChild(&m_node);
m_node.SetUpdateFrequency(OpenVulkano::Scene::UpdateFrequency::Always);
m_node.AddDrawable(&m_drawable);
m_node.SetMatrix(OpenVulkano::Math::Matrix4f(1));
GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
m_cameraControl.Init(&m_camera);
m_cameraControl.SetDefaultKeybindings();
OpenVulkano::Math::Pose srcPose(OpenVulkano::Math::Quaternion<float>(), OpenVulkano::Math::Vector3f_SIMD(-3, 0, 0));
OpenVulkano::Math::Pose destPose(OpenVulkano::Math::Quaternion<float>(), OpenVulkano::Math::Vector3f_SIMD(3, 0, 0));
m_animationController = std::make_unique<OpenVulkano::Scene::SimpleAnimationController>(&m_node, srcPose, destPose, 3);
}
void Tick() override
{
m_cameraControl.Tick();
m_animationController->Tick();
}
void Close() override
{
}
};
IGraphicsApp* MovingCubeApp::Create()
{
return new MovingCubeAppImpl();
}
std::unique_ptr<IGraphicsApp> MovingCubeApp::CreateUnique()
{
return std::make_unique<MovingCubeAppImpl>();
}
}

View File

@@ -0,0 +1,24 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <memory>
#include "Base/IGraphicsApp.hpp"
namespace OpenVulkano
{
class MovingCubeApp : public IGraphicsApp
{
public:
static std::unique_ptr<IGraphicsApp> CreateUnique();
static IGraphicsApp* Create();
std::string GetAppName() const final { return "Moving Cube App"; }
OpenVulkano::Version GetAppVersion() const final { return {"v1.0"}; }
};
}

View File

@@ -6,12 +6,14 @@
#include "Host/GraphicsAppManager.hpp" #include "Host/GraphicsAppManager.hpp"
#include "ExampleApps/CubesExampleApp.hpp" #include "ExampleApps/CubesExampleApp.hpp"
#include "ExampleApps/MovingCubeApp.hpp"
using namespace OpenVulkano; using namespace OpenVulkano;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::unique_ptr<IGraphicsApp> app = CubesExampleApp::CreateUnique(); // std::unique_ptr<IGraphicsApp> app = CubesExampleApp::CreateUnique();
std::unique_ptr<IGraphicsApp> app = MovingCubeApp::CreateUnique();
GraphicsAppManager manager(app.get()); GraphicsAppManager manager(app.get());
manager.Run(); manager.Run();
return 0; return 0;

View File

@@ -0,0 +1,45 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <utility>
#include "SimpleAnimationController.hpp"
#include "Base/FrameMetadata.hpp"
namespace OpenVulkano::Scene
{
SimpleAnimationController::SimpleAnimationController(OpenVulkano::Scene::Node *node, OpenVulkano::Math::Pose<float> initialPose, OpenVulkano::Math::Pose<float> targetPose, float duration)
{
m_node = node;
m_initialPose = initialPose;
m_targetPose = targetPose;
m_duration = duration;
m_event += EventHandler(this, &SimpleAnimationController::OnAnimationCompleted);
}
void SimpleAnimationController::OnAnimationCompleted()
{
std::swap(m_targetPose, m_initialPose);
m_elapsed = 0;
}
void SimpleAnimationController::Tick()
{
if(!m_node || m_duration <= 0)
return;
m_elapsed += OpenVulkano::CURRENT_FRAME.frameTime;
float progress = m_elapsed / m_duration;
if(progress >= 1.0)
{
progress = 1;
m_event.NotifyAll();
}
OpenVulkano::Math::Pose<float> currentPose = m_initialPose.Interpolate(m_targetPose, progress);
m_node->SetMatrix(currentPose.ToMatrix());
}
}

View File

@@ -0,0 +1,32 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Base/ITickable.hpp"
#include "Base/Event.hpp"
#include "Math/Math.hpp"
#include "Math/Pose.hpp"
#include "Scene/Node.hpp"
namespace OpenVulkano::Scene
{
class SimpleAnimationController : public ITickable
{
OpenVulkano::Scene::Node *m_node = nullptr;
OpenVulkano::Event<> m_event;
OpenVulkano::Math::Pose<float> m_targetPose;
OpenVulkano::Math::Pose<float> m_initialPose;
float m_duration = 0;
float m_elapsed = 0;
public:
SimpleAnimationController() = default;
SimpleAnimationController(OpenVulkano::Scene::Node *node, OpenVulkano::Math::Pose<float> initialPose, OpenVulkano::Math::Pose<float> targetPose, float duration);
void OnAnimationCompleted();
void Tick() override;
};
}