Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/demos/boxes/trackball.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the demonstration applications of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    4141
    4242#include "trackball.h"
     43#include "scene.h"
    4344
    4445//============================================================================//
     
    5253    , m_mode(mode)
    5354{
    54     m_axis = gfx::Vector3f::vector(0, 1, 0);
    55     m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);
     55    m_axis = QVector3D(0, 1, 0);
     56    m_rotation = QQuaternion();
    5657    m_lastTime = QTime::currentTime();
    5758}
    5859
    59 TrackBall::TrackBall(float angularVelocity, const gfx::Vector3f& axis, TrackMode mode)
     60TrackBall::TrackBall(float angularVelocity, const QVector3D& axis, TrackMode mode)
    6061    : m_axis(axis)
    6162    , m_angularVelocity(angularVelocity)
     
    6465    , m_mode(mode)
    6566{
    66     m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);
     67    m_rotation = QQuaternion();
    6768    m_lastTime = QTime::currentTime();
    6869}
    6970
    70 void TrackBall::push(const QPointF& p, const gfx::Quaternionf &)
     71void TrackBall::push(const QPointF& p, const QQuaternion &)
    7172{
    7273    m_rotation = rotation();
     
    7778}
    7879
    79 void TrackBall::move(const QPointF& p, const gfx::Quaternionf &transformation)
     80void TrackBall::move(const QPointF& p, const QQuaternion &transformation)
    8081{
    8182    if (!m_pressed)
     
    9192        {
    9293            QLineF delta(m_lastPos, p);
    93             m_angularVelocity = delta.length() / msecs;
    94             m_axis = gfx::Vector3f::vector(delta.dy(), -delta.dx(), 0.0f).normalized();
    95             m_axis = transformation.transform(m_axis);
    96             m_rotation *= gfx::Quaternionf::rotation(delta.length(), m_axis);
     94            m_angularVelocity = 180*delta.length() / (PI*msecs);
     95            m_axis = QVector3D(-delta.dy(), delta.dx(), 0.0f).normalized();
     96            m_axis = transformation.rotatedVector(m_axis);
     97            m_rotation = QQuaternion::fromAxisAndAngle(m_axis, 180 / PI * delta.length()) * m_rotation;
    9798        }
    9899        break;
    99100    case Sphere:
    100101        {
    101             gfx::Vector3f lastPos3D = gfx::Vector3f::vector(m_lastPos.x(), m_lastPos.y(), 0);
    102             float sqrZ = 1 - lastPos3D.sqrNorm();
     102            QVector3D lastPos3D = QVector3D(m_lastPos.x(), m_lastPos.y(), 0.0f);
     103            float sqrZ = 1 - QVector3D::dotProduct(lastPos3D, lastPos3D);
    103104            if (sqrZ > 0)
    104                 lastPos3D[2] = sqrt(sqrZ);
     105                lastPos3D.setZ(sqrt(sqrZ));
    105106            else
    106107                lastPos3D.normalize();
    107108
    108             gfx::Vector3f currentPos3D = gfx::Vector3f::vector(p.x(), p.y(), 0);
    109             sqrZ = 1 - currentPos3D.sqrNorm();
     109            QVector3D currentPos3D = QVector3D(p.x(), p.y(), 0.0f);
     110            sqrZ = 1 - QVector3D::dotProduct(currentPos3D, currentPos3D);
    110111            if (sqrZ > 0)
    111                 currentPos3D[2] = sqrt(sqrZ);
     112                currentPos3D.setZ(sqrt(sqrZ));
    112113            else
    113114                currentPos3D.normalize();
    114115
    115             m_axis = gfx::Vector3f::cross(currentPos3D, lastPos3D);
    116             float angle = asin(sqrt(m_axis.sqrNorm()));
     116            m_axis = QVector3D::crossProduct(lastPos3D, currentPos3D);
     117            float angle = 180 / PI * asin(sqrt(QVector3D::dotProduct(m_axis, m_axis)));
    117118
    118119            m_angularVelocity = angle / msecs;
    119120            m_axis.normalize();
    120             m_axis = transformation.transform(m_axis);
    121             m_rotation *= gfx::Quaternionf::rotation(angle, m_axis);
     121            m_axis = transformation.rotatedVector(m_axis);
     122            m_rotation = QQuaternion::fromAxisAndAngle(m_axis, angle) * m_rotation;
    122123        }
    123124        break;
    124125    }
     126
    125127
    126128    m_lastPos = p;
     
    128130}
    129131
    130 void TrackBall::release(const QPointF& p, const gfx::Quaternionf &transformation)
     132void TrackBall::release(const QPointF& p, const QQuaternion &transformation)
    131133{
    132134    // Calling move() caused the rotation to stop if the framerate was too low.
     
    147149}
    148150
    149 gfx::Quaternionf TrackBall::rotation() const
     151QQuaternion TrackBall::rotation() const
    150152{
    151153    if (m_paused || m_pressed)
     
    154156    QTime currentTime = QTime::currentTime();
    155157    float angle = m_angularVelocity * m_lastTime.msecsTo(currentTime);
    156     return m_rotation * gfx::Quaternionf::rotation(angle, m_axis);
     158    return QQuaternion::fromAxisAndAngle(m_axis, angle) * m_rotation;
    157159}
    158160
Note: See TracChangeset for help on using the changeset viewer.