Files
OpenVulkano/examples/ExampleApps/MovingCubeApp.cpp
2024-06-07 16:09:39 +03:00

136 lines
5.1 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;
std::unique_ptr<Scene::SimpleAnimationController> m_simpleAnimationController;
std::unique_ptr<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 = std::make_unique<Scene::SimpleAnimationController>();
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 = std::make_unique<Scene::SequenceAnimationController>();
m_sequenceAnimationController->EnableLoop(false);
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->m_sequenceCompletionEvent += EventHandler(this, &MovingCubeAppImpl::OnSequenceAnimationCompleted);
}
void OnSimpleAnimationCompleted(Scene::SimpleAnimationController *anim)
{
anim->SwapPoses();
anim->Reset();
}
void OnSequenceAnimationCompleted(Scene::SequenceAnimationController *anim)
{
Logger::APP->info("Animation sequence completed - restarting...");
m_sequenceAnimationController->Restart();
}
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>();
}
}