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();
}
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

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 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;

View File

@ -4,15 +4,13 @@
#include <Common/Math.h>
#include <gtc/matrix_transform.hpp>
#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
);

View File

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

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

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

View File

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

View File

@ -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;