3
* (c) Copyright 1993, 1994, Silicon Graphics, Inc.
5
* Permission to use, copy, modify, and distribute this software for
6
* any purpose and without fee is hereby granted, provided that the above
7
* copyright notice appear in all copies and that both the copyright notice
8
* and this permission notice appear in supporting documentation, and that
9
* the name of Silicon Graphics, Inc. not be used in advertising
10
* or publicity pertaining to distribution of the software without specific,
11
* written prior permission.
13
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
14
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
19
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
20
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
21
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
22
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
23
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
24
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
26
* US Government Users Restricted Rights
27
* Use, duplication, or disclosure by the Government is subject to
28
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
30
* clause at DFARS 252.227-7013 and/or in similar or successor
31
* clauses in the FAR or the DOD or NASA FAR Supplement.
32
* Unpublished-- rights reserved under the copyright laws of the
33
* United States. Contractor/manufacturer is Silicon Graphics,
34
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
36
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
41
* Implementation of a virtual trackball.
42
* Implemented by Gavin Bell, lots of ideas from Thant Tessman and
43
* the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
48
* David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
50
* Much mucking with by:
54
#pragma warning (disable:4244) /* disable bogus conversion warnings */
57
#include "trackball.h"
60
* This size should really be based on the distance from the center of
61
* rotation to the point on the object underneath the mouse. That
62
* point would then track the mouse as closely as possible. This is a
63
* simple example, though, so that is left as an Exercise for the
66
#define TRACKBALLSIZE (0.8f)
69
* Local function prototypes (not defined in trackball.h)
71
static float tb_project_to_sphere(float, float, float);
72
static void normalize_quat(float [4]);
83
vset(float v[3], float x, float y, float z)
91
vsub(const float src1[3], const float src2[3], float dst[3])
93
dst[0] = src1[0] - src2[0];
94
dst[1] = src1[1] - src2[1];
95
dst[2] = src1[2] - src2[2];
99
vcopy(const float v1[3], float v2[3])
102
for (i = 0 ; i < 3 ; i++)
107
vcross(const float v1[3], const float v2[3], float cross[3])
111
temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
112
temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
113
temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
118
vlength(const float v[3])
120
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
124
vscale(float v[3], float div)
134
vscale(v,1.0/vlength(v));
138
vdot(const float v1[3], const float v2[3])
140
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
144
vadd(const float src1[3], const float src2[3], float dst[3])
146
dst[0] = src1[0] + src2[0];
147
dst[1] = src1[1] + src2[1];
148
dst[2] = src1[2] + src2[2];
152
* Ok, simulate a track-ball. Project the points onto the virtual
153
* trackball, then figure out the axis of rotation, which is the cross
154
* product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
155
* Note: This is a deformed trackball-- is a trackball in the center,
156
* but is deformed into a hyperbolic sheet of rotation away from the
157
* center. This particular function was chosen after trying out
158
* several variations.
160
* It is assumed that the arguments to this routine are in the range
164
trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
166
float a[3]; /* Axis of rotation */
167
float phi; /* how much to rotate about axis */
168
float p1[3], p2[3], d[3];
171
if (p1x == p2x && p1y == p2y) {
179
* First, figure out z-coordinates for projection of P1 and P2 to
182
vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
183
vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
186
* Now, we want the cross product of P1 and P2
191
* Figure out how much to rotate around that axis.
194
t = vlength(d) / (2.0*TRACKBALLSIZE);
197
* Avoid problems with out-of-control values...
199
if (t > 1.0) t = 1.0;
200
if (t < -1.0) t = -1.0;
203
axis_to_quat(a,phi,q);
207
* Given an axis and angle, compute quaternion.
210
axis_to_quat(const float a[3], float phi, float q[4])
214
vscale(q, sin(phi/2.0));
219
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
220
* if we are away from the center of the sphere.
223
tb_project_to_sphere(float r, float x, float y)
228
if (d < r * 0.70710678118654752440) { /* Inside sphere */
230
} else { /* On hyperbola */
231
t = r / 1.41421356237309504880;
238
* Given two rotations, e1 and e2, expressed as quaternion rotations,
239
* figure out the equivalent single rotation and stuff it into dest.
241
* This routine also normalizes the result every RENORMCOUNT times it is
242
* called, to keep error from creeping in.
244
* NOTE: This routine is written so that q1 or q2 may be the same
245
* as dest (or each other).
248
#define RENORMCOUNT 97
251
add_quats(const float q1[4], const float q2[4], float dest[4])
254
float t1[4], t2[4], t3[4];
258
printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
259
printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
271
tf[3] = q1[3] * q2[3] - vdot(q1,q2);
274
printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
282
if (++count > RENORMCOUNT) {
284
normalize_quat(dest);
289
* Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
290
* If they don't add up to 1.0, dividing by their magnitued will
293
* Note: See the following for more information on quaternions:
295
* - Shoemake, K., Animating rotation with quaternion curves, Computer
296
* Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
297
* - Pletinckx, D., Quaternion calculus as a basic tool in computer
298
* graphics, The Visual Computer 5, 2-13, 1989.
301
normalize_quat(float q[4])
306
mag = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
307
for (i = 0; i < 4; i++)
312
* Build a rotation matrix, given a quaternion rotation.
316
build_rotmatrix(float m[4][4], const float q[4])
318
m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
319
m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
320
m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
323
m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
324
m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
325
m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
328
m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
329
m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
330
m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);