Merge pull request 'Morphable Camera & Controller' (#56) from morphable_camera_and_controller into master
Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/56 Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
@@ -233,7 +233,7 @@ namespace OpenVulkano::Scene
|
||||
return 2.0f * atanf(tanf(m_fov * 0.5f) * m_aspect);
|
||||
}
|
||||
|
||||
void UpdateProjectionMatrix() final
|
||||
void UpdateProjectionMatrix()
|
||||
{
|
||||
m_projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane);
|
||||
UpdateViewProjectionMatrix();
|
||||
|
||||
@@ -18,6 +18,6 @@ namespace OpenVulkano::Scene
|
||||
struct Material
|
||||
{
|
||||
MaterialProperties properties;
|
||||
Texture* texture;
|
||||
Texture* texture = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
42
openVulkanoCpp/Scene/MorphableCamera.cpp
Normal file
42
openVulkanoCpp/Scene/MorphableCamera.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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 "MorphableCamera.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
namespace
|
||||
{
|
||||
Math::Matrix4f BlendMatrices(const Math::Matrix4f& mat1, const Math::Matrix4f& mat2, float t)
|
||||
{
|
||||
Math::Matrix4f result;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = Math::Utils::mix(mat1[i], mat2[i], t);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
void MorphableCamera::UpdateProjectionMatrix()
|
||||
{
|
||||
PerspectiveCamera::UpdateProjectionMatrix();
|
||||
if (m_morphState != 0)
|
||||
{
|
||||
float aspectH = m_aspect;
|
||||
float aspectV = 1;
|
||||
if (m_morphState == 1)
|
||||
{
|
||||
m_projection = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane);
|
||||
m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState);
|
||||
}
|
||||
UpdateViewProjectionMatrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
33
openVulkanoCpp/Scene/MorphableCamera.hpp
Normal file
33
openVulkanoCpp/Scene/MorphableCamera.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 "Camera.hpp"
|
||||
#include "Math/Math.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
class MorphableCamera : public PerspectiveCamera
|
||||
{
|
||||
float m_morphState;
|
||||
Math::Matrix4f m_orthoMatrix;
|
||||
|
||||
public:
|
||||
MorphableCamera(float fovDegrees, float width, float height, float nearPlane, float farPlane)
|
||||
: PerspectiveCamera(fovDegrees, nearPlane, farPlane, width, height), m_morphState(0.0f)
|
||||
{
|
||||
UpdateProjectionMatrix();
|
||||
}
|
||||
|
||||
void SetMorphState(float state)
|
||||
{
|
||||
m_morphState = Math::Utils::clamp(state, 0.0f, 1.0f);
|
||||
UpdateProjectionMatrix();
|
||||
}
|
||||
|
||||
void UpdateProjectionMatrix() override;
|
||||
};
|
||||
}
|
||||
65
openVulkanoCpp/Scene/MorphableCameraController.cpp
Normal file
65
openVulkanoCpp/Scene/MorphableCameraController.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 "MorphableCameraController.hpp"
|
||||
#include "Input/InputManager.hpp"
|
||||
#include "Input/InputKey.hpp"
|
||||
#include "Base/FrameMetadata.hpp"
|
||||
#include "Scene/Camera.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
MorphableCameraController::MorphableCameraController(MorphableCamera* camera)
|
||||
: CameraController(camera)
|
||||
, m_animationDuration(1.0f)
|
||||
, m_currentTime(0.0f)
|
||||
, m_isMorphing(false)
|
||||
, m_targetMorphStatePerspective(false)
|
||||
{
|
||||
auto input = OpenVulkano::Input::InputManager::GetInstance();
|
||||
m_actionMorph = input->GetAction("morph");
|
||||
}
|
||||
|
||||
void MorphableCameraController::Init(MorphableCamera* camera)
|
||||
{
|
||||
CameraController::Init(camera);
|
||||
m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P);
|
||||
}
|
||||
|
||||
void MorphableCameraController::Tick()
|
||||
{
|
||||
auto input = OpenVulkano::Input::InputManager::GetInstance();
|
||||
|
||||
bool isMorphingDown = input->GetButton(m_actionMorph);
|
||||
if (!m_wasMorphingKeyDown && isMorphingDown)
|
||||
{
|
||||
m_isMorphing = true;
|
||||
m_currentTime = 0.0f;
|
||||
m_targetMorphStatePerspective = !m_targetMorphStatePerspective;
|
||||
}
|
||||
m_wasMorphingKeyDown = isMorphingDown;
|
||||
|
||||
if (m_isMorphing)
|
||||
{
|
||||
m_currentTime += CURRENT_FRAME.frameTime;
|
||||
if (m_currentTime > m_animationDuration) m_currentTime = m_animationDuration;
|
||||
float t = m_currentTime / m_animationDuration;
|
||||
if (t >= 1.0f)
|
||||
{
|
||||
t = 1.0f;
|
||||
m_isMorphing = false;
|
||||
}
|
||||
float newState = m_targetMorphStatePerspective ? t : (1.0f - t);
|
||||
static_cast<MorphableCamera*>(GetCamera())->SetMorphState(newState);
|
||||
}
|
||||
}
|
||||
|
||||
void MorphableCameraController::SetTargetState(bool toPerspective)
|
||||
{
|
||||
m_targetMorphStatePerspective = toPerspective;
|
||||
m_isMorphing = true;
|
||||
}
|
||||
}
|
||||
37
openVulkanoCpp/Scene/MorphableCameraController.hpp
Normal file
37
openVulkanoCpp/Scene/MorphableCameraController.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 "MorphableCamera.hpp"
|
||||
#include "Controller/CameraController.hpp"
|
||||
#include "Input/InputManager.hpp"
|
||||
#include "Input/InputKey.hpp"
|
||||
#include "Math/Math.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
class MorphableCameraController final : public CameraController
|
||||
{
|
||||
double m_animationDuration;
|
||||
double m_currentTime;
|
||||
bool m_isMorphing;
|
||||
bool m_targetMorphStatePerspective; // true for perspective, false for orthographic
|
||||
|
||||
Input::InputAction* m_actionMorph;
|
||||
bool m_wasMorphingKeyDown = false;
|
||||
|
||||
public:
|
||||
MorphableCameraController(MorphableCamera* camera = nullptr);
|
||||
|
||||
void Init(MorphableCamera* camera);
|
||||
void Tick() override;
|
||||
|
||||
void SetDuration(double duration) { m_animationDuration = duration; }
|
||||
double GetDuration() { return m_animationDuration; }
|
||||
void SetTargetState(bool toPerspective);
|
||||
void Reset() { m_currentTime = 0; }
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user