Rework arcball camera controller
This commit is contained in:
@@ -20,31 +20,36 @@ namespace OpenVulkano
|
||||
m_actionUp = input->GetAction("up");
|
||||
m_actionLookUp = input->GetAction("look up");
|
||||
m_actionLookSide = input->GetAction("look side");
|
||||
|
||||
m_actionForwardOrtho = input->GetAction("ArcForwardOrtho");
|
||||
m_actionSideOrtho = input->GetAction("ArcSideOrtho");
|
||||
m_actionUpOrtho = input->GetAction("ArcUpOrtho");
|
||||
m_actionZoomOrtho = input->GetAction("ArcZoomOrtho");
|
||||
}
|
||||
|
||||
void ArcballCameraController::Tick()
|
||||
void ArcballCameraController::HandleRotation()
|
||||
{
|
||||
if (!GetCamera()) return;
|
||||
Input::InputManager* input = Input::InputManager::GetInstance();
|
||||
float inYaw = (lockYaw) ? 0 : input->GetAxis(m_actionLookSide);
|
||||
float inPitch = (lockPitch) ? 0 : input->GetAxis(m_actionLookUp);
|
||||
Math::Vector4f_SIMD position = GetCamera()->GetPosition();
|
||||
bool updated = false;
|
||||
float inYaw = (m_lockYaw) ? 0 : input->GetAxis(m_actionLookSide);
|
||||
float inPitch = (m_lockPitch) ? 0 : input->GetAxis(m_actionLookUp);
|
||||
|
||||
if (inYaw != 0 || inPitch != 0)
|
||||
{
|
||||
yaw += inYaw;
|
||||
pitch = std::min(1.5f, std::max(-1.5f, pitch + inPitch));
|
||||
m_yaw += inYaw;
|
||||
m_pitch = std::min(1.5f, std::max(-1.5f, m_pitch + inPitch));
|
||||
|
||||
Math::Matrix4f rotateY = Math::Utils::rotate(yaw, Math::Vector3f_SIMD(0, 1, 0));
|
||||
Math::Matrix4f rotateY = Math::Utils::rotate(m_yaw, Math::Vector3f_SIMD(0, 1, 0));
|
||||
Math::Vector4f x = rotateY * Math::Vector4f(1, 0, 0, 0);
|
||||
Math::Matrix4f rotateX = Math::Utils::rotate(pitch, Math::Vector3f_SIMD(x));
|
||||
position = rotateX * rotateY * Math::Vector4f_SIMD(0, 0, 3, 0);
|
||||
position += m_pivotPoint;
|
||||
updated = true;
|
||||
Math::Matrix4f rotateX = Math::Utils::rotate(m_pitch, Math::Vector3f_SIMD(x));
|
||||
m_framePosition = rotateX * rotateY * Math::Vector4f_SIMD(0, 0, m_distance, 0);
|
||||
m_framePosition += m_pivotPoint;
|
||||
m_frameUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Move the camera and the pivot point
|
||||
void ArcballCameraController::HandleMovement()
|
||||
{
|
||||
Input::InputManager* input = Input::InputManager::GetInstance();
|
||||
Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), -input->GetAxis(m_actionForward));
|
||||
if (vec != Math::Vector3f_SIMD (0))
|
||||
{
|
||||
@@ -56,15 +61,52 @@ namespace OpenVulkano
|
||||
vec = vec * timeScale * 3.0f; // scale vector
|
||||
vec = GetCamera()->GetRotationMatrix() * Math::Vector3f(vec);
|
||||
const Math::Vector4f_SIMD movement(vec, 0);
|
||||
position += movement;
|
||||
m_framePosition += movement;
|
||||
m_pivotPoint += movement;
|
||||
updated = true;
|
||||
m_frameUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (updated)
|
||||
void ArcballCameraController::HandleMovementOrtho()
|
||||
{
|
||||
Input::InputManager* input = Input::InputManager::GetInstance();
|
||||
Math::Vector4f vec(input->GetAxis(m_actionSideOrtho), input->GetAxis(m_actionUpOrtho), -input->GetAxis(m_actionForwardOrtho), 0);
|
||||
if (vec != Math::Vector4f(0))
|
||||
{
|
||||
vec /= GetCamera()->GetZoom();
|
||||
vec /= GetCamera()->GetContentScaleFactor();
|
||||
|
||||
m_framePosition += vec;
|
||||
m_pivotPoint += vec;
|
||||
m_frameUpdated = true;
|
||||
}
|
||||
float zoom = Input::InputManager::GetInstance()->GetAxis(m_actionZoomOrtho);
|
||||
if (zoom != 0)
|
||||
{
|
||||
float tmp = sqrt(GetCamera()->GetZoom()) + zoom / std::max(10.0f, (100 - GetCamera()->GetZoom()));
|
||||
tmp = std::max(3.0f, tmp);
|
||||
GetCamera()->SetZoom(tmp * tmp);
|
||||
GetCamera()->UpdateProjectionMatrix();
|
||||
m_frameUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ArcballCameraController::Tick()
|
||||
{
|
||||
if (!GetCamera()) return;
|
||||
m_frameUpdated = false;
|
||||
m_framePosition = GetCamera()->GetPosition();
|
||||
|
||||
HandleRotation();
|
||||
if (GetCamera() -> IsOrtho())
|
||||
HandleMovementOrtho();
|
||||
else
|
||||
HandleMovement();
|
||||
|
||||
if (m_frameUpdated)
|
||||
{
|
||||
// Update the camera view
|
||||
GetCamera()->SetViewMatrix(Math::Utils::lookAt(reinterpret_cast<Math::Vector3f_SIMD&>(position),
|
||||
GetCamera()->SetViewMatrix(Math::Utils::lookAt(reinterpret_cast<Math::Vector3f_SIMD&>(m_framePosition),
|
||||
reinterpret_cast<Math::Vector3f_SIMD&>(m_pivotPoint),
|
||||
Math::Vector3f_SIMD(0, 1, 0)));
|
||||
CURRENT_FRAME.needsRedraw = true;
|
||||
@@ -73,11 +115,12 @@ namespace OpenVulkano
|
||||
|
||||
void ArcballCameraController::SetActive()
|
||||
{
|
||||
if (!GetCamera()) return;
|
||||
Math::Vector3f viewDir = Math::Utils::normalize(GetCamera()->GetViewDirection());
|
||||
m_pivotPoint = GetCamera()->GetPosition() + Math::Vector4f(viewDir * 3.0f, 0);
|
||||
m_pivotPoint = GetCamera()->GetPosition() + Math::Vector4f(viewDir * m_distance, 0);
|
||||
|
||||
pitch = Math::Utils::asin(viewDir.y);
|
||||
yaw = Math::Utils::atan(-viewDir.x, -viewDir.z);
|
||||
m_pitch = Math::Utils::asin(viewDir.y);
|
||||
m_yaw = Math::Utils::atan(-viewDir.x, -viewDir.z);
|
||||
}
|
||||
|
||||
void ArcballCameraController::SetDefaultKeybindings()
|
||||
@@ -98,5 +141,20 @@ namespace OpenVulkano
|
||||
m_actionLookSide->BindKey(Input::InputKey::Touch::AXIS_PAN_X, -0.001f);
|
||||
m_actionLookUp->BindKey(Input::InputKey::Mouse::AXIS_Y);
|
||||
m_actionLookSide->BindKey(Input::InputKey::Mouse::AXIS_X);
|
||||
|
||||
//m_actionForwardOrtho
|
||||
m_actionSideOrtho->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_X, -1);
|
||||
m_actionUpOrtho->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y);
|
||||
m_actionZoomOrtho->BindKey(Input::InputKey::Mouse::AXIS_WHEEL_Y, 2);
|
||||
m_actionZoomOrtho->BindKey(Input::InputKey::Touch::AXIS_PINCH, 0.2);
|
||||
}
|
||||
|
||||
void ArcballCameraController::SetPivotPoint(const Math::Vector4f_SIMD& pivotPoint, bool updateDistance)
|
||||
{
|
||||
m_pivotPoint = pivotPoint;
|
||||
if (updateDistance && GetCamera())
|
||||
{
|
||||
m_distance = Math::Utils::length(m_pivotPoint - GetCamera()->GetPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,18 +18,31 @@ namespace OpenVulkano
|
||||
|
||||
class ArcballCameraController final : public CameraController
|
||||
{
|
||||
Math::Vector4f_SIMD m_pivotPoint{ 0, 0, 0, 1 };
|
||||
float yaw = 0, pitch = 0;
|
||||
bool lockYaw = false, lockPitch = false;
|
||||
Math::Vector4f m_pivotPoint{ 0, 0, 0, 1 };
|
||||
float m_yaw = 0, m_pitch = 0, m_distance = 3;
|
||||
|
||||
Input::InputAction* m_actionForward = nullptr;
|
||||
Input::InputAction* m_actionSide = nullptr;
|
||||
Input::InputAction* m_actionUp = nullptr;
|
||||
Input::InputAction* m_actionLookUp = nullptr;
|
||||
Input::InputAction* m_actionLookSide = nullptr;
|
||||
Input::InputAction* m_actionForwardOrtho = nullptr;
|
||||
Input::InputAction* m_actionSideOrtho = nullptr;
|
||||
Input::InputAction* m_actionUpOrtho = nullptr;
|
||||
Input::InputAction* m_actionZoomOrtho = nullptr;
|
||||
|
||||
Math::Vector4f m_framePosition;
|
||||
bool m_frameUpdated = false;
|
||||
bool m_lockYaw = false, m_lockPitch = false;
|
||||
|
||||
void SetupInputActions();
|
||||
|
||||
void HandleRotation();
|
||||
|
||||
void HandleMovement();
|
||||
|
||||
void HandleMovementOrtho();
|
||||
|
||||
public:
|
||||
ArcballCameraController() { SetupInputActions(); }
|
||||
|
||||
@@ -37,9 +50,9 @@ namespace OpenVulkano
|
||||
|
||||
void Tick() override;
|
||||
|
||||
[[nodiscard]] const Math::Vector4f_SIMD& GetPivotPoint() const { return m_pivotPoint; }
|
||||
[[nodiscard]] const Math::Vector4f& GetPivotPoint() const { return m_pivotPoint; }
|
||||
|
||||
void SetPivotPoint(const Math::Vector4f_SIMD& pivotPoint) { m_pivotPoint = pivotPoint; }
|
||||
void SetPivotPoint(const Math::Vector4f& pivotPoint, bool updateDistance = false);
|
||||
|
||||
void SetActive() override;
|
||||
|
||||
@@ -47,13 +60,15 @@ namespace OpenVulkano
|
||||
|
||||
void Init(Scene::Camera *camera) override { CameraController::SetCamera(camera); SetActive(); }
|
||||
|
||||
void LockYaw(float value = INFINITY, bool locked = true) { lockYaw = locked; if (value != INFINITY) yaw = value; }
|
||||
void LockPitch(float value = INFINITY, bool locked = true) { lockPitch = locked; if (value != INFINITY) pitch = value; }
|
||||
void LockYaw(float value = INFINITY, bool locked = true) { m_lockYaw = locked; if (value != INFINITY) m_yaw = value; }
|
||||
void LockPitch(float value = INFINITY, bool locked = true) { m_lockPitch = locked; if (value != INFINITY) m_pitch = value; }
|
||||
|
||||
[[nodiscard]] float GetYaw() const { return yaw; }
|
||||
[[nodiscard]] float GetPitch() const { return pitch; }
|
||||
[[nodiscard]] float GetYaw() const { return m_yaw; }
|
||||
[[nodiscard]] float GetPitch() const { return m_pitch; }
|
||||
[[nodiscard]] float GetDistance() const { return m_distance; }
|
||||
|
||||
void SetYaw(float y) { yaw = y; }
|
||||
void SetPitch(float p) { pitch = p; }
|
||||
void SetYaw(float y) { m_yaw = y; }
|
||||
void SetPitch(float p) { m_pitch = p; }
|
||||
void SetDistance(float dist) { m_distance = dist; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -156,6 +156,9 @@ namespace OpenVulkano::Scene
|
||||
[[nodiscard]] std::array<uint8_t, 8>& GetUserData() { return m_userData; }
|
||||
|
||||
[[nodiscard]] float GetPixelScaleFactor() const { return m_perPixelScaleFactor; }
|
||||
|
||||
[[nodiscard]] virtual bool IsPerspective() const { return false; }
|
||||
[[nodiscard]] virtual bool IsOrtho() const { return false; }
|
||||
};
|
||||
|
||||
class PerspectiveCamera : public Camera
|
||||
@@ -250,6 +253,8 @@ namespace OpenVulkano::Scene
|
||||
m_projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane);
|
||||
UpdateViewProjectionMatrix();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool IsPerspective() const override { return true; }
|
||||
};
|
||||
|
||||
class OrthographicCamera : public Camera
|
||||
@@ -262,6 +267,8 @@ namespace OpenVulkano::Scene
|
||||
m_projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
|
||||
UpdateViewProjectionMatrix();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool IsOrtho() const override { return true; }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -29,5 +29,8 @@ namespace OpenVulkano::Scene
|
||||
}
|
||||
|
||||
void UpdateProjectionMatrix() override;
|
||||
|
||||
[[nodiscard]] bool IsPerspective() const override { return m_morphState == 0; }
|
||||
[[nodiscard]] bool IsOrtho() const override { return m_morphState == 1; }
|
||||
};
|
||||
}
|
||||
@@ -30,7 +30,7 @@ namespace OpenVulkano::Scene
|
||||
void Tick() override;
|
||||
|
||||
void SetDuration(double duration) { m_animationDuration = duration; }
|
||||
double GetDuration() { return m_animationDuration; }
|
||||
[[nodiscard]] double GetDuration() { return m_animationDuration; }
|
||||
void SetTargetState(bool toPerspective);
|
||||
void Reset() { m_currentTime = 0; }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user