2015-09-01 17:05:48 +00:00
|
|
|
#include "CBasicViewport.h"
|
|
|
|
#include <Core/CDrawUtil.h>
|
2015-07-26 21:39:49 +00:00
|
|
|
#include <Core/CGraphics.h>
|
2015-09-01 17:05:48 +00:00
|
|
|
#include <Common/Math.h>
|
|
|
|
#include <GL/glew.h>
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
CBasicViewport::CBasicViewport(QWidget *pParent) :
|
|
|
|
QOpenGLWidget(pParent),
|
|
|
|
mLastDrawTime(CTimer::GlobalTime()),
|
|
|
|
mKeysPressed(0),
|
|
|
|
mButtonsPressed(0),
|
|
|
|
mCursorState(Qt::ArrowCursor),
|
|
|
|
mCursorVisible(true)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
setMouseTracking(true);
|
|
|
|
mCamera.SetAspectRatio((float) width() / height());
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
CBasicViewport::~CBasicViewport()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::initializeGL()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
// Initialize CGraphics
|
|
|
|
CGraphics::Initialize();
|
|
|
|
|
|
|
|
// Setting various GL flags
|
|
|
|
glEnable(GL_PRIMITIVE_RESTART);
|
|
|
|
glPrimitiveRestartIndex(0xFFFF);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glEnable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
glPolygonOffset(1.f, 5.f);
|
|
|
|
|
|
|
|
// Clear cached material
|
|
|
|
CMaterial::KillCachedMaterial();
|
|
|
|
CShader::KillCachedShader();
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
// Initialize size
|
|
|
|
OnResize();
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::paintGL()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-09-01 17:05:48 +00:00
|
|
|
// Prep render
|
|
|
|
glViewport(0, 0, width(), height());
|
|
|
|
glLineWidth(1.f);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
// Actual rendering is intended to be handled by subclassing CBasicViewport and
|
|
|
|
// reimplementing Render().
|
|
|
|
Paint();
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
// Finally, draw XYZ axes in the corner
|
2015-09-01 23:21:10 +00:00
|
|
|
DrawAxes();
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::resizeGL(int w, int h)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
mCamera.SetAspectRatio((float) w / h);
|
|
|
|
glViewport(0, 0, w, h);
|
2015-09-01 17:05:48 +00:00
|
|
|
OnResize();
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::mousePressEvent(QMouseEvent *pEvent)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
setFocus();
|
|
|
|
|
|
|
|
if (pEvent->button() == Qt::MidButton) mButtonsPressed |= eMiddleButton;
|
|
|
|
if (pEvent->button() == Qt::RightButton) mButtonsPressed |= eRightButton;
|
|
|
|
|
|
|
|
if (IsMouseInputActive())
|
2015-09-01 17:05:48 +00:00
|
|
|
{
|
2015-07-26 21:39:49 +00:00
|
|
|
SetCursorVisible(false);
|
2015-09-01 17:05:48 +00:00
|
|
|
mMouseMoved = false;
|
|
|
|
mMoveTimer.Restart();
|
|
|
|
}
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
// Left click only activates if mouse input is inactive to prevent the user from
|
|
|
|
// clicking on things and creating selection rectangles while the cursor is hidden
|
2015-08-20 01:01:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pEvent->button() == Qt::LeftButton)
|
|
|
|
mButtonsPressed |= eLeftButton;
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
OnMouseClick(pEvent);
|
2015-08-20 01:01:58 +00:00
|
|
|
}
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
mLastMousePos = pEvent->globalPos();
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::mouseReleaseEvent(QMouseEvent *pEvent)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-08-20 01:01:58 +00:00
|
|
|
bool fromMouseInput = IsMouseInputActive();
|
2015-07-26 21:39:49 +00:00
|
|
|
if (pEvent->button() == Qt::LeftButton) mButtonsPressed &= ~eLeftButton;
|
|
|
|
if (pEvent->button() == Qt::MidButton) mButtonsPressed &= ~eMiddleButton;
|
|
|
|
if (pEvent->button() == Qt::RightButton) mButtonsPressed &= ~eRightButton;
|
|
|
|
|
2015-08-20 01:01:58 +00:00
|
|
|
// Make cursor visible if needed
|
2015-07-26 21:39:49 +00:00
|
|
|
if (!IsMouseInputActive())
|
|
|
|
SetCursorVisible(true);
|
2015-08-20 01:01:58 +00:00
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
// Run mouse release if we didn't just exit mouse input (or regardless on left click)
|
2015-08-20 01:01:58 +00:00
|
|
|
if (!fromMouseInput || (pEvent->button() == Qt::LeftButton))
|
2015-09-01 17:05:48 +00:00
|
|
|
OnMouseRelease(pEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBasicViewport::mouseMoveEvent(QMouseEvent *pEvent)
|
|
|
|
{
|
|
|
|
// todo: draggable selection rectangle
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBasicViewport::wheelEvent(QWheelEvent *pEvent)
|
|
|
|
{
|
|
|
|
// Maybe track a "wheel delta" member variable and let CCamera decide what to do with it?
|
|
|
|
mCamera.Zoom(pEvent->angleDelta().y() / 6000.f);
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::keyPressEvent(QKeyEvent *pEvent)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
switch (pEvent->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Q: mKeysPressed |= eQKey; break;
|
|
|
|
case Qt::Key_W: mKeysPressed |= eWKey; break;
|
|
|
|
case Qt::Key_E: mKeysPressed |= eEKey; break;
|
|
|
|
case Qt::Key_A: mKeysPressed |= eAKey; break;
|
|
|
|
case Qt::Key_S: mKeysPressed |= eSKey; break;
|
|
|
|
case Qt::Key_D: mKeysPressed |= eDKey; break;
|
|
|
|
case Qt::Key_Control: mKeysPressed |= eCtrlKey; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::keyReleaseEvent(QKeyEvent *pEvent)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
switch (pEvent->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Q: mKeysPressed &= ~eQKey; break;
|
|
|
|
case Qt::Key_W: mKeysPressed &= ~eWKey; break;
|
|
|
|
case Qt::Key_E: mKeysPressed &= ~eEKey; break;
|
|
|
|
case Qt::Key_A: mKeysPressed &= ~eAKey; break;
|
|
|
|
case Qt::Key_S: mKeysPressed &= ~eSKey; break;
|
|
|
|
case Qt::Key_D: mKeysPressed &= ~eDKey; break;
|
|
|
|
case Qt::Key_Control: mKeysPressed &= ~eCtrlKey; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::focusOutEvent(QFocusEvent*)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
// When the widget loses focus, release all input.
|
|
|
|
mButtonsPressed = 0;
|
|
|
|
mKeysPressed = 0;
|
|
|
|
SetCursorVisible(true);
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::contextMenuEvent(QContextMenuEvent *pEvent)
|
|
|
|
{
|
|
|
|
// Only allow context menu if we aren't exiting mouse input mode.
|
|
|
|
if (!mMouseMoved && (mMoveTimer.Time() < 0.5))
|
|
|
|
ContextMenu(pEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBasicViewport::SetCursorState(const QCursor &Cursor)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
mCursorState = Cursor;
|
|
|
|
|
|
|
|
if (IsCursorVisible())
|
|
|
|
setCursor(Cursor);
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
void CBasicViewport::SetCursorVisible(bool visible)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
mCursorVisible = visible;
|
|
|
|
|
|
|
|
if (visible)
|
|
|
|
setCursor(mCursorState);
|
|
|
|
else
|
|
|
|
setCursor(Qt::BlankCursor);
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
bool CBasicViewport::IsCursorVisible()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
return mCursorVisible;
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
bool CBasicViewport::IsMouseInputActive()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
static const int skMoveButtons = eMiddleButton | eRightButton;
|
|
|
|
return ((mButtonsPressed & skMoveButtons) != 0);
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
bool CBasicViewport::IsKeyboardInputActive()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
static const int skMoveKeys = eQKey | eWKey | eEKey | eAKey | eSKey | eDKey;
|
|
|
|
return ((mKeysPressed & skMoveKeys) != 0);
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
CCamera& CBasicViewport::Camera()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
return mCamera;
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
CRay CBasicViewport::CastRay()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
CVector2f MouseCoords = MouseDeviceCoordinates();
|
|
|
|
return mCamera.CastRay(MouseCoords);
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
CVector2f CBasicViewport::MouseDeviceCoordinates()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
QPoint MousePos = QCursor::pos();
|
|
|
|
QPoint ThisPos = this->mapToGlobal(pos());
|
|
|
|
MousePos -= ThisPos;
|
|
|
|
|
|
|
|
CVector2f Device(
|
|
|
|
(((2.f * MousePos.x()) / width()) - 1.f),
|
|
|
|
(1.f - ((2.f * MousePos.y()) / height()))
|
|
|
|
);
|
|
|
|
return Device;
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
double CBasicViewport::LastRenderDuration()
|
|
|
|
{
|
|
|
|
return mFrameTimer.Time();
|
|
|
|
}
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-09-01 17:05:48 +00:00
|
|
|
// ************ PUBLIC SLOTS ************
|
|
|
|
void CBasicViewport::ProcessInput()
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-09-01 17:05:48 +00:00
|
|
|
// Process camera input
|
|
|
|
double DeltaTime = CTimer::GlobalTime() - mLastDrawTime;
|
|
|
|
mLastDrawTime = CTimer::GlobalTime();
|
|
|
|
|
2015-07-26 21:39:49 +00:00
|
|
|
if (IsMouseInputActive())
|
|
|
|
{
|
|
|
|
float XMovement = (QCursor::pos().x() - mLastMousePos.x()) * 0.01f;
|
|
|
|
float YMovement = (QCursor::pos().y() - mLastMousePos.y()) * 0.01f;
|
|
|
|
|
|
|
|
if ((XMovement != 0) || (YMovement != 0))
|
|
|
|
{
|
|
|
|
mCamera.ProcessMouseInput((EKeyInputs) mKeysPressed, (EMouseInputs) mButtonsPressed, XMovement, YMovement);
|
|
|
|
QCursor::setPos(mLastMousePos);
|
2015-09-01 17:05:48 +00:00
|
|
|
mMouseMoved = true;
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsKeyboardInputActive())
|
|
|
|
mCamera.ProcessKeyInput((EKeyInputs) mKeysPressed, DeltaTime);
|
2015-09-01 17:05:48 +00:00
|
|
|
|
|
|
|
// Check user input
|
|
|
|
CheckUserInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBasicViewport::Render()
|
|
|
|
{
|
|
|
|
mFrameTimer.Start();
|
|
|
|
update();
|
|
|
|
mFrameTimer.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ************ PRIVATE ************
|
|
|
|
void CBasicViewport::ProcessInput(double DeltaTime)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBasicViewport::DrawAxes()
|
|
|
|
{
|
|
|
|
// Draw 64x64 axes in lower-left corner with 8px margins
|
2015-09-01 23:21:10 +00:00
|
|
|
glViewport(8, 8, 64, 64);
|
2015-09-01 17:05:48 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
2015-09-01 23:21:10 +00:00
|
|
|
glDepthRange(0.f, 1.f);
|
2015-09-01 17:05:48 +00:00
|
|
|
|
2015-09-01 23:21:10 +00:00
|
|
|
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);
|
2015-09-01 17:05:48 +00:00
|
|
|
CGraphics::UpdateMVPBlock();
|
|
|
|
|
2015-09-01 23:21:10 +00:00
|
|
|
glLineWidth(1.f);
|
2015-09-01 17:05:48 +00:00
|
|
|
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
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|