Changeset 561 for trunk/demos/boxes/trackball.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/demos/boxes/trackball.cpp
r2 r561 2 2 ** 3 3 ** 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) 5 6 ** 6 7 ** This file is part of the demonstration applications of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 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. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** 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. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 41 41 42 42 #include "trackball.h" 43 #include "scene.h" 43 44 44 45 //============================================================================// … … 52 53 , m_mode(mode) 53 54 { 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(); 56 57 m_lastTime = QTime::currentTime(); 57 58 } 58 59 59 TrackBall::TrackBall(float angularVelocity, const gfx::Vector3f& axis, TrackMode mode)60 TrackBall::TrackBall(float angularVelocity, const QVector3D& axis, TrackMode mode) 60 61 : m_axis(axis) 61 62 , m_angularVelocity(angularVelocity) … … 64 65 , m_mode(mode) 65 66 { 66 m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);67 m_rotation = QQuaternion(); 67 68 m_lastTime = QTime::currentTime(); 68 69 } 69 70 70 void TrackBall::push(const QPointF& p, const gfx::Quaternionf&)71 void TrackBall::push(const QPointF& p, const QQuaternion &) 71 72 { 72 73 m_rotation = rotation(); … … 77 78 } 78 79 79 void TrackBall::move(const QPointF& p, const gfx::Quaternionf&transformation)80 void TrackBall::move(const QPointF& p, const QQuaternion &transformation) 80 81 { 81 82 if (!m_pressed) … … 91 92 { 92 93 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; 97 98 } 98 99 break; 99 100 case Sphere: 100 101 { 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); 103 104 if (sqrZ > 0) 104 lastPos3D [2] = sqrt(sqrZ);105 lastPos3D.setZ(sqrt(sqrZ)); 105 106 else 106 107 lastPos3D.normalize(); 107 108 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); 110 111 if (sqrZ > 0) 111 currentPos3D [2] = sqrt(sqrZ);112 currentPos3D.setZ(sqrt(sqrZ)); 112 113 else 113 114 currentPos3D.normalize(); 114 115 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))); 117 118 118 119 m_angularVelocity = angle / msecs; 119 120 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; 122 123 } 123 124 break; 124 125 } 126 125 127 126 128 m_lastPos = p; … … 128 130 } 129 131 130 void TrackBall::release(const QPointF& p, const gfx::Quaternionf&transformation)132 void TrackBall::release(const QPointF& p, const QQuaternion &transformation) 131 133 { 132 134 // Calling move() caused the rotation to stop if the framerate was too low. … … 147 149 } 148 150 149 gfx::QuaternionfTrackBall::rotation() const151 QQuaternion TrackBall::rotation() const 150 152 { 151 153 if (m_paused || m_pressed) … … 154 156 QTime currentTime = QTime::currentTime(); 155 157 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; 157 159 } 158 160
Note:
See TracChangeset
for help on using the changeset viewer.