aboutsummaryrefslogtreecommitdiff
path: root/src/opengl_widget.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/opengl_widget.cc')
-rw-r--r--src/opengl_widget.cc78
1 files changed, 63 insertions, 15 deletions
diff --git a/src/opengl_widget.cc b/src/opengl_widget.cc
index 582bf3e..92166ac 100644
--- a/src/opengl_widget.cc
+++ b/src/opengl_widget.cc
@@ -24,13 +24,21 @@ OpenGLWidget *OpenGLWidget::instance = nullptr;
OpenGLWidget::OpenGLWidget(QWidget *parent)
- :QOpenGLWidget(parent) {
+ :QOpenGLWidget(parent),
+ move_timer(this) {
OpenGLWidget::instance = this;
QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CoreProfile);
format.setDepthBufferSize(24);
format.setSamples(4);
setFormat(format);
+ setFocusPolicy(Qt::StrongFocus);
+ trans.translate(0, -10, -10);
+ rot.rotate(30, QVector3D(1, 0, 0));
+ rot_start = rot;
+ move_timer.setTimerType(Qt::PreciseTimer);
+ connect(&move_timer, &QTimer::timeout, this, &OpenGLWidget::move);
+ move_timer.start(16);
}
@@ -179,9 +187,7 @@ void OpenGLWidget::paintGL() {
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- QMatrix4x4 trans;
- trans.translate(0, 0, -cam_dist);
- QMatrix4x4 view = trans * rot;
+ QMatrix4x4 view = rot * trans;
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
@@ -230,18 +236,62 @@ void OpenGLWidget::mouseReleaseEvent(QMouseEvent *e) {
void OpenGLWidget::mouseMoveEvent(QMouseEvent *e) {
- if (e->buttons() & Qt::LeftButton) {
- QPoint delta = e->pos() - mouse_pos;
- rot = rot_start;
- rot.rotate(delta.x() / 5., 0, 1, 0);
- rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot);
- update();
+ if (!(e->buttons() & Qt::LeftButton)) return;
+ QPoint delta = e->pos() - mouse_pos;
+ rot = rot_start;
+ rot.rotate(delta.x() / 5., 0, 1, 0);
+ rot.rotate(delta.y() / 5., QVector3D(1, 0, 0) * rot);
+ update();
+}
+
+
+bool OpenGLWidget::keyEvent(QKeyEvent *e, bool press) {
+ if (e->isAutoRepeat()) return false;
+ /* would do wasd if qt had proper support for layout-independent
+ input, but it doesnt :< */
+ switch (e->key()) {
+ case Qt::Key_Up:
+ move_forward = press;
+ break;
+ case Qt::Key_Down:
+ move_back = press;
+ break;
+ case Qt::Key_Left:
+ move_left = press;
+ break;
+ case Qt::Key_Right:
+ move_right = press;
+ break;
+ default:
+ return false;
}
+ update();
+ return true;
+}
+
+
+void OpenGLWidget::keyPressEvent(QKeyEvent *e) {
+ if (!keyEvent(e, true)) QOpenGLWidget::keyPressEvent(e);
+}
+
+
+void OpenGLWidget::keyReleaseEvent(QKeyEvent *e) {
+ if (!keyEvent(e, false)) QOpenGLWidget::keyReleaseEvent(e);
+}
+
+
+void OpenGLWidget::focusOutEvent(QFocusEvent *e) {
+ Q_UNUSED(e);
+ move_forward = move_back = move_left = move_right = false;
}
-void OpenGLWidget::wheelEvent(QWheelEvent *e) {
- cam_dist -= e->angleDelta().y() / 1000. * cam_dist;
+void OpenGLWidget::move() {
+ QMatrix4x4 rotation = rot.inverted();
+ if (move_forward) trans.translate(-(rotation * QVector3D(0, 0, -1)));
+ if (move_back) trans.translate(-(rotation * QVector3D(0, 0, 1)));
+ if (move_left) trans.translate(-(rotation * QVector3D(-1, 0, 0)));
+ if (move_right) trans.translate(-(rotation * QVector3D(1, 0, 0)));
update();
}
@@ -261,9 +311,7 @@ QOpenGLShaderProgram *OpenGLWidget::getLineProgram() {
bool OpenGLWidget::project(const QVector3D &p, QPoint &point) const {
- QMatrix4x4 trans;
- trans.translate(0, 0, -cam_dist);
- QMatrix4x4 view = trans * rot;
+ QMatrix4x4 view = rot * trans;
QVector3D projected = proj * view * p;
if (projected.x() < -1 || projected.x() > 1
|| projected.y() < -1 || projected.y() > 1