From 8c25712ea6131d19d7bb92a978ce7cda1c309404 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Tue, 11 Feb 2014 15:45:45 +0000 Subject: [PATCH] Viewport movement --- rwviewer/ViewerWidget.cpp | 34 ++++++++++++++++++++++++++++++++-- rwviewer/ViewerWidget.hpp | 12 ++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rwviewer/ViewerWidget.cpp b/rwviewer/ViewerWidget.cpp index e244be96..9f2c1443 100644 --- a/rwviewer/ViewerWidget.cpp +++ b/rwviewer/ViewerWidget.cpp @@ -1,10 +1,11 @@ #include "ViewerWidget.hpp" #include #include +#include ViewerWidget::ViewerWidget(QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f) : QGLWidget(parent, shareWidget, f), gworld(nullptr), currentModel(nullptr), - viewDistance(1.f), fm(ViewerWidget::UNK) + viewDistance(1.f), dragging(false), fm(ViewerWidget::UNK) { } @@ -42,6 +43,8 @@ void ViewerWidget::paintGL() r.camera.frustum.aspectRatio = width()/(height()*1.f); if(currentModel) { + glEnable(GL_DEPTH_TEST); + glm::mat4 m; glUseProgram(r.worldProgram); @@ -56,7 +59,8 @@ void ViewerWidget::paintGL() glUniform1f(r.uniMatAmbient, 0.1f); glm::mat4 proj = r.camera.frustum.projection(); - glm::mat4 view = glm::lookAt(glm::vec3(0.f, viewDistance, 0.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 1.f)); + glm::vec3 eye(sin(viewAngles.x) * cos(viewAngles.y), cos(viewAngles.x) * cos(viewAngles.y), sin(viewAngles.y)); + glm::mat4 view = glm::lookAt(eye * viewDistance, glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 1.f)); glUniformMatrix4fv(r.uniView, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(r.uniProj, 1, GL_FALSE, glm::value_ptr(proj)); @@ -110,3 +114,29 @@ void ViewerWidget::showTXD(const QString& file) { fm = ViewerWidget::TXD; } + +void ViewerWidget::mousePressEvent(QMouseEvent* e) +{ + dragging = true; + dstart = e->localPos(); + dastart = viewAngles; +} + +void ViewerWidget::mouseReleaseEvent(QMouseEvent*) +{ + dragging = false; +} + +void ViewerWidget::mouseMoveEvent(QMouseEvent* e) +{ + if(dragging) { + auto d = e->localPos() - dstart; + viewAngles = dastart + glm::vec2(d.x(), d.y()) * 0.01f; + } +} + +void ViewerWidget::wheelEvent(QWheelEvent* e) +{ + viewDistance = qMax(viewDistance - e->angleDelta().y() / 240.f, 1.f); +} + diff --git a/rwviewer/ViewerWidget.hpp b/rwviewer/ViewerWidget.hpp index d75be9ea..b88abf4f 100644 --- a/rwviewer/ViewerWidget.hpp +++ b/rwviewer/ViewerWidget.hpp @@ -19,6 +19,11 @@ class ViewerWidget : public QGLWidget Model* currentModel; float viewDistance; + glm::vec2 viewAngles; + + bool dragging; + QPointF dstart; + glm::vec2 dastart; public: enum FileMode { @@ -45,6 +50,13 @@ signals: void fileOpened(const QString& file); +protected: + + virtual void mousePressEvent(QMouseEvent*); + virtual void mouseReleaseEvent(QMouseEvent*); + virtual void mouseMoveEvent(QMouseEvent*); + virtual void wheelEvent(QWheelEvent*); + private: FileMode fm; };