removed timer rotation, now the user has full control,

added keyboard support,
reset rotation with space
This commit is contained in:
Anakin 2016-12-27 14:05:39 +01:00
parent a30f1b125c
commit faea3b0737
2 changed files with 46 additions and 42 deletions

View File

@ -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:
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;
}; };

View File

@ -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,45 +25,49 @@ 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)
// Rotation axis is perpendicular to the mouse position difference m_mouse.right = false;
// vector
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
// Accelerate angular speed relative to the length of the mouse sweep
qreal acc = diff.length() / 100.0;
// Calculate new rotation axis as weighted sum
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
// Increase angular speed
angularSpeed += acc;
} }
void OglViewerWidget::timerEvent(QTimerEvent *) void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
{ {
// Decrease angular speed (friction) if (m_mouse.left)
angularSpeed *= 0.99; {
// get the difference between last press and now
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
// Stop rotation when speed goes below threshold // update the new position
if (angularSpeed < 0.01) { m_mouse.position = QVector2D(e->localPos());
angularSpeed = 0.0;
} else {
// Update rotation
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
// Request an update // calculate the rotation axis and rotate
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
// request an update
update(); update();
} }
} }
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Space)
m_rotation = QQuaternion();
update();
}
void OglViewerWidget::initializeGL() void OglViewerWidget::initializeGL()
{ {
initializeOpenGLFunctions(); initializeOpenGLFunctions();
@ -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)
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()
// 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);