From 89c1654b6a52e53134279a88561c41a1ea912c5e Mon Sep 17 00:00:00 2001 From: parax0 Date: Tue, 1 Sep 2015 19:21:10 -0400 Subject: [PATCH] Set up CBasicViewport to draw an XYZ axis overlay in the corner --- Common/Math.cpp | 5 +++++ Common/Math.h | 2 ++ Core/CCamera.cpp | 26 ++++++++++++-------------- Core/CCamera.h | 2 +- Core/CDrawUtil.cpp | 4 ---- Scene/CScriptNode.cpp | 3 +++ UI/CBasicViewport.cpp | 12 +++++++----- UI/CBasicViewport.h | 14 ++++++++------ UI/CModelEditorViewport.h | 1 + UI/CWorldEditor.cpp | 3 ++- 10 files changed, 41 insertions(+), 31 deletions(-) diff --git a/Common/Math.cpp b/Common/Math.cpp index 3d2447d5..bf46c34c 100644 --- a/Common/Math.cpp +++ b/Common/Math.cpp @@ -344,4 +344,9 @@ CMatrix4f PerspectiveMatrix(float fov, float aspect, float near, float far) return CMatrix4f::FromGlmMat4(glm::perspective(fov, aspect, near, far)).Transpose(); } +CMatrix4f OrthographicMatrix(float left, float right, float bottom, float top, float near, float far) +{ + return CMatrix4f::FromGlmMat4(glm::ortho(left, right, bottom, top, near, far)).Transpose(); +} + } // End namespace diff --git a/Common/Math.h b/Common/Math.h index 765a5433..6d8ff97e 100644 --- a/Common/Math.h +++ b/Common/Math.h @@ -34,6 +34,8 @@ std::pair RayTriangleIntersection(const CRay& Ray, const CVector3f& CMatrix4f PerspectiveMatrix(float fov, float aspect, float near, float far); +CMatrix4f OrthographicMatrix(float left, float right, float bottom, float top, float near, float far); + // Constants static const float skPi = 3.14159265358979323846f; static const float skHalfPi = skPi / 2.f; diff --git a/Core/CCamera.cpp b/Core/CCamera.cpp index b160b91a..32f5a292 100644 --- a/Core/CCamera.cpp +++ b/Core/CCamera.cpp @@ -4,15 +4,13 @@ #include #include -#define HALF_PI 1.570796371f - CCamera::CCamera() { mMode = eFreeCamera; mPosition = CVector3f(0); mAspectRatio = 1.7777777f; - mYaw = -HALF_PI; + mYaw = -Math::skHalfPi; mPitch = 0.0f; CalculateDirection(); @@ -30,7 +28,7 @@ CCamera::CCamera(CVector3f Position, CVector3f) mMoveSpeed = 1.f; // Old: 0.01f mLookSpeed = 1.f; // Old: 0.003f mPosition = Position; - mYaw = -HALF_PI; + mYaw = -Math::skHalfPi; mPitch = 0.0f; CalculateDirection(); } @@ -43,8 +41,8 @@ void CCamera::Pan(float XAmount, float YAmount) case eFreeCamera: { CVector3f Right( - cos(mYaw - HALF_PI), - sin(mYaw - HALF_PI), + cos(mYaw - Math::skHalfPi), + sin(mYaw - Math::skHalfPi), 0 ); CVector3f Up = Right.Cross(mDirection); @@ -59,8 +57,8 @@ void CCamera::Pan(float XAmount, float YAmount) case eOrbitCamera: { CVector3f Right( - cos(mYaw - HALF_PI), - sin(mYaw - HALF_PI), + cos(mYaw - Math::skHalfPi), + sin(mYaw - Math::skHalfPi), 0 ); CVector3f Up = Right.Cross(mDirection); @@ -92,7 +90,7 @@ void CCamera::Zoom(float Amount) void CCamera::Snap(CVector3f Position) { mPosition = Position; - mYaw = -1.570796371f; + mYaw = -Math::skHalfPi; mPitch = 0.0f; mViewOutdated = true; } @@ -198,7 +196,7 @@ const CMatrix4f& CCamera::ViewMatrix() return mCachedViewMatrix; } -const CMatrix4f& CCamera::RotationOnlyViewMatrix() +CMatrix4f CCamera::RotationOnlyViewMatrix() { if (mViewOutdated) CalculateView(); @@ -270,8 +268,8 @@ void CCamera::SetAspectRatio(float AspectRatio) // ************ PRIVATE ************ void CCamera::CalculateDirection() { - if (mPitch > HALF_PI) mPitch = HALF_PI; - if (mPitch < -HALF_PI) mPitch = -HALF_PI; + if (mPitch > Math::skHalfPi) mPitch = Math::skHalfPi; + if (mPitch < -Math::skHalfPi) mPitch = -Math::skHalfPi; mDirection = CVector3f( cos(mPitch) * cos(mYaw), @@ -286,8 +284,8 @@ void CCamera::CalculateView() CalculateDirection(); CVector3f Right( - cos(mYaw - HALF_PI), - sin(mYaw - HALF_PI), + cos(mYaw - Math::skHalfPi), + sin(mYaw - Math::skHalfPi), 0 ); diff --git a/Core/CCamera.h b/Core/CCamera.h index ecd2cfa7..003bff37 100644 --- a/Core/CCamera.h +++ b/Core/CCamera.h @@ -54,7 +54,7 @@ public: float Pitch() const; float FieldOfView() const; const CMatrix4f& ViewMatrix(); - const CMatrix4f& RotationOnlyViewMatrix(); + CMatrix4f RotationOnlyViewMatrix(); const CMatrix4f& ProjectionMatrix(); // Setters diff --git a/Core/CDrawUtil.cpp b/Core/CDrawUtil.cpp index 57b1bd73..7fcc44d7 100644 --- a/Core/CDrawUtil.cpp +++ b/Core/CDrawUtil.cpp @@ -107,10 +107,6 @@ void CDrawUtil::DrawLine(const CVector3f& PointA, const CVector3f& PointB, const { Init(); - glLineWidth(1.f); - CGraphics::sMVPBlock.ModelMatrix = CMatrix4f::skIdentity; - CGraphics::UpdateMVPBlock(); - // Copy vec3s into an array to ensure they are adjacent in memory CVector3f Points[2] = { PointA, PointB }; mLineVertices.BufferAttrib(ePosition, Points); diff --git a/Scene/CScriptNode.cpp b/Scene/CScriptNode.cpp index 36c3d29c..140e9955 100644 --- a/Scene/CScriptNode.cpp +++ b/Scene/CScriptNode.cpp @@ -168,6 +168,9 @@ void CScriptNode::DrawSelection() if (mpInstance) { + CGraphics::sMVPBlock.ModelMatrix = CMatrix4f::skIdentity; + CGraphics::UpdateMVPBlock(); + for (u32 iIn = 0; iIn < mpInstance->NumInLinks(); iIn++) { const SLink& con = mpInstance->InLink(iIn); diff --git a/UI/CBasicViewport.cpp b/UI/CBasicViewport.cpp index 14dbf007..026190aa 100644 --- a/UI/CBasicViewport.cpp +++ b/UI/CBasicViewport.cpp @@ -53,7 +53,7 @@ void CBasicViewport::paintGL() Paint(); // Finally, draw XYZ axes in the corner -// DrawAxes(); + DrawAxes(); } void CBasicViewport::resizeGL(int w, int h) @@ -266,15 +266,17 @@ void CBasicViewport::ProcessInput(double DeltaTime) void CBasicViewport::DrawAxes() { // Draw 64x64 axes in lower-left corner with 8px margins - glViewport(8, height() - 72, 64, 64); + glViewport(8, 8, 64, 64); glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); + glDepthRange(0.f, 1.f); - CGraphics::sMVPBlock.ViewMatrix = CTransform4f::TranslationMatrix(CVector3f(0,2,0)).ToMatrix4f() * mCamera.RotationOnlyViewMatrix(); - CGraphics::sMVPBlock.ProjectionMatrix = Math::PerspectiveMatrix(mCamera.FieldOfView(), 1.f, 0.1f, 4096.f); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(mCamera.Direction() * 5).ToMatrix4f(); + CGraphics::sMVPBlock.ViewMatrix = mCamera.RotationOnlyViewMatrix(); + CGraphics::sMVPBlock.ProjectionMatrix = Math::OrthographicMatrix(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f); CGraphics::UpdateMVPBlock(); - glLineWidth(2.f); + glLineWidth(1.f); CDrawUtil::DrawLine(CVector3f(0,0,0), CVector3f(1,0,0), CColor::skRed); // X CDrawUtil::DrawLine(CVector3f(0,0,0), CVector3f(0,1,0), CColor::skGreen); // Y CDrawUtil::DrawLine(CVector3f(0,0,0), CVector3f(0,0,1), CColor::skBlue); // Z diff --git a/UI/CBasicViewport.h b/UI/CBasicViewport.h index 9ac6b1fd..5622e0df 100644 --- a/UI/CBasicViewport.h +++ b/UI/CBasicViewport.h @@ -1,17 +1,18 @@ #ifndef CEDITORGLWIDGET_H #define CEDITORGLWIDGET_H -#include #include #include -#include -#include -#include -#include + #include -#include #include #include +#include +#include + +#include +#include +#include class CBasicViewport : public QOpenGLWidget { @@ -54,6 +55,7 @@ public: bool IsCursorVisible(); bool IsMouseInputActive(); bool IsKeyboardInputActive(); + CRenderer* Renderer(); CCamera& Camera(); CRay CastRay(); CVector2f MouseDeviceCoordinates(); diff --git a/UI/CModelEditorViewport.h b/UI/CModelEditorViewport.h index ebf23b22..6d27c2dc 100644 --- a/UI/CModelEditorViewport.h +++ b/UI/CModelEditorViewport.h @@ -2,6 +2,7 @@ #define CMODELEDITORVIEWPORT_H #include "CBasicViewport.h" +#include class CModelEditorViewport : public CBasicViewport { diff --git a/UI/CWorldEditor.cpp b/UI/CWorldEditor.cpp index d250e59c..4fe1be86 100644 --- a/UI/CWorldEditor.cpp +++ b/UI/CWorldEditor.cpp @@ -313,8 +313,9 @@ void CWorldEditor::OnTransformSpinBoxModified(CVector3f value) void CWorldEditor::OnTransformSpinBoxEdited(CVector3f) { + // bit of a hack - the vector editor emits a second "editing done" signal when it loses focus ui->TransformSpinBox->blockSignals(true); - setFocus(); + ui->MainViewport->setFocus(); ui->TransformSpinBox->blockSignals(false); if (mSelection.empty()) return;