Set up CBasicViewport to draw an XYZ axis overlay in the corner

This commit is contained in:
parax0 2015-09-01 19:21:10 -04:00
parent ea7e4d2b25
commit 89c1654b6a
10 changed files with 41 additions and 31 deletions

View File

@ -344,4 +344,9 @@ CMatrix4f PerspectiveMatrix(float fov, float aspect, float near, float far)
return CMatrix4f::FromGlmMat4(glm::perspective(fov, aspect, near, far)).Transpose(); 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 } // End namespace

View File

@ -34,6 +34,8 @@ std::pair<bool,float> RayTriangleIntersection(const CRay& Ray, const CVector3f&
CMatrix4f PerspectiveMatrix(float fov, float aspect, float near, float far); 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 // Constants
static const float skPi = 3.14159265358979323846f; static const float skPi = 3.14159265358979323846f;
static const float skHalfPi = skPi / 2.f; static const float skHalfPi = skPi / 2.f;

View File

@ -4,15 +4,13 @@
#include <Common/Math.h> #include <Common/Math.h>
#include <gtc/matrix_transform.hpp> #include <gtc/matrix_transform.hpp>
#define HALF_PI 1.570796371f
CCamera::CCamera() CCamera::CCamera()
{ {
mMode = eFreeCamera; mMode = eFreeCamera;
mPosition = CVector3f(0); mPosition = CVector3f(0);
mAspectRatio = 1.7777777f; mAspectRatio = 1.7777777f;
mYaw = -HALF_PI; mYaw = -Math::skHalfPi;
mPitch = 0.0f; mPitch = 0.0f;
CalculateDirection(); CalculateDirection();
@ -30,7 +28,7 @@ CCamera::CCamera(CVector3f Position, CVector3f)
mMoveSpeed = 1.f; // Old: 0.01f mMoveSpeed = 1.f; // Old: 0.01f
mLookSpeed = 1.f; // Old: 0.003f mLookSpeed = 1.f; // Old: 0.003f
mPosition = Position; mPosition = Position;
mYaw = -HALF_PI; mYaw = -Math::skHalfPi;
mPitch = 0.0f; mPitch = 0.0f;
CalculateDirection(); CalculateDirection();
} }
@ -43,8 +41,8 @@ void CCamera::Pan(float XAmount, float YAmount)
case eFreeCamera: case eFreeCamera:
{ {
CVector3f Right( CVector3f Right(
cos(mYaw - HALF_PI), cos(mYaw - Math::skHalfPi),
sin(mYaw - HALF_PI), sin(mYaw - Math::skHalfPi),
0 0
); );
CVector3f Up = Right.Cross(mDirection); CVector3f Up = Right.Cross(mDirection);
@ -59,8 +57,8 @@ void CCamera::Pan(float XAmount, float YAmount)
case eOrbitCamera: case eOrbitCamera:
{ {
CVector3f Right( CVector3f Right(
cos(mYaw - HALF_PI), cos(mYaw - Math::skHalfPi),
sin(mYaw - HALF_PI), sin(mYaw - Math::skHalfPi),
0 0
); );
CVector3f Up = Right.Cross(mDirection); CVector3f Up = Right.Cross(mDirection);
@ -92,7 +90,7 @@ void CCamera::Zoom(float Amount)
void CCamera::Snap(CVector3f Position) void CCamera::Snap(CVector3f Position)
{ {
mPosition = Position; mPosition = Position;
mYaw = -1.570796371f; mYaw = -Math::skHalfPi;
mPitch = 0.0f; mPitch = 0.0f;
mViewOutdated = true; mViewOutdated = true;
} }
@ -198,7 +196,7 @@ const CMatrix4f& CCamera::ViewMatrix()
return mCachedViewMatrix; return mCachedViewMatrix;
} }
const CMatrix4f& CCamera::RotationOnlyViewMatrix() CMatrix4f CCamera::RotationOnlyViewMatrix()
{ {
if (mViewOutdated) if (mViewOutdated)
CalculateView(); CalculateView();
@ -270,8 +268,8 @@ void CCamera::SetAspectRatio(float AspectRatio)
// ************ PRIVATE ************ // ************ PRIVATE ************
void CCamera::CalculateDirection() void CCamera::CalculateDirection()
{ {
if (mPitch > HALF_PI) mPitch = HALF_PI; if (mPitch > Math::skHalfPi) mPitch = Math::skHalfPi;
if (mPitch < -HALF_PI) mPitch = -HALF_PI; if (mPitch < -Math::skHalfPi) mPitch = -Math::skHalfPi;
mDirection = CVector3f( mDirection = CVector3f(
cos(mPitch) * cos(mYaw), cos(mPitch) * cos(mYaw),
@ -286,8 +284,8 @@ void CCamera::CalculateView()
CalculateDirection(); CalculateDirection();
CVector3f Right( CVector3f Right(
cos(mYaw - HALF_PI), cos(mYaw - Math::skHalfPi),
sin(mYaw - HALF_PI), sin(mYaw - Math::skHalfPi),
0 0
); );

View File

@ -54,7 +54,7 @@ public:
float Pitch() const; float Pitch() const;
float FieldOfView() const; float FieldOfView() const;
const CMatrix4f& ViewMatrix(); const CMatrix4f& ViewMatrix();
const CMatrix4f& RotationOnlyViewMatrix(); CMatrix4f RotationOnlyViewMatrix();
const CMatrix4f& ProjectionMatrix(); const CMatrix4f& ProjectionMatrix();
// Setters // Setters

View File

@ -107,10 +107,6 @@ void CDrawUtil::DrawLine(const CVector3f& PointA, const CVector3f& PointB, const
{ {
Init(); Init();
glLineWidth(1.f);
CGraphics::sMVPBlock.ModelMatrix = CMatrix4f::skIdentity;
CGraphics::UpdateMVPBlock();
// Copy vec3s into an array to ensure they are adjacent in memory // Copy vec3s into an array to ensure they are adjacent in memory
CVector3f Points[2] = { PointA, PointB }; CVector3f Points[2] = { PointA, PointB };
mLineVertices.BufferAttrib(ePosition, Points); mLineVertices.BufferAttrib(ePosition, Points);

View File

@ -168,6 +168,9 @@ void CScriptNode::DrawSelection()
if (mpInstance) if (mpInstance)
{ {
CGraphics::sMVPBlock.ModelMatrix = CMatrix4f::skIdentity;
CGraphics::UpdateMVPBlock();
for (u32 iIn = 0; iIn < mpInstance->NumInLinks(); iIn++) for (u32 iIn = 0; iIn < mpInstance->NumInLinks(); iIn++)
{ {
const SLink& con = mpInstance->InLink(iIn); const SLink& con = mpInstance->InLink(iIn);

View File

@ -53,7 +53,7 @@ void CBasicViewport::paintGL()
Paint(); Paint();
// Finally, draw XYZ axes in the corner // Finally, draw XYZ axes in the corner
// DrawAxes(); DrawAxes();
} }
void CBasicViewport::resizeGL(int w, int h) void CBasicViewport::resizeGL(int w, int h)
@ -266,15 +266,17 @@ void CBasicViewport::ProcessInput(double DeltaTime)
void CBasicViewport::DrawAxes() void CBasicViewport::DrawAxes()
{ {
// Draw 64x64 axes in lower-left corner with 8px margins // 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); glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glDepthRange(0.f, 1.f);
CGraphics::sMVPBlock.ViewMatrix = CTransform4f::TranslationMatrix(CVector3f(0,2,0)).ToMatrix4f() * mCamera.RotationOnlyViewMatrix(); CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(mCamera.Direction() * 5).ToMatrix4f();
CGraphics::sMVPBlock.ProjectionMatrix = Math::PerspectiveMatrix(mCamera.FieldOfView(), 1.f, 0.1f, 4096.f); CGraphics::sMVPBlock.ViewMatrix = mCamera.RotationOnlyViewMatrix();
CGraphics::sMVPBlock.ProjectionMatrix = Math::OrthographicMatrix(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f);
CGraphics::UpdateMVPBlock(); 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(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,1,0), CColor::skGreen); // Y
CDrawUtil::DrawLine(CVector3f(0,0,0), CVector3f(0,0,1), CColor::skBlue); // Z CDrawUtil::DrawLine(CVector3f(0,0,0), CVector3f(0,0,1), CColor::skBlue); // Z

View File

@ -1,17 +1,18 @@
#ifndef CEDITORGLWIDGET_H #ifndef CEDITORGLWIDGET_H
#define CEDITORGLWIDGET_H #define CEDITORGLWIDGET_H
#include <QTimer>
#include <gl/glew.h> #include <gl/glew.h>
#include <QOpenGLWidget> #include <QOpenGLWidget>
#include <QMouseEvent>
#include <Common/CVector2f.h>
#include <Common/CVector2i.h>
#include <Core/CSceneManager.h>
#include <Core/CRenderer.h> #include <Core/CRenderer.h>
#include <Resource/CFont.h>
#include <Common/CRay.h> #include <Common/CRay.h>
#include <Common/CTimer.h> #include <Common/CTimer.h>
#include <Common/CVector2i.h>
#include <Common/CVector2f.h>
#include <QMouseEvent>
#include <QPoint>
#include <QTimer>
class CBasicViewport : public QOpenGLWidget class CBasicViewport : public QOpenGLWidget
{ {
@ -54,6 +55,7 @@ public:
bool IsCursorVisible(); bool IsCursorVisible();
bool IsMouseInputActive(); bool IsMouseInputActive();
bool IsKeyboardInputActive(); bool IsKeyboardInputActive();
CRenderer* Renderer();
CCamera& Camera(); CCamera& Camera();
CRay CastRay(); CRay CastRay();
CVector2f MouseDeviceCoordinates(); CVector2f MouseDeviceCoordinates();

View File

@ -2,6 +2,7 @@
#define CMODELEDITORVIEWPORT_H #define CMODELEDITORVIEWPORT_H
#include "CBasicViewport.h" #include "CBasicViewport.h"
#include <Scene/CModelNode.h>
class CModelEditorViewport : public CBasicViewport class CModelEditorViewport : public CBasicViewport
{ {

View File

@ -313,8 +313,9 @@ void CWorldEditor::OnTransformSpinBoxModified(CVector3f value)
void CWorldEditor::OnTransformSpinBoxEdited(CVector3f) 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); ui->TransformSpinBox->blockSignals(true);
setFocus(); ui->MainViewport->setFocus();
ui->TransformSpinBox->blockSignals(false); ui->TransformSpinBox->blockSignals(false);
if (mSelection.empty()) return; if (mSelection.empty()) return;