diff options
| author | Samuel Rødal <sroedal@trolltech.com> | 2008-06-24 10:11:16 +0200 |
|---|---|---|
| committer | Samuel Rødal <sroedal@trolltech.com> | 2008-06-24 10:13:16 +0200 |
| commit | 441e88cc54fa82f3778876ad6fcfbe82a6957c25 (patch) | |
| tree | 5fc8da906945f9d1ccb3939bac49745ccf83aed0 | |
| parent | 7826de9714746120b5c1a0a1110d1cfee2314df7 (diff) | |
Improve rotation code.
| -rw-r--r-- | openglscene.cpp | 36 | ||||
| -rw-r--r-- | openglscene.h | 9 |
2 files changed, 34 insertions, 11 deletions
diff --git a/openglscene.cpp b/openglscene.cpp index badc1ff..05e8ca3 100644 --- a/openglscene.cpp +++ b/openglscene.cpp @@ -35,6 +35,7 @@ private: QRgb m_modelColor; QRgb m_backgroundColor; + QDir m_dir; }; Controls::Controls(OpenGLScene *scene) @@ -42,14 +43,14 @@ Controls::Controls(OpenGLScene *scene) , m_models(new QComboBox) , m_modelColor(qRgb(180, 100, 255)) , m_backgroundColor(qRgb(0, 0, 0)) + , m_dir("models") { QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(new QLabel("Model:")); - QDir dir("models"); - dir.setNameFilters(QStringList() << "*.obj"); - m_models->addItems(dir.entryList()); + m_dir.setNameFilters(QStringList() << "*.obj"); + m_models->addItems(m_dir.entryList()); connect(m_models, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(loadModel(const QString &))); #ifndef QT_NO_CONCURRENT connect(&m_modelLoader, SIGNAL(finished()), this, SLOT(modelLoaded())); @@ -95,7 +96,7 @@ Controls::Controls(OpenGLScene *scene) Model *loadModel(const QString &filename) { - return new Model(QString("models/") + filename); + return new Model(filename); } void Controls::loadModel(const QString &filename) @@ -103,7 +104,7 @@ void Controls::loadModel(const QString &filename) m_models->setEnabled(false); QApplication::setOverrideCursor(Qt::BusyCursor); #ifndef QT_NO_CONCURRENT - m_modelLoader.setFuture(QtConcurrent::run(::loadModel, filename)); + m_modelLoader.setFuture(QtConcurrent::run(::loadModel, m_dir.filePath(filename))); #else m_scene->setModel(::loadModel(filename)); modelLoaded(); @@ -140,10 +141,11 @@ OpenGLScene::OpenGLScene() , m_normalsEnabled(false) , m_autoRotate(true) , m_rotating(false) - , m_axis(0.5f, 1.0f, 0.1f) - , m_angle(0.2f) + , m_axis(0.0f, 1.0f, 0.0f) + , m_angle(20.0f) , m_distance(1.5f) , m_model(0) + , m_lastTime(0) { // set identity matrix for (int i = 0; i < 4; ++i) @@ -156,9 +158,11 @@ OpenGLScene::OpenGLScene() QGraphicsProxyWidget *item = addWidget(controls); item->translate(10, 10); item->setCacheMode(QGraphicsItem::DeviceCoordinateCache); + + m_time.start(); } -void OpenGLScene::updateMatrix() +void OpenGLScene::updateMatrix(qreal delta) { if (!QGLContext::currentContext()) return; @@ -166,7 +170,7 @@ void OpenGLScene::updateMatrix() glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); - glRotatef(m_angle, m_axis.x, m_axis.y, m_axis.z); + glRotatef(m_angle * delta, m_axis.x, m_axis.y, m_axis.z); glMultMatrixf(&m_matrix[0][0]); glGetFloatv(GL_MODELVIEW_MATRIX, &m_matrix[0][0]); glPopMatrix(); @@ -194,8 +198,12 @@ void OpenGLScene::drawBackground(QPainter *painter, const QRectF &) glLightfv(GL_LIGHT0, GL_POSITION, pos); glColor4f(qRed(m_modelColor)/255.0f, qGreen(m_modelColor)/255.0f, qBlue(m_modelColor)/255.0f, 1.0f); + unsigned int current = m_time.elapsed(); + unsigned int delta = current - m_lastTime; + m_lastTime = current; + if (m_autoRotate && !m_rotating) - updateMatrix(); + updateMatrix(delta / 1000.0); glLoadIdentity(); glTranslatef(0, 0, -m_distance); @@ -294,6 +302,8 @@ void OpenGLScene::updateRotation(const QPointF &last, const QPointF ¤t) m_axis.y /= length; m_axis.z /= length; + m_accumulated += m_angle; + updateMatrix(); update(); } @@ -315,6 +325,9 @@ void OpenGLScene::mousePressEvent(QGraphicsSceneMouseEvent *event) if (event->isAccepted()) return; + m_startTime = m_time.elapsed(); + m_accumulated = 0; + event->accept(); m_rotating = true; } @@ -325,6 +338,9 @@ void OpenGLScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) if (event->isAccepted()) return; + const unsigned int delta = m_time.elapsed() - m_startTime; + m_angle = m_accumulated / (delta / 1000.0); + event->accept(); m_rotating = false; } diff --git a/openglscene.h b/openglscene.h index 7c2c9c4..5be1c53 100644 --- a/openglscene.h +++ b/openglscene.h @@ -4,6 +4,7 @@ #include "point3d.h" #include <QGraphicsScene> +#include <QTime> class Model; @@ -32,7 +33,7 @@ protected: void wheelEvent(QGraphicsSceneWheelEvent * wheelEvent); private: - void updateMatrix(); + void updateMatrix(qreal delta = 1); void updateRotation(const QPointF &last, const QPointF ¤t); bool m_wireframeEnabled; @@ -50,6 +51,12 @@ private: Model *m_model; + QTime m_time; + unsigned int m_lastTime; + unsigned int m_startTime; + + qreal m_accumulated; + float m_matrix[4][4]; }; |
