Skip to content

Commit

Permalink
camera refact
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw committed Nov 15, 2023
1 parent 69e046a commit 72371a6
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 126 deletions.
31 changes: 31 additions & 0 deletions Documents/EditorUserGuide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Editor User Guide

Document will use some short names to keep clean and tidy:

* Mouse Left Button -> MLB

* Mouse Middle Button -> MMB

* Mouse Right Button -> MRB



Viewport is a generic name for different kinds of views, including SceneView, GameView...

Here is viewport camera operations in the Editor.

* Zoom

* Scroll mouse wheel vertically

* Keep pressing MLB and move mouse vertically

* FOV Zoom

* Not support : user case?

* Panning

* Keep pressing MMB and move.

* Keep pressing MLB and MRB at the same time and move.
18 changes: 0 additions & 18 deletions Documents/TerrainUserGuide.md

This file was deleted.

104 changes: 59 additions & 45 deletions Engine/Source/Editor/Camera/ViewportCameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,11 @@
namespace engine
{

ViewportCameraController::ViewportCameraController(
const SceneWorld* pSceneWorld,
float sensitivity,
float movement_speed)
: ViewportCameraController(pSceneWorld, sensitivity, sensitivity, movement_speed)
{
}

ViewportCameraController::ViewportCameraController(
const SceneWorld* pSceneWorld,
float horizontal_sensitivity,
float vertical_sensitivity,
float movement_speed)
: m_pSceneWorld(pSceneWorld)
, m_horizontalSensitivity(horizontal_sensitivity)
, m_verticalSensitivity(vertical_sensitivity)
, m_movementSpeed(movement_speed)
, m_initialMovemenSpeed(movement_speed)
ViewportCameraController::ViewportCameraController(const SceneWorld* pSceneWorld, float horizontal_sensitivity, float vertical_sensitivity, float movement_speed) :
m_pSceneWorld(pSceneWorld),
m_horizontalSensitivity(horizontal_sensitivity),
m_verticalSensitivity(vertical_sensitivity),
m_movementSpeed(movement_speed)
{
assert(pSceneWorld);
}
Expand All @@ -53,14 +40,18 @@ bool ViewportCameraController::IsZooming() const

bool ViewportCameraController::IsPanning() const
{
return ImGui::IsMouseDown(ImGuiMouseButton_Middle);
if (ImGui::IsMouseDown(ImGuiMouseButton_Middle))
{
return !ImGui::IsMouseDown(ImGuiMouseButton_Left) && !ImGui::IsMouseDown(ImGuiMouseButton_Right);
}

return ImGui::IsMouseDown(ImGuiMouseButton_Left) && ImGui::IsMouseDown(ImGuiMouseButton_Right);
}

bool ViewportCameraController::IsTurning() const
{
if (ImGui::IsMouseDown(ImGuiMouseButton_Left))
{
// Offset Y is used to zoom in/out when left mouse button down.
return Input::Get().GetMousePositionOffsetX() != 0.0f;
}

Expand Down Expand Up @@ -100,11 +91,28 @@ void ViewportCameraController::OnMouseUp()
void ViewportCameraController::OnMouseMove(float x, float y)
{
bool dirty = false;
if (IsZooming())
do
{
Zoom(y);
dirty = true;
}
if (IsPanning())
{
Panning(x, y);
dirty = true;
// While panning, should not do other opertions at the same time.
break;
}

if (IsZooming())
{
Zoom(-y);
dirty = true;
}

if (IsTurning())
{
Turning(x, y);
dirty = true;
}
} while (false);

if (dirty)
{
Expand All @@ -127,6 +135,24 @@ void ViewportCameraController::OnMouseWheel(float y)
}
}

void ViewportCameraController::Zoom(float delta)
{
float scaleDelta = delta * m_movementSpeed;
m_distanceFromLookAt -= scaleDelta;
m_eye = m_eye + m_lookAt * scaleDelta;
}

void ViewportCameraController::Panning(float x, float y)
{
MoveLeft(x);
m_eye = m_eye + m_up * y;
}

void ViewportCameraController::Turning(float x, float y)
{

}

void ViewportCameraController::Update(float deltaTime)
{
if (IsInAnimation())
Expand All @@ -135,6 +161,11 @@ void ViewportCameraController::Update(float deltaTime)
return;
}

if (IsInControl())
{
CameraToController();
}

float offsetX = static_cast<float>(Input::Get().GetMousePositionOffsetX());
float offsetY = static_cast<float>(Input::Get().GetMousePositionOffsetY());
if (offsetX != 0.0f || offsetY != 0.0f)
Expand Down Expand Up @@ -248,8 +279,8 @@ void ViewportCameraController::CameraToController()
void ViewportCameraController::ControllerToCamera()
{
cd::Vec3f eye = m_eye;
cd::Vec3f lookAt = m_lookAt;
cd::Vec3f up = m_up;
cd::Vec3f lookAt = m_lookAt.Normalize();
cd::Vec3f up = m_up.Normalize();

if (m_isTracking)
{
Expand Down Expand Up @@ -282,9 +313,10 @@ void ViewportCameraController::ControllerToCamera()
TransformComponent* pTransformComponent = GetMainCameraTransformComponent();
pTransformComponent->GetTransform().SetTranslation(eye);

cd::Vec3f rotationAxis = cd::Vec3f(0.0f, 0.0f,1.0f).Cross(lookAt).Normalize();
cd::Vec3f rotationAxis = cd::Vec3f(0.0f, 0.0f, 1.0f).Cross(lookAt);
float rotationAngle = std::acos(cd::Vec3f(0.0f, 0.0f, 1.0f).Dot(lookAt));
pTransformComponent->GetTransform().SetRotation(cd::Quaternion::FromAxisAngle(rotationAxis, rotationAngle));
pTransformComponent->Dirty();
pTransformComponent->Build();
}

Expand Down Expand Up @@ -327,7 +359,6 @@ const cd::Transform& ViewportCameraController::GetMainCameraTransform()
void ViewportCameraController::MoveForward(float amount)
{
m_eye = m_eye + m_lookAt * amount;
ControllerToCamera();
}

void ViewportCameraController::MoveBackward(float amount)
Expand All @@ -338,7 +369,6 @@ void ViewportCameraController::MoveBackward(float amount)
void ViewportCameraController::MoveLeft(float amount)
{
m_eye = m_eye + m_lookAt.Cross(m_up) * amount;
ControllerToCamera();
}

void ViewportCameraController::MoveRight(float amount)
Expand All @@ -349,7 +379,6 @@ void ViewportCameraController::MoveRight(float amount)
void ViewportCameraController::MoveUp(float amount)
{
m_eye = m_eye + cd::Vec3f(0.0f, 1.0f, 0.0f) * amount;
ControllerToCamera();
}

void ViewportCameraController::MoveDown(float amount)
Expand Down Expand Up @@ -411,7 +440,6 @@ void ViewportCameraController::ElevationChanging(float angleDegrees)
{
m_elevation += cd::Math::TWO_PI;
}
ControllerToCamera();
}

void ViewportCameraController::AzimuthChanging(float angleDegrees)
Expand All @@ -425,7 +453,6 @@ void ViewportCameraController::AzimuthChanging(float angleDegrees)
{
m_azimuth += cd::Math::TWO_PI;
}
ControllerToCamera();
}

void ViewportCameraController::SynchronizeTrackingCamera()
Expand Down Expand Up @@ -497,19 +524,6 @@ void ViewportCameraController::Focusing()
}
}

void ViewportCameraController::Zoom(float delta)
{
float scaleDelta = delta * m_movementSpeed;
m_distanceFromLookAt -= scaleDelta;
m_eye = m_eye + m_lookAt * scaleDelta;
}

void ViewportCameraController::Panning(float x, float y)
{
MoveLeft(x);
m_eye = m_eye + m_up * y;
}

void ViewportCameraController::MoveToPosition(cd::Point position, cd::Vec3f rotation)
{
m_isFocusing = true;
Expand Down
25 changes: 10 additions & 15 deletions Engine/Source/Editor/Camera/ViewportCameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ class ViewportCameraController : public ICameraController
{
public:
ViewportCameraController() = delete;
explicit ViewportCameraController(const SceneWorld* pSceneWorld, float sensitivity, float movement_speed);
explicit ViewportCameraController(const SceneWorld* pSceneWorld, float horizontal_sensitivity, float vertical_sensitivity, float movement_speed);
~ViewportCameraController() = default;

ViewportCameraController(const ViewportCameraController&) = delete;
ViewportCameraController(ViewportCameraController&&) = delete;
ViewportCameraController& operator=(const ViewportCameraController&) = delete;
ViewportCameraController& operator=(ViewportCameraController&&) = delete;
virtual ~ViewportCameraController() = default;

// Operations
virtual bool IsInAnimation() const override;
Expand All @@ -41,18 +39,14 @@ class ViewportCameraController : public ICameraController
// TODO : EventDriven, not update.
void Update(float deltaTime);

// Synchronizes the controller to the transform of camera current state
void CameraToController();

// Synchronizes the transform of camera to the controller's current state
void ControllerToCamera();

// Implement the effect of a translation animation.
void CameraFocus();
void Focusing();

//
void Zoom(float delta);
void Panning(float x, float y);
void Turning(float x, float y);

// Configs
void SetMovementSpeed(float speed);
Expand All @@ -76,14 +70,10 @@ class ViewportCameraController : public ICameraController
void PitchLocal(float angleDegrees);
void RollLocal(float angleDegrees);

void Panning(float x, float y);

// Circling Operations
void AzimuthChanging(float amount);
void ElevationChanging(float amount);



// Synchronize view when begin using mayastyle camera or fpscamera
void SynchronizeTrackingCamera();

Expand All @@ -95,6 +85,12 @@ class ViewportCameraController : public ICameraController
const cd::Transform& GetMainCameraTransform();
float CalculateZoomScale() { return std::max(std::abs(m_distanceFromLookAt), m_dollyThreshold); }

// Synchronizes the controller to the transform of camera current state
void CameraToController();

// Synchronizes the transform of camera to the controller's current state
void ControllerToCamera();

private:
const SceneWorld* m_pSceneWorld;

Expand All @@ -106,7 +102,6 @@ class ViewportCameraController : public ICameraController
float m_horizontalSensitivity = 0.0f;
float m_verticalSensitivity = 0.0f;
float m_movementSpeed = 0.0f;
float m_initialMovemenSpeed = 0.0f;
float m_mouseScroll = 0.0f;

cd::Vec3f m_lookAtPoint = cd::Vec3f::Zero();
Expand Down
21 changes: 7 additions & 14 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void EditorApp::InitEditorImGuiContext(engine::Language language)

void EditorApp::InitEditorUILayers()
{
InitEditorController();
InitCameraController();

// Add UI layers after finish imgui and rendering contexts' initialization.
auto pMainMenu = std::make_unique<MainMenu>("MainMenu");
Expand Down Expand Up @@ -562,14 +562,10 @@ void EditorApp::InitShaderPrograms(bool compileAllShaders) const
}
}

void EditorApp::InitEditorController()
void EditorApp::InitCameraController()
{
// Controller for Input events.
m_pViewportCameraController = std::make_unique<engine::ViewportCameraController>(
m_pSceneWorld.get(),
12.0f /* horizontal sensitivity */,
12.0f /* vertical sensitivity */);
m_pViewportCameraController->CameraToController();
m_pViewportCameraController = std::make_unique<engine::ViewportCameraController>(m_pSceneWorld.get(),
12.0f /* horizontal sensitivity */, 12.0f /* vertical sensitivity */, 5.0f /* move speed */);
}

void EditorApp::AddEditorRenderer(std::unique_ptr<engine::Renderer> pRenderer)
Expand Down Expand Up @@ -620,6 +616,9 @@ bool EditorApp::Update(float deltaTime)
InitEngineUILayers();
}

// Input
engine::Input::Get().Reset();

// Window
m_pWindowManager->Update();
auto mainWindowPos = m_pMainWindow->GetPosition();
Expand All @@ -629,9 +628,6 @@ bool EditorApp::Update(float deltaTime)
// World Data
m_pSceneWorld->Update();

// Input
engine::Input::Get().Update();

// Update Editor GUI
m_pEditorImGuiContext->SetRectPosition(mainWindowX, mainWindowY);
m_pEditorImGuiContext->BeginFrame();
Expand Down Expand Up @@ -689,9 +685,6 @@ bool EditorApp::Update(float deltaTime)
}
m_pRenderContext->EndFrame();

// Reset input data.
engine::Input::Get().FlushInputs();

return !GetMainWindow()->ShouldClose();
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class EditorApp final : public engine::IApplication
void InitECWorld();

// Misc
void InitEditorController();
void InitCameraController();
bool IsAtmosphericScatteringEnable() const;

private:
Expand Down
Loading

0 comments on commit 72371a6

Please sign in to comment.