Added orbit camera mode, set up orbit in World Editor and Model Editor

This commit is contained in:
parax0 2015-11-26 00:47:02 -07:00
parent 373426a98f
commit 2e5678e863
11 changed files with 284 additions and 192 deletions

View File

@ -12,81 +12,63 @@ CCamera::CCamera()
mYaw = -Math::skHalfPi;
mPitch = 0.0f;
CalculateDirection();
SetOrbit(CVector3f(0), 5.f);
Update();
mMoveSpeed = 1.f; // Old: 0.01f
mLookSpeed = 1.f; // Old: 0.003f
mMoveSpeed = 1.f;
mLookSpeed = 1.f;
mViewOutdated = true;
mProjectionOutdated = true;
mFrustumPlanesOutdated = true;
}
CCamera::CCamera(CVector3f Position, CVector3f)
CCamera::CCamera(CVector3f Position, CVector3f /*Target*/)
{
// todo: make it actually look at the target!
// Not using parameter 2 (CVector3f - Target)
mMode = eFreeCamera;
mMoveSpeed = 1.f; // Old: 0.01f
mLookSpeed = 1.f; // Old: 0.003f
mMoveSpeed = 1.f;
mLookSpeed = 1.f;
mPosition = Position;
mYaw = -Math::skHalfPi;
mPitch = 0.0f;
CalculateDirection();
Update();
}
void CCamera::Pan(float XAmount, float YAmount)
{
switch (mMode)
if (mMode == eFreeCamera)
{
case eFreeCamera:
{
CVector3f Right(
cos(mYaw - Math::skHalfPi),
sin(mYaw - Math::skHalfPi),
0
);
CVector3f Up = Right.Cross(mDirection);
mPosition += Right * (XAmount * mMoveSpeed);
mPosition += Up * (YAmount * mMoveSpeed);
Update();
mPosition += mRightVector * (XAmount * mMoveSpeed);
mPosition += mUpVector * (YAmount * mMoveSpeed);
mViewOutdated = true;
mFrustumPlanesOutdated = true;
break;
}
// Unfinished
case eOrbitCamera:
{
CVector3f Right(
cos(mYaw - Math::skHalfPi),
sin(mYaw - Math::skHalfPi),
0
);
CVector3f Up = Right.Cross(mDirection);
CVector3f TargetDirection = mPosition - mOrbitTarget;
//CMatrix4f YawRotation = CQuaternion::F
}
}
else
Rotate(-XAmount * 0.3f, YAmount * 0.3f);
}
void CCamera::Rotate(float XAmount, float YAmount)
{
switch (mMode)
{
case eFreeCamera:
mYaw -= (XAmount * mLookSpeed * 0.3f);
mPitch -= (YAmount * mLookSpeed * 0.3f);
mViewOutdated = true;
mFrustumPlanesOutdated = true;
break;
}
}
void CCamera::Zoom(float Amount)
{
if (mMode == eFreeCamera)
{
Update();
mPosition += (mDirection * Amount) * (mMoveSpeed * 25.f);
}
else
mOrbitDistance -= Amount * mMoveSpeed * 25.f;
mViewOutdated = true;
mFrustumPlanesOutdated = true;
}
@ -98,21 +80,19 @@ void CCamera::Snap(CVector3f Position)
mPitch = 0.0f;
mViewOutdated = true;
mFrustumPlanesOutdated = true;
CalculateDirection();
Update();
}
void CCamera::ProcessKeyInput(EKeyInputs KeyFlags, double DeltaTime)
{
float FDeltaTime = (float) DeltaTime;
if (mMode == eFreeCamera)
{
if (KeyFlags & eWKey) Zoom(FDeltaTime);
if (KeyFlags & eSKey) Zoom(-FDeltaTime);
if (KeyFlags & eQKey) Pan(0, -FDeltaTime * 25.f);
if (KeyFlags & eEKey) Pan(0, FDeltaTime * 25.f);
if (KeyFlags & eAKey) Pan(-FDeltaTime * 25.f, 0);
if (KeyFlags & eDKey) Pan(FDeltaTime * 25.f, 0);
}
}
void CCamera::ProcessMouseInput(EKeyInputs KeyFlags, EMouseInputs MouseFlags, float XMovement, float YMovement)
@ -132,14 +112,9 @@ void CCamera::ProcessMouseInput(EKeyInputs KeyFlags, EMouseInputs MouseFlags, fl
// Orbit Camera
else if (mMode == eOrbitCamera)
{
if ((MouseFlags & eMiddleButton) && (KeyFlags & eCtrlKey))
Zoom(-YMovement * 0.2f);
else if ((MouseFlags & eMiddleButton) || (MouseFlags & eRightButton))
Pan(XMovement, YMovement);
if ((MouseFlags & eMiddleButton) || (MouseFlags & eRightButton))
Pan(-XMovement, YMovement);
}
CalculateDirection();
}
CRay CCamera::CastRay(CVector2f DeviceCoords)
@ -156,6 +131,52 @@ CRay CCamera::CastRay(CVector2f DeviceCoords)
return Ray;
}
void CCamera::SetMoveMode(ECameraMoveMode Mode)
{
mMode = Mode;
mViewOutdated = true;
mFrustumPlanesOutdated = true;
}
void CCamera::SetOrbit(const CVector3f& OrbitTarget, float Distance)
{
mOrbitTarget = OrbitTarget;
mOrbitDistance = Distance;
if (mMode == eOrbitCamera)
{
mViewOutdated = true;
mFrustumPlanesOutdated = true;
}
}
void CCamera::SetOrbit(const CAABox& OrbitTarget, float DistScale /*= 2.5f*/)
{
CVector3f Min = OrbitTarget.Min();
CVector3f Max = OrbitTarget.Max();
mOrbitTarget = OrbitTarget.Center();
mOrbitDistance = ((Max.x - Min.x) + (Max.y - Min.y) + (Max.z - Min.z)) / 3.f;
mOrbitDistance *= DistScale;
if (mMode == eOrbitCamera)
{
mViewOutdated = true;
mFrustumPlanesOutdated = true;
}
}
void CCamera::SetOrbitDistance(float Distance)
{
mOrbitDistance = Distance;
if (mMode == eOrbitCamera)
{
mViewOutdated = true;
mFrustumPlanesOutdated = true;
}
}
void CCamera::LoadMatrices()
{
CGraphics::sMVPBlock.ViewMatrix = ViewMatrix();
@ -199,10 +220,15 @@ float CCamera::FieldOfView() const
return 55.f;
}
ECameraMoveMode CCamera::MoveMode() const
{
return mMode;
}
const CMatrix4f& CCamera::ViewMatrix()
{
if (mViewOutdated)
CalculateView();
UpdateView();
return mCachedViewMatrix;
}
@ -210,7 +236,7 @@ const CMatrix4f& CCamera::ViewMatrix()
const CMatrix4f& CCamera::ProjectionMatrix()
{
if (mProjectionOutdated)
CalculateProjection();
UpdateProjection();
return mCachedProjectionMatrix;
}
@ -218,7 +244,7 @@ const CMatrix4f& CCamera::ProjectionMatrix()
const CFrustumPlanes& CCamera::FrustumPlanes()
{
if (mFrustumPlanesOutdated)
CalculateFrustumPlanes();
UpdateFrustum();
return mCachedFrustumPlanes;
}
@ -258,17 +284,6 @@ void CCamera::SetLookSpeed(float LookSpeed)
mLookSpeed = LookSpeed;
}
void CCamera::SetFree()
{
mMode = eFreeCamera;
}
void CCamera::SetOrbit(const CVector3f& OrbitTarget)
{
mMode = eOrbitCamera;
mOrbitTarget = OrbitTarget;
}
void CCamera::SetAspectRatio(float AspectRatio)
{
mAspectRatio = AspectRatio;
@ -277,8 +292,9 @@ void CCamera::SetAspectRatio(float AspectRatio)
}
// ************ PRIVATE ************
void CCamera::CalculateDirection()
void CCamera::Update()
{
// Update direction
if (mPitch > Math::skHalfPi) mPitch = Math::skHalfPi;
if (mPitch < -Math::skHalfPi) mPitch = -Math::skHalfPi;
@ -295,12 +311,22 @@ void CCamera::CalculateDirection()
);
mUpVector = mRightVector.Cross(mDirection);
// Update position
if (mMode == eOrbitCamera)
{
if (mOrbitDistance < 1.f) mOrbitDistance = 1.f;
mPosition = mOrbitTarget + (mDirection * -mOrbitDistance);
}
mViewOutdated = true;
mFrustumPlanesOutdated = true;
}
void CCamera::CalculateView()
void CCamera::UpdateView()
{
// todo: don't use glm
CalculateDirection();
Update();
glm::vec3 glmpos(mPosition.x, mPosition.y, mPosition.z);
glm::vec3 glmdir(mDirection.x, mDirection.y, mDirection.z);
@ -309,13 +335,13 @@ void CCamera::CalculateView()
mViewOutdated = false;
}
void CCamera::CalculateProjection()
void CCamera::UpdateProjection()
{
mCachedProjectionMatrix = Math::PerspectiveMatrix(55.f, mAspectRatio, 0.1f, 4096.f);
mProjectionOutdated = false;
}
void CCamera::CalculateFrustumPlanes()
void CCamera::UpdateFrustum()
{
mCachedFrustumPlanes.SetPlanes(mPosition, mDirection, 55.f, mAspectRatio, 0.1f, 4096.f);
mFrustumPlanesOutdated = false;

View File

@ -28,6 +28,7 @@ class CCamera
float mYaw;
float mPitch;
CVector3f mOrbitTarget;
float mOrbitDistance;
float mMoveSpeed;
float mLookSpeed;
@ -51,6 +52,11 @@ public:
CRay CastRay(CVector2f DeviceCoords);
void LoadMatrices();
void SetMoveMode(ECameraMoveMode Mode);
void SetOrbit(const CVector3f& OrbitTarget, float Distance);
void SetOrbit(const CAABox& OrbitTarget, float DistScale = 2.5f);
void SetOrbitDistance(float Distance);
// Getters
CVector3f Position() const;
CVector3f Direction() const;
@ -59,6 +65,7 @@ public:
float Yaw() const;
float Pitch() const;
float FieldOfView() const;
ECameraMoveMode MoveMode() const;
const CMatrix4f& ViewMatrix();
const CMatrix4f& ProjectionMatrix();
const CFrustumPlanes& FrustumPlanes();
@ -70,17 +77,14 @@ public:
void SetPitch(float Pitch);
void SetMoveSpeed(float MoveSpeed);
void SetLookSpeed(float LookSpeed);
void SetFree();
void SetOrbit(const CVector3f& OrbitTarget);
void SetOrbit(const CAABox& OrbitTarget);
void SetAspectRatio(float AspectRatio);
// Private
private:
void CalculateDirection();
void CalculateView();
void CalculateProjection();
void CalculateFrustumPlanes();
void Update();
void UpdateView();
void UpdateProjection();
void UpdateFrustum();
};
#endif // CCAMERA_H

View File

@ -47,8 +47,8 @@ public:
void mouseReleaseEvent(QMouseEvent *pEvent);
void mouseMoveEvent(QMouseEvent *pEvent);
void wheelEvent(QWheelEvent *pEvent);
void keyPressEvent(QKeyEvent *pEvent);
void keyReleaseEvent(QKeyEvent *pEvent);
virtual void keyPressEvent(QKeyEvent *pEvent);
virtual void keyReleaseEvent(QKeyEvent *pEvent);
void focusOutEvent(QFocusEvent *pEvent);
void contextMenuEvent(QContextMenuEvent *pEvent);

View File

@ -39,7 +39,8 @@ CModelEditorWindow::CModelEditorWindow(QWidget *parent) :
CCamera& camera = ui->Viewport->Camera();
camera.Snap(CVector3f(0, 3, 1));
camera.SetFree();
camera.SetMoveMode(eOrbitCamera);
camera.SetOrbit(CVector3f(0, 0, 1), 3.f);
camera.SetMoveSpeed(0.5f);
// UI initialization
@ -153,6 +154,7 @@ void CModelEditorWindow::SetActiveModel(CModel *pModel)
mpCurrentModelNode->MarkTransformChanged();
mpCurrentModel = pModel;
mModelToken = CToken(pModel);
ui->Viewport->Camera().SetOrbit(pModel->AABox());
u32 numVertices = (pModel ? pModel->GetVertexCount() : 0);
u32 numTriangles = (pModel ? pModel->GetTriangleCount() : 0);
@ -807,3 +809,26 @@ void CModelEditorWindow::on_actionSave_as_triggered()
TString name = TString(filename.toStdString());
setWindowTitle("Prime World Editor - Model Editor: " + TO_QSTRING(name));
}
void CModelEditorWindow::on_CameraModeButton_clicked()
{
CCamera *pCam = &ui->Viewport->Camera();
if (pCam->MoveMode() == eOrbitCamera)
{
pCam->SetMoveMode(eFreeCamera);
ui->CameraModeButton->setIcon(QIcon(":/icons/EditorAssets/Free Camera.png"));
ui->CameraModeButton->setToolTip(QString("Free Camera"));
}
else if (pCam->MoveMode() == eFreeCamera)
{
pCam->SetMoveMode(eOrbitCamera);
ui->CameraModeButton->setIcon(QIcon(":/icons/EditorAssets/Orbit Camera v2.png"));
ui->CameraModeButton->setToolTip(QString("Orbit Camera"));
CVector3f Pos = pCam->Position();
CVector3f Target = mpCurrentModelNode->AABox().Center();
pCam->SetOrbitDistance(Pos.Distance(Target));
}
}

View File

@ -115,6 +115,8 @@ private slots:
void on_actionSave_as_triggered();
void on_CameraModeButton_clicked();
signals:
void Closed();
};

View File

@ -2140,7 +2140,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,0,0,0,0,0,0,0,0">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,0,0,0,0,0,0,0,0,0">
<property name="spacing">
<number>50</number>
</property>
@ -2227,6 +2227,32 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="CameraModeButton">
<property name="toolTip">
<string>Orbit Camera</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Icons.qrc">
<normaloff>:/icons/EditorAssets/Orbit Camera.png</normaloff>:/icons/EditorAssets/Orbit Camera.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">

View File

@ -109,6 +109,26 @@ bool CSceneViewport::IsHoveringGizmo()
return mGizmoHovering;
}
void CSceneViewport::keyPressEvent(QKeyEvent* pEvent)
{
CBasicViewport::keyPressEvent(pEvent);
if (pEvent->key() == Qt::Key_Z)
{
mCamera.SetMoveMode(eOrbitCamera);
}
}
void CSceneViewport::keyReleaseEvent(QKeyEvent* pEvent)
{
CBasicViewport::keyReleaseEvent(pEvent);
if (pEvent->key() == Qt::Key_Z)
{
mCamera.SetMoveMode(eFreeCamera);
}
}
// ************ PROTECTED SLOTS ************
void CSceneViewport::CheckUserInput()
{

View File

@ -34,6 +34,9 @@ public:
void ResetHover();
bool IsHoveringGizmo();
void keyPressEvent(QKeyEvent* pEvent);
void keyReleaseEvent(QKeyEvent* pEvent);
signals:
void GizmoMoved();

View File

@ -95,6 +95,7 @@ void CWorldEditor::SetArea(CWorld *pWorld, CGameArea *pArea)
ui->ModifyTabContents->ClearCachedEditors();
ui->InstancesTabContents->SetMaster(nullptr);
ui->InstancesTabContents->SetArea(pArea);
mUndoStack.clear();
// Clear old area - hack until better world/area loader is implemented
if ((mpArea) && (pArea != mpArea))
@ -109,10 +110,17 @@ void CWorldEditor::SetArea(CWorld *pWorld, CGameArea *pArea)
mScene.SetActiveWorld(pWorld);
mScene.SetActiveArea(pArea);
// Snap camera to location of area
// Snap camera to new area
CCamera *pCamera = &ui->MainViewport->Camera();
if (pCamera->MoveMode() == eFreeCamera)
{
CTransform4f AreaTransform = pArea->GetTransform();
CVector3f AreaPosition(AreaTransform[0][3], AreaTransform[1][3], AreaTransform[2][3]);
ui->MainViewport->Camera().Snap(AreaPosition);
pCamera->Snap(AreaPosition);
}
UpdateCameraOrbit();
// Default bloom to Fake Bloom for Metroid Prime 3; disable for other games
if (mpWorld->Version() == eCorruption)
@ -137,63 +145,7 @@ CGameArea* CWorldEditor::ActiveArea()
return mpArea;
}
// ************ SLOTS ************
void CWorldEditor::UpdateCursor()
{
if (ui->MainViewport->IsCursorVisible())
{
CSceneNode *pHoverNode = ui->MainViewport->HoverNode();
if (ui->MainViewport->IsHoveringGizmo())
ui->MainViewport->SetCursorState(Qt::SizeAllCursor);
else if ((pHoverNode) && (pHoverNode->NodeType() != eStaticNode))
ui->MainViewport->SetCursorState(Qt::PointingHandCursor);
else
ui->MainViewport->SetCursorState(Qt::ArrowCursor);
}
}
void CWorldEditor::UpdateStatusBar()
{
// Would be cool to do more frequent status bar updates with more info. Unfortunately, this causes lag.
QString StatusText = "";
if (!mGizmoHovering)
{
if (ui->MainViewport->underMouse())
{
CSceneNode *pHoverNode = ui->MainViewport->HoverNode();
if (pHoverNode && (pHoverNode->NodeType() != eStaticNode))
StatusText = TO_QSTRING(pHoverNode->Name());
}
}
if (ui->statusbar->currentMessage() != StatusText)
ui->statusbar->showMessage(StatusText);
}
void CWorldEditor::UpdateSelectionUI()
{
// Update sidebar
ui->ModifyTabContents->GenerateUI(mSelection);
// Update selection info text
QString SelectionText;
if (mSelection.size() == 1)
SelectionText = TO_QSTRING(mSelection.front()->Name());
else if (mSelection.size() > 1)
SelectionText = QString("%1 objects selected").arg(mSelection.size());
QFontMetrics Metrics(ui->SelectionInfoLabel->font());
SelectionText = Metrics.elidedText(SelectionText, Qt::ElideRight, ui->SelectionInfoFrame->width() - 10);
ui->SelectionInfoLabel->setText(SelectionText);
// Update gizmo stuff
UpdateGizmoUI();
}
// ************ UPDATE UI ************
void CWorldEditor::UpdateGizmoUI()
{
// Update transform XYZ spin boxes
@ -248,13 +200,83 @@ void CWorldEditor::UpdateGizmoUI()
}
}
void CWorldEditor::UpdateSelectionUI()
{
// Update camera orbit
UpdateCameraOrbit();
// Update sidebar
ui->ModifyTabContents->GenerateUI(mSelection);
// Update selection info text
QString SelectionText;
if (mSelection.size() == 1)
SelectionText = TO_QSTRING(mSelection.front()->Name());
else if (mSelection.size() > 1)
SelectionText = QString("%1 objects selected").arg(mSelection.size());
QFontMetrics Metrics(ui->SelectionInfoLabel->font());
SelectionText = Metrics.elidedText(SelectionText, Qt::ElideRight, ui->SelectionInfoFrame->width() - 10);
ui->SelectionInfoLabel->setText(SelectionText);
// Update gizmo stuff
UpdateGizmoUI();
}
void CWorldEditor::UpdateStatusBar()
{
// Would be cool to do more frequent status bar updates with more info. Unfortunately, this causes lag.
QString StatusText = "";
if (!mGizmoHovering)
{
if (ui->MainViewport->underMouse())
{
CSceneNode *pHoverNode = ui->MainViewport->HoverNode();
if (pHoverNode && (pHoverNode->NodeType() != eStaticNode))
StatusText = TO_QSTRING(pHoverNode->Name());
}
}
if (ui->statusbar->currentMessage() != StatusText)
ui->statusbar->showMessage(StatusText);
}
// ************ PROTECTED ************
void CWorldEditor::GizmoModeChanged(CGizmo::EGizmoMode mode)
{
ui->TransformSpinBox->SetSingleStep( (mode == CGizmo::eRotate ? 1.0 : 0.1) );
ui->TransformSpinBox->SetDefaultValue( (mode == CGizmo::eScale ? 1.0 : 0.0) );
}
// ************ ACTIONS ************
void CWorldEditor::UpdateCursor()
{
if (ui->MainViewport->IsCursorVisible())
{
CSceneNode *pHoverNode = ui->MainViewport->HoverNode();
if (ui->MainViewport->IsHoveringGizmo())
ui->MainViewport->SetCursorState(Qt::SizeAllCursor);
else if ((pHoverNode) && (pHoverNode->NodeType() != eStaticNode))
ui->MainViewport->SetCursorState(Qt::PointingHandCursor);
else
ui->MainViewport->SetCursorState(Qt::ArrowCursor);
}
}
void CWorldEditor::UpdateCameraOrbit()
{
CCamera *pCamera = &ui->MainViewport->Camera();
if (!mSelection.isEmpty())
pCamera->SetOrbit(mSelectionBounds);
else
pCamera->SetOrbit(mpArea->AABox(), 0.8f);
}
// ************ PRIVATE SLOTS ************
void CWorldEditor::RefreshViewport()
{
if (!mGizmo.IsTransforming())
@ -413,37 +435,6 @@ void CWorldEditor::on_ActionBloom_triggered()
ui->ActionBloom->setChecked(true);
}
void CWorldEditor::on_ActionZoomOnSelection_triggered()
{
static const float skDistScale = 2.5f;
static const float skAreaDistScale = 0.8f;
CCamera& Camera = ui->MainViewport->Camera();
CVector3f CamDir = Camera.Direction();
CVector3f NewPos;
// Zoom on selection
if (mSelection.size() != 0)
{
CVector3f Min = mSelectionBounds.Min();
CVector3f Max = mSelectionBounds.Max();
float Dist = ((Max.x - Min.x) + (Max.y - Min.y) + (Max.z - Min.z)) / 3.f;
NewPos = mSelectionBounds.Center() + (CamDir * -(Dist * skDistScale));
}
// Zoom on area
else
{
CAABox AreaBox = mpArea->AABox();
CVector3f Min = AreaBox.Min();
CVector3f Max = AreaBox.Max();
float Dist = ((Max.x - Min.x) + (Max.y - Min.y) + (Max.z - Min.z)) / 3.f;
NewPos = AreaBox.Center() + (CamDir * -(Dist * skAreaDistScale));
}
Camera.SetPosition(NewPos);
}
void CWorldEditor::on_ActionDisableBackfaceCull_triggered()
{
ui->MainViewport->Renderer()->ToggleBackfaceCull(!ui->ActionDisableBackfaceCull->isChecked());

View File

@ -50,10 +50,8 @@ public:
protected:
void GizmoModeChanged(CGizmo::EGizmoMode mode);
private:
void UpdateCursor();
void OnSidebarResize();
void UpdateCameraOrbit();
private slots:
void RefreshViewport();
@ -72,7 +70,6 @@ private slots:
void on_ActionBloomMaps_triggered();
void on_ActionFakeBloom_triggered();
void on_ActionBloom_triggered();
void on_ActionZoomOnSelection_triggered();
void on_ActionDisableBackfaceCull_triggered();
void on_ActionDisableAlpha_triggered();
void on_ActionEditLayers_triggered();

View File

@ -267,8 +267,6 @@
<addaction name="ActionFakeBloom"/>
<addaction name="ActionBloom"/>
</widget>
<addaction name="ActionZoomOnSelection"/>
<addaction name="separator"/>
<addaction name="ActionDrawWorld"/>
<addaction name="ActionDrawObjects"/>
<addaction name="ActionDrawCollision"/>