source: trunk/demos/boxes/trackball.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
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.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "trackball.h"
43
44//============================================================================//
45// TrackBall //
46//============================================================================//
47
48TrackBall::TrackBall(TrackMode mode)
49 : m_angularVelocity(0)
50 , m_paused(false)
51 , m_pressed(false)
52 , m_mode(mode)
53{
54 m_axis = gfx::Vector3f::vector(0, 1, 0);
55 m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);
56 m_lastTime = QTime::currentTime();
57}
58
59TrackBall::TrackBall(float angularVelocity, const gfx::Vector3f& axis, TrackMode mode)
60 : m_axis(axis)
61 , m_angularVelocity(angularVelocity)
62 , m_paused(false)
63 , m_pressed(false)
64 , m_mode(mode)
65{
66 m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);
67 m_lastTime = QTime::currentTime();
68}
69
70void TrackBall::push(const QPointF& p, const gfx::Quaternionf &)
71{
72 m_rotation = rotation();
73 m_pressed = true;
74 m_lastTime = QTime::currentTime();
75 m_lastPos = p;
76 m_angularVelocity = 0.0f;
77}
78
79void TrackBall::move(const QPointF& p, const gfx::Quaternionf &transformation)
80{
81 if (!m_pressed)
82 return;
83
84 QTime currentTime = QTime::currentTime();
85 int msecs = m_lastTime.msecsTo(currentTime);
86 if (msecs <= 20)
87 return;
88
89 switch (m_mode) {
90 case Plane:
91 {
92 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);
97 }
98 break;
99 case Sphere:
100 {
101 gfx::Vector3f lastPos3D = gfx::Vector3f::vector(m_lastPos.x(), m_lastPos.y(), 0);
102 float sqrZ = 1 - lastPos3D.sqrNorm();
103 if (sqrZ > 0)
104 lastPos3D[2] = sqrt(sqrZ);
105 else
106 lastPos3D.normalize();
107
108 gfx::Vector3f currentPos3D = gfx::Vector3f::vector(p.x(), p.y(), 0);
109 sqrZ = 1 - currentPos3D.sqrNorm();
110 if (sqrZ > 0)
111 currentPos3D[2] = sqrt(sqrZ);
112 else
113 currentPos3D.normalize();
114
115 m_axis = gfx::Vector3f::cross(currentPos3D, lastPos3D);
116 float angle = asin(sqrt(m_axis.sqrNorm()));
117
118 m_angularVelocity = angle / msecs;
119 m_axis.normalize();
120 m_axis = transformation.transform(m_axis);
121 m_rotation *= gfx::Quaternionf::rotation(angle, m_axis);
122 }
123 break;
124 }
125
126 m_lastPos = p;
127 m_lastTime = currentTime;
128}
129
130void TrackBall::release(const QPointF& p, const gfx::Quaternionf &transformation)
131{
132 // Calling move() caused the rotation to stop if the framerate was too low.
133 move(p, transformation);
134 m_pressed = false;
135}
136
137void TrackBall::start()
138{
139 m_lastTime = QTime::currentTime();
140 m_paused = false;
141}
142
143void TrackBall::stop()
144{
145 m_rotation = rotation();
146 m_paused = true;
147}
148
149gfx::Quaternionf TrackBall::rotation() const
150{
151 if (m_paused || m_pressed)
152 return m_rotation;
153
154 QTime currentTime = QTime::currentTime();
155 float angle = m_angularVelocity * m_lastTime.msecsTo(currentTime);
156 return m_rotation * gfx::Quaternionf::rotation(angle, m_axis);
157}
158
Note: See TracBrowser for help on using the repository browser.