2 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
\r
3 * ALL RIGHTS RESERVED
\r
4 * Permission to use, copy, modify, and distribute this software for
\r
5 * any purpose and without fee is hereby granted, provided that the above
\r
6 * copyright notice appear in all copies and that both the copyright notice
\r
7 * and this permission notice appear in supporting documentation, and that
\r
8 * the name of Silicon Graphics, Inc. not be used in advertising
\r
9 * or publicity pertaining to distribution of the software without specific,
\r
10 * written prior permission.
\r
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
\r
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
\r
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
\r
15 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
\r
16 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
\r
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
\r
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
\r
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
\r
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
\r
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
\r
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
\r
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
\r
25 * US Government Users Restricted Rights
\r
26 * Use, duplication, or disclosure by the Government is subject to
\r
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
\r
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
\r
29 * clause at DFARS 252.227-7013 and/or in similar or successor
\r
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
\r
31 * Unpublished-- rights reserved under the copyright laws of the
\r
32 * United States. Contractor/manufacturer is Silicon Graphics,
\r
33 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
\r
35 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
\r
40 * Implementation of a virtual trackball.
\r
41 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
\r
42 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
\r
44 * Vector manip code:
\r
46 * Original code from:
\r
47 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
\r
49 * Much mucking with by:
\r
53 #pragma warning (disable:4244) /* disable bogus conversion warnings */
\r
56 #include "trackball.h"
\r
59 * This size should really be based on the distance from the center of
\r
60 * rotation to the point on the object underneath the mouse. That
\r
61 * point would then track the mouse as closely as possible. This is a
\r
62 * simple example, though, so that is left as an Exercise for the
\r
65 #define TRACKBALLSIZE (0.8)
\r
68 * Local function prototypes (not defined in trackball.h)
\r
70 static float tb_project_to_sphere(float, float, float);
\r
71 static void normalize_quat(float [4]);
\r
82 vset(float *v, float x, float y, float z)
\r
90 vsub(const float *src1, const float *src2, float *dst)
\r
92 dst[0] = src1[0] - src2[0];
\r
93 dst[1] = src1[1] - src2[1];
\r
94 dst[2] = src1[2] - src2[2];
\r
98 vcopy(const float *v1, float *v2)
\r
101 for (i = 0 ; i < 3 ; i++)
\r
106 vcross(const float *v1, const float *v2, float *cross)
\r
110 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
\r
111 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
\r
112 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
\r
113 vcopy(temp, cross);
\r
117 vlength(const float *v)
\r
119 return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
\r
123 vscale(float *v, float div)
\r
133 vscale(v,1.0/vlength(v));
\r
137 vdot(const float *v1, const float *v2)
\r
139 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
\r
143 vadd(const float *src1, const float *src2, float *dst)
\r
145 dst[0] = src1[0] + src2[0];
\r
146 dst[1] = src1[1] + src2[1];
\r
147 dst[2] = src1[2] + src2[2];
\r
151 * Ok, simulate a track-ball. Project the points onto the virtual
\r
152 * trackball, then figure out the axis of rotation, which is the cross
\r
153 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
\r
154 * Note: This is a deformed trackball-- is a trackball in the center,
\r
155 * but is deformed into a hyperbolic sheet of rotation away from the
\r
156 * center. This particular function was chosen after trying out
\r
157 * several variations.
\r
159 * It is assumed that the arguments to this routine are in the range
\r
163 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
\r
165 float a[3]; /* Axis of rotation */
\r
166 float phi; /* how much to rotate about axis */
\r
167 float p1[3], p2[3], d[3];
\r
170 if (p1x == p2x && p1y == p2y) {
\r
171 /* Zero rotation */
\r
178 * First, figure out z-coordinates for projection of P1 and P2 to
\r
181 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
\r
182 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
\r
185 * Now, we want the cross product of P1 and P2
\r
190 * Figure out how much to rotate around that axis.
\r
193 t = vlength(d) / (2.0*TRACKBALLSIZE);
\r
196 * Avoid problems with out-of-control values...
\r
198 if (t > 1.0) t = 1.0;
\r
199 if (t < -1.0) t = -1.0;
\r
200 phi = 2.0 * asin(t);
\r
202 axis_to_quat(a,phi,q);
\r
206 * Given an axis and angle, compute quaternion.
\r
209 axis_to_quat(float a[3], float phi, float q[4])
\r
213 vscale(q,sin(phi/2.0));
\r
214 q[3] = cos(phi/2.0);
\r
218 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
\r
219 * if we are away from the center of the sphere.
\r
222 tb_project_to_sphere(float r, float x, float y)
\r
226 d = sqrt(x*x + y*y);
\r
227 if (d < r * 0.70710678118654752440) { /* Inside sphere */
\r
228 z = sqrt(r*r - d*d);
\r
229 } else { /* On hyperbola */
\r
230 t = r / 1.41421356237309504880;
\r
237 * Given two rotations, e1 and e2, expressed as quaternion rotations,
\r
238 * figure out the equivalent single rotation and stuff it into dest.
\r
240 * This routine also normalizes the result every RENORMCOUNT times it is
\r
241 * called, to keep error from creeping in.
\r
243 * NOTE: This routine is written so that q1 or q2 may be the same
\r
244 * as dest (or each other).
\r
247 #define RENORMCOUNT 97
\r
250 add_quats(float q1[4], float q2[4], float dest[4])
\r
252 static int count=0;
\r
253 float t1[4], t2[4], t3[4];
\r
265 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
\r
272 if (++count > RENORMCOUNT) {
\r
274 normalize_quat(dest);
\r
279 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
\r
280 * If they don't add up to 1.0, dividing by their magnitued will
\r
281 * renormalize them.
\r
283 * Note: See the following for more information on quaternions:
\r
285 * - Shoemake, K., Animating rotation with quaternion curves, Computer
\r
286 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
\r
287 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
\r
288 * graphics, The Visual Computer 5, 2-13, 1989.
\r
291 normalize_quat(float q[4])
\r
296 mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
\r
297 for (i = 0; i < 4; i++) q[i] /= mag;
\r
301 * Build a rotation matrix, given a quaternion rotation.
\r
305 build_rotmatrix(float m[4][4], float q[4])
\r
307 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
\r
308 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
\r
309 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
\r
312 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
\r
313 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
\r
314 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
\r
317 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
\r
318 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
\r
319 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
\r