Commit faea3b07 authored by Anakin's avatar Anakin
Browse files

removed timer rotation, now the user has full control,

added keyboard support,
reset rotation with space
parent a30f1b12
...@@ -24,7 +24,8 @@ public: ...@@ -24,7 +24,8 @@ public:
protected: protected:
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
void initializeGL() Q_DECL_OVERRIDE; void initializeGL() Q_DECL_OVERRIDE;
void resizeGL(int w, int h) Q_DECL_OVERRIDE; void resizeGL(int w, int h) Q_DECL_OVERRIDE;
...@@ -34,17 +35,17 @@ protected: ...@@ -34,17 +35,17 @@ protected:
void initTextures(); void initTextures();
private: private:
QBasicTimer timer; struct {
bool left = false;
bool right = false;
QVector2D position;
} m_mouse;
QOpenGLShaderProgram program; QOpenGLShaderProgram program;
GeometryEngine *geometries; GeometryEngine *geometries;
QOpenGLTexture *texture; QOpenGLTexture *texture;
QMatrix4x4 projection; QMatrix4x4 m_projection;
QQuaternion m_rotation;
QVector2D mousePressPosition;
QVector3D rotationAxis;
qreal angularSpeed;
QQuaternion rotation;
}; };
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <math.h> #include <math.h>
#include <iostream>
OglViewerWidget::OglViewerWidget(QWidget *parent) : OglViewerWidget::OglViewerWidget(QWidget *parent) :
QOpenGLWidget(parent), QOpenGLWidget(parent),
geometries(0), geometries(0),
texture(0), texture(0)
angularSpeed(0)
{ {
setFocus();
} }
OglViewerWidget::~OglViewerWidget() OglViewerWidget::~OglViewerWidget()
...@@ -24,43 +25,47 @@ OglViewerWidget::~OglViewerWidget() ...@@ -24,43 +25,47 @@ OglViewerWidget::~OglViewerWidget()
void OglViewerWidget::mousePressEvent(QMouseEvent *e) void OglViewerWidget::mousePressEvent(QMouseEvent *e)
{ {
// Save mouse press position // Save mouse press position
mousePressPosition = QVector2D(e->localPos()); m_mouse.position = QVector2D(e->localPos());
// Which button has been pressed?
if (e->button() == Qt::LeftButton)
m_mouse.left = true;
else if (e->button() == Qt::RightButton)
m_mouse.right = true;
} }
void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e) void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e)
{ {
// Mouse release position - mouse press position if (e->button() == Qt::LeftButton)
QVector2D diff = QVector2D(e->localPos()) - mousePressPosition; m_mouse.left = false;
else if (e->button() == Qt::RightButton)
m_mouse.right = false;
}
// Rotation axis is perpendicular to the mouse position difference void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
// vector {
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized(); if (m_mouse.left)
{
// get the difference between last press and now
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
// Accelerate angular speed relative to the length of the mouse sweep // update the new position
qreal acc = diff.length() / 100.0; m_mouse.position = QVector2D(e->localPos());
// Calculate new rotation axis as weighted sum // calculate the rotation axis and rotate
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized(); m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
// Increase angular speed // request an update
angularSpeed += acc; update();
}
} }
void OglViewerWidget::timerEvent(QTimerEvent *) void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{ {
// Decrease angular speed (friction) if (e->key() == Qt::Key_Space)
angularSpeed *= 0.99; m_rotation = QQuaternion();
// Stop rotation when speed goes below threshold
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
// Update rotation
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
// Request an update
update(); update();
}
} }
void OglViewerWidget::initializeGL() void OglViewerWidget::initializeGL()
...@@ -80,8 +85,6 @@ void OglViewerWidget::initializeGL() ...@@ -80,8 +85,6 @@ void OglViewerWidget::initializeGL()
geometries = new GeometryEngine; geometries = new GeometryEngine;
// Use QBasicTimer because its faster than QTimer
timer.start(12, this);
} }
void OglViewerWidget::initShaders() void OglViewerWidget::initShaders()
...@@ -128,10 +131,10 @@ void OglViewerWidget::resizeGL(int w, int h) ...@@ -128,10 +131,10 @@ void OglViewerWidget::resizeGL(int w, int h)
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0; const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
// Reset projection // Reset projection
projection.setToIdentity(); m_projection.setToIdentity();
// Set perspective projection // Set perspective projection
projection.perspective(fov, aspect, zNear, zFar); m_projection.perspective(fov, aspect, zNear, zFar);
} }
void OglViewerWidget::paintGL() void OglViewerWidget::paintGL()
...@@ -144,10 +147,10 @@ void OglViewerWidget::paintGL() ...@@ -144,10 +147,10 @@ void OglViewerWidget::paintGL()
// Calculate model view transformation // Calculate model view transformation
QMatrix4x4 matrix; QMatrix4x4 matrix;
matrix.translate(0.0, 0.0, -5.0); matrix.translate(0.0, 0.0, -5.0);
matrix.rotate(rotation); matrix.rotate(m_rotation);
// Set modelview-projection matrix // Set modelview-projection matrix
program.setUniformValue("mvp_matrix", projection * matrix); program.setUniformValue("mvp_matrix", m_projection * matrix);
// Use texture unit 0 which contains cube.png // Use texture unit 0 which contains cube.png
program.setUniformValue("texture", 0); program.setUniformValue("texture", 0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment