diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp new file mode 100644 index 0000000..bc9be4b --- /dev/null +++ b/examples/ExampleApps/MovingCubeApp.cpp @@ -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 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(), OpenVulkano::Math::Vector3f_SIMD(-3, 0, 0)); + OpenVulkano::Math::Pose destPose(OpenVulkano::Math::Quaternion(), OpenVulkano::Math::Vector3f_SIMD(3, 0, 0)); + m_animationController = std::make_unique(&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 MovingCubeApp::CreateUnique() + { + return std::make_unique(); + } +} diff --git a/examples/ExampleApps/MovingCubeApp.hpp b/examples/ExampleApps/MovingCubeApp.hpp new file mode 100644 index 0000000..75d464e --- /dev/null +++ b/examples/ExampleApps/MovingCubeApp.hpp @@ -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 + +#include "Base/IGraphicsApp.hpp" + +namespace OpenVulkano +{ + class MovingCubeApp : public IGraphicsApp + { + public: + static std::unique_ptr CreateUnique(); + static IGraphicsApp* Create(); + + std::string GetAppName() const final { return "Moving Cube App"; } + OpenVulkano::Version GetAppVersion() const final { return {"v1.0"}; } + }; +} \ No newline at end of file diff --git a/examples/main.cpp b/examples/main.cpp index acc1b76..7e9c527 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -6,12 +6,14 @@ #include "Host/GraphicsAppManager.hpp" #include "ExampleApps/CubesExampleApp.hpp" +#include "ExampleApps/MovingCubeApp.hpp" using namespace OpenVulkano; int main(int argc, char** argv) { - std::unique_ptr app = CubesExampleApp::CreateUnique(); + // std::unique_ptr app = CubesExampleApp::CreateUnique(); + std::unique_ptr app = MovingCubeApp::CreateUnique(); GraphicsAppManager manager(app.get()); manager.Run(); return 0; diff --git a/openVulkanoCpp/Scene/SimpleAnimationController.cpp b/openVulkanoCpp/Scene/SimpleAnimationController.cpp new file mode 100644 index 0000000..03fae9a --- /dev/null +++ b/openVulkanoCpp/Scene/SimpleAnimationController.cpp @@ -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 + +#include "SimpleAnimationController.hpp" +#include "Base/FrameMetadata.hpp" + +namespace OpenVulkano::Scene +{ + SimpleAnimationController::SimpleAnimationController(OpenVulkano::Scene::Node *node, OpenVulkano::Math::Pose initialPose, OpenVulkano::Math::Pose 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 currentPose = m_initialPose.Interpolate(m_targetPose, progress); + m_node->SetMatrix(currentPose.ToMatrix()); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/SimpleAnimationController.hpp b/openVulkanoCpp/Scene/SimpleAnimationController.hpp new file mode 100644 index 0000000..720e9f8 --- /dev/null +++ b/openVulkanoCpp/Scene/SimpleAnimationController.hpp @@ -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 m_targetPose; + OpenVulkano::Math::Pose m_initialPose; + float m_duration = 0; + float m_elapsed = 0; + + public: + SimpleAnimationController() = default; + SimpleAnimationController(OpenVulkano::Scene::Node *node, OpenVulkano::Math::Pose initialPose, OpenVulkano::Math::Pose targetPose, float duration); + void OnAnimationCompleted(); + void Tick() override; + }; +} \ No newline at end of file