diff options
Diffstat (limited to 'src/opengl_widget.cc')
-rw-r--r-- | src/opengl_widget.cc | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/opengl_widget.cc b/src/opengl_widget.cc index 0251491..f10a8c6 100644 --- a/src/opengl_widget.cc +++ b/src/opengl_widget.cc @@ -1,5 +1,7 @@ #include "opengl_widget.hh" +#include <QMouseEvent> + static const GLchar *vertex_shader_source = R"glsl( #version 330 core @@ -135,9 +137,43 @@ void OpenGLWidget::resizeGL(int w, int h) { void OpenGLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + QMatrix4x4 trans; + trans.translate(0, 0, -cam_dist); + QMatrix4x4 view = trans * rot; + glUniformMatrix4fv(view_attr, 1, GL_FALSE, view.data()); for (const OpenGLMesh &mesh : meshes) { glUniformMatrix4fv(model_attr, 1, GL_FALSE, mesh.mat.data()); glBindVertexArray(mesh.vao); glDrawArrays(GL_TRIANGLES, 0, mesh.nverts); } } + + +void OpenGLWidget::mousePressEvent(QMouseEvent *e) { + if (e->button() == Qt::LeftButton) { + mouse_pos = e->pos(); + } +} + + +void OpenGLWidget::mouseReleaseEvent(QMouseEvent *e) { + (void) e; + rot_start = rot; +} + + +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(); + } +} + + +void OpenGLWidget::wheelEvent(QWheelEvent *e) { + cam_dist -= e->angleDelta().y() / 1000. * cam_dist; + update(); +} |