diff --git a/opglwidget.cpp b/opglwidget.cpp index b2c600c..4d42347 100644 --- a/opglwidget.cpp +++ b/opglwidget.cpp @@ -177,4 +177,33 @@ void OPGLWidget::keyReleaseEvent(QKeyEvent *event) { }; } +void OPGLWidget::mousePressEvent(QMouseEvent *event) { + setMouseTracking(true); + mouse_loc_old[0] = event -> x(); + mouse_loc_old[1] = event -> y(); +} +void OPGLWidget::mouseReleaseEvent(QMouseEvent *event) { + setMouseTracking(false); + mouse_loc_old[0] = event -> x(); + mouse_loc_old[1] = event -> y(); +} + +void OPGLWidget::mouseMoveEvent(QMouseEvent *event) { + float delta_x = event->x() - mouse_loc_old[0]; + float delta_y = event->y() - mouse_loc_old[1]; + mouse_loc_old[0] = event->x(); + mouse_loc_old[1] = event->y(); + for (int i = 0; i < 3; i++) { + f[i] += right[i] * delta_x / 1000.0f; + f[i] -= up[i] * delta_y / 1000.0f; + } + right[0] = -f[2]; + right[2] = f[0]; + float F_mag = pow(pow(f[0], 2) + pow(f[1], 2) + pow(f[2], 2), 0.5); + float right_mag = pow(pow(right[0], 2) + pow(right[1], 2) + pow(right[2], 2), 0.5); + for (int i = 0; i < 3; i++) { + f[i] = f[i] / F_mag; + right[i] = right[i] / right_mag; + } +} diff --git a/opglwidget.h b/opglwidget.h index 75bd1dc..10f6908 100644 --- a/opglwidget.h +++ b/opglwidget.h @@ -1,6 +1,7 @@ #include #include "obj.h" #include +#include class OPGLWidget : public QOpenGLWidget { @@ -17,6 +18,9 @@ protected: void keyPressEvent(QKeyEvent *event); void keyReleaseEvent(QKeyEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); int mtl_idx = -1; float loc[3] = {0, 0, 10}; @@ -27,4 +31,5 @@ protected: float speed = 0; ObjectLoader obj; bool key_states[6] = {false, false, false, false, false, false}; + float mouse_loc_old[2]; };