mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-03 17:19:46 +02:00
Viewport movement
This commit is contained in:
parent
8110358dbf
commit
8c25712ea6
@ -1,10 +1,11 @@
|
||||
#include "ViewerWidget.hpp"
|
||||
#include <render/Model.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <QMouseEvent>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user