Files
OpenVulkano/examples/ExampleApps/MovingCubeApp.cpp
Vladyslav Baranovskyi 1311165139 Summary:
- In MovingCubeApp animations are now allocated on stack
- m_sequenceAnimationController now uses SetAnimationPoseResetTime()
- Removed OnSequenceAnimationCompleted()
- Pose::GetOrientation() now returns by value(without it my code doesn't compile)
- GetStep() getter
- In if(m_loop) checking for m_resetTime to be zero or last step to be equal to the first
- Tick now ticks only if there are at least one element in m_steps
- IsFinished() function
- AddAnimationSteps() functions that take initializer_lists
- Getters in SimpleAnimationController are now const
2024-06-08 17:36:03 +03:00

128 lines
4.6 KiB
C++

/*
* 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 "Scene/SequenceAnimationController.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Base/Logger.hpp"
#include "Controller/FreeCamCameraController.hpp"
namespace OpenVulkano
{
namespace
{
struct SceneElement
{
Scene::Geometry m_geometry;
Scene::SimpleDrawable m_drawable;
Scene::Node m_node;
};
}
class MovingCubeAppImpl final : public MovingCubeApp
{
Scene::Scene m_scene;
Scene::PerspectiveCamera m_camera;
FreeCamCameraController m_cameraControl;
Scene::Material m_material;
Scene::Shader m_shader;
Scene::SimpleAnimationController m_simpleAnimationController;
Scene::SequenceAnimationController m_sequenceAnimationController;
SceneElement m_whiteBox;
SceneElement m_redBox;
void CreateSceneElement(SceneElement *dest, const Math::Vector4f &color, float scale)
{
dest->m_geometry.InitCube(scale, scale, scale, color);
dest->m_drawable.Init(&m_shader, &dest->m_geometry, &m_material);
dest->m_node.Init();
m_scene.GetRoot()->AddChild(&dest->m_node);
dest->m_node.SetUpdateFrequency(Scene::UpdateFrequency::Always);
dest->m_node.AddDrawable(&dest->m_drawable);
dest->m_node.SetMatrix(Math::Matrix4f(1));
}
public:
void Init() override
{
auto engineConfig = 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(ShaderProgramType::VERTEX, "Shader/basic");
m_shader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/basic");
m_shader.AddVertexInputDescription(Vertex::GetVertexInputDescription());
GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
m_cameraControl.Init(&m_camera);
m_cameraControl.SetDefaultKeybindings();
CreateSceneElement(&m_whiteBox, Math::Vector4f(1, 1, 1, 1), 1);
CreateSceneElement(&m_redBox, Math::Vector4f(1, 0.2, 0.2, 1.0), 0.3);
m_simpleAnimationController.SetNode(&m_whiteBox.m_node);
m_simpleAnimationController.SetDuration(3);
Math::Pose srcPose(Math::Quaternion<float>(), Math::Vector3f_SIMD(-3, 0, 0));
Math::Pose destPose(Math::Quaternion<float>(), Math::Vector3f_SIMD(3, 0, 0));
m_simpleAnimationController.SetPoses(srcPose, destPose);
m_simpleAnimationController.m_completionEvent += EventHandler(this, &MovingCubeAppImpl::OnSimpleAnimationCompleted);
m_sequenceAnimationController.EnableLoop(true);
m_sequenceAnimationController.SetNode(&m_redBox.m_node);
m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 0, 0, 1)), Math::Vector3f_SIMD(0, 0, 1)), 5);
m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(2, 1, 0, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3);
m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 1, 1, 1)), Math::Vector3f_SIMD(2, 1, -2)), 3);
m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 1, 1, 0)), Math::Vector3f_SIMD(2, 1, -1)), 3);
m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(3, 2, 1, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3);
m_sequenceAnimationController.AddAnimationStep(Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 0, 1, 1)), Math::Vector3f_SIMD(0, 1, 0)), 1);
m_sequenceAnimationController.SetAnimationPoseResetTime(10);
}
void OnSimpleAnimationCompleted(Scene::SimpleAnimationController *anim)
{
anim->SwapPoses();
anim->Reset();
}
void Tick() override
{
m_cameraControl.Tick();
m_simpleAnimationController.Tick();
m_sequenceAnimationController.Tick();
}
void Close() override
{
}
};
IGraphicsApp* MovingCubeApp::Create()
{
return new MovingCubeAppImpl();
}
std::unique_ptr<IGraphicsApp> MovingCubeApp::CreateUnique()
{
return std::make_unique<MovingCubeAppImpl>();
}
}