4 * Implementation of a virtual trackball.
5 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
6 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
11 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
13 * Much mucking with by:
17 #if defined(_WIN32) && !defined( __CYGWIN32__ )
18 #pragma warning (disable:4244) /* disable bogus conversion warnings */
22 #include "trackball.h"
25 * This size should really be based on the distance from the center of
26 * rotation to the point on the object underneath the mouse. That
27 * point would then track the mouse as closely as possible. This is a
28 * simple example, though, so that is left as an Exercise for the
31 #define TRACKBALLSIZE (0.8f)
32 #define SQRT(x) sqrt(x)
35 * Local function prototypes (not defined in trackball.h)
37 static float tb_project_to_sphere(float, float, float);
38 static void normalize_quat(float [4]);
49 vset(float *v, float x, float y, float z)
57 vsub(const float *src1, const float *src2, float *dst)
59 dst[0] = src1[0] - src2[0];
60 dst[1] = src1[1] - src2[1];
61 dst[2] = src1[2] - src2[2];
65 vcopy(const float *v1, float *v2)
68 for (i = 0 ; i < 3 ; i++)
73 vcross(const float *v1, const float *v2, float *cross)
77 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
78 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
79 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
84 vlength(const float *v)
86 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
91 vscale(float *v, float div)
101 vscale(v,1.0/vlength(v));
105 vdot(const float *v1, const float *v2)
107 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
111 vadd(const float *src1, const float *src2, float *dst)
113 dst[0] = src1[0] + src2[0];
114 dst[1] = src1[1] + src2[1];
115 dst[2] = src1[2] + src2[2];
119 * Given an axis and angle, compute quaternion.
122 axis_to_quat(float a[3], float phi, float q[4])
124 double sinphi2, cosphi2;
125 double phi2 = phi/2.0;
135 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
136 * if we are away from the center of the sphere.
139 tb_project_to_sphere(float r, float x, float y)
145 if (d < r * 0.70710678118654752440) { /* Inside sphere */
148 } else { /* On hyperbola */
149 t = r / 1.41421356237309504880;
156 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
157 * If they don't add up to 1.0, dividing by their magnitued will
160 * Note: See the following for more information on quaternions:
162 * - Shoemake, K., Animating rotation with quaternion curves, Computer
163 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
164 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
165 * graphics, The Visual Computer 5, 2-13, 1989.
168 normalize_quat(float q[4])
173 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
174 mag = 1.0 / SQRT(tmp);
175 for (i = 0; i < 4; i++)
180 * Ok, simulate a track-ball. Project the points onto the virtual
181 * trackball, then figure out the axis of rotation, which is the cross
182 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
183 * Note: This is a deformed trackball-- is a trackball in the center,
184 * but is deformed into a hyperbolic sheet of rotation away from the
185 * center. This particular function was chosen after trying out
186 * several variations.
188 * It is assumed that the arguments to this routine are in the range
192 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
194 float a[3]; /* Axis of rotation */
195 float phi; /* how much to rotate about axis */
196 float p1[3], p2[3], d[3];
199 if (p1x == p2x && p1y == p2y) {
207 * First, figure out z-coordinates for projection of P1 and P2 to
210 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
211 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
214 * Now, we want the cross product of P1 and P2
219 * Figure out how much to rotate around that axis.
222 t = vlength(d) / (2.0*TRACKBALLSIZE);
225 * Avoid problems with out-of-control values...
227 if (t > 1.0) t = 1.0;
228 if (t < -1.0) t = -1.0;
231 axis_to_quat(a,phi,q);
235 * Given two rotations, e1 and e2, expressed as quaternion rotations,
236 * figure out the equivalent single rotation and stuff it into dest.
238 * This routine also normalizes the result every RENORMCOUNT times it is
239 * called, to keep error from creeping in.
241 * NOTE: This routine is written so that q1 or q2 may be the same
242 * as dest (or each other).
245 #define RENORMCOUNT 97
248 add_quats(float q1[4], float q2[4], float dest[4])
251 float t1[4], t2[4], t3[4];
255 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
256 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
268 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
271 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
279 if (++count > RENORMCOUNT) {
281 normalize_quat(dest);
286 * Build a rotation matrix, given a quaternion rotation.
289 void build_rotmatrix(float m[4][4], float q[4])
291 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
292 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
293 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
296 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
297 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
298 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
301 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
302 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
303 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
312 void build_transposed_rotmatrix(float m[4][4], float q[4])
314 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
315 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
316 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
319 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
320 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
321 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
324 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
325 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
326 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);