Commit faea3b07 authored by Anakin's avatar Anakin

removed timer rotation, now the user has full control,

added keyboard support,
reset rotation with space
parent a30f1b12
......@@ -24,7 +24,8 @@ public:
protected:
void mousePressEvent(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 resizeGL(int w, int h) Q_DECL_OVERRIDE;
......@@ -34,17 +35,17 @@ protected:
void initTextures();
private:
QBasicTimer timer;
struct {
bool left = false;
bool right = false;
QVector2D position;
} m_mouse;
QOpenGLShaderProgram program;
GeometryEngine *geometries;
QOpenGLTexture *texture;
QMatrix4x4 projection;
QVector2D mousePressPosition;
QVector3D rotationAxis;
qreal angularSpeed;
QQuaternion rotation;
QMatrix4x4 m_projection;
QQuaternion m_rotation;
};
......@@ -2,13 +2,14 @@
#include <QMouseEvent>
#include <math.h>
#include <iostream>
OglViewerWidget::OglViewerWidget(QWidget *parent) :
QOpenGLWidget(parent),
geometries(0),
texture(0),
angularSpeed(0)
texture(0)
{
setFocus();
}
OglViewerWidget::~OglViewerWidget()
......@@ -24,43 +25,47 @@ OglViewerWidget::~OglViewerWidget()
void OglViewerWidget::mousePressEvent(QMouseEvent *e)
{
// 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)
{
// Mouse release position - mouse press position
QVector2D diff = QVector2D(e->localPos()) - mousePressPosition;
if (e->button() == Qt::LeftButton)
m_mouse.left = false;
else if (e->button() == Qt::RightButton)
m_mouse.right = false;
}
// Rotation axis is perpendicular to the mouse position difference
// vector
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
{
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
qreal acc = diff.length() / 100.0;
// update the new position
m_mouse.position = QVector2D(e->localPos());
// Calculate new rotation axis as weighted sum
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
// calculate the rotation axis and rotate
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
// Increase angular speed
angularSpeed += acc;
// request an update
update();
}
}
void OglViewerWidget::timerEvent(QTimerEvent *)
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{
// Decrease angular speed (friction)
angularSpeed *= 0.99;
// Stop rotation when speed goes below threshold
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
// Update rotation
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
if (e->key() == Qt::Key_Space)
m_rotation = QQuaternion();
// Request an update
update();
}
update();
}
void OglViewerWidget::initializeGL()
......@@ -80,8 +85,6 @@ void OglViewerWidget::initializeGL()
geometries = new GeometryEngine;
// Use QBasicTimer because its faster than QTimer
timer.start(12, this);
}
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;
// Reset projection
projection.setToIdentity();
m_projection.setToIdentity();
// Set perspective projection
projection.perspective(fov, aspect, zNear, zFar);
m_projection.perspective(fov, aspect, zNear, zFar);
}
void OglViewerWidget::paintGL()
......@@ -144,10 +147,10 @@ void OglViewerWidget::paintGL()
// Calculate model view transformation
QMatrix4x4 matrix;
matrix.translate(0.0, 0.0, -5.0);
matrix.rotate(rotation);
matrix.rotate(m_rotation);
// 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
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