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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user