+++ /dev/null
-/*\r
- * (c) Copyright 1993, 1994, Silicon Graphics, Inc.\r
- * ALL RIGHTS RESERVED\r
- * Permission to use, copy, modify, and distribute this software for\r
- * any purpose and without fee is hereby granted, provided that the above\r
- * copyright notice appear in all copies and that both the copyright notice\r
- * and this permission notice appear in supporting documentation, and that\r
- * the name of Silicon Graphics, Inc. not be used in advertising\r
- * or publicity pertaining to distribution of the software without specific,\r
- * written prior permission.\r
- *\r
- * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"\r
- * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,\r
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR\r
- * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON\r
- * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,\r
- * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY\r
- * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,\r
- * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF\r
- * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN\r
- * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON\r
- * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE\r
- * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.\r
- *\r
- * US Government Users Restricted Rights\r
- * Use, duplication, or disclosure by the Government is subject to\r
- * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph\r
- * (c)(1)(ii) of the Rights in Technical Data and Computer Software\r
- * clause at DFARS 252.227-7013 and/or in similar or successor\r
- * clauses in the FAR or the DOD or NASA FAR Supplement.\r
- * Unpublished-- rights reserved under the copyright laws of the\r
- * United States. Contractor/manufacturer is Silicon Graphics,\r
- * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.\r
- *\r
- * OpenGL(TM) is a trademark of Silicon Graphics, Inc.\r
- */\r
-/*\r
- * Trackball code:\r
- *\r
- * Implementation of a virtual trackball.\r
- * Implemented by Gavin Bell, lots of ideas from Thant Tessman and\r
- * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.\r
- *\r
- * Vector manip code:\r
- *\r
- * Original code from:\r
- * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli\r
- *\r
- * Much mucking with by:\r
- * Gavin Bell\r
- */\r
-#if defined(WIN32)\r
-#pragma warning (disable:4244) /* disable bogus conversion warnings */\r
-#endif\r
-#include <math.h>\r
-#include "trackball.h"\r
-\r
-/*\r
- * This size should really be based on the distance from the center of\r
- * rotation to the point on the object underneath the mouse. That\r
- * point would then track the mouse as closely as possible. This is a\r
- * simple example, though, so that is left as an Exercise for the\r
- * Programmer.\r
- */\r
-#define TRACKBALLSIZE (0.8)\r
-\r
-/*\r
- * Local function prototypes (not defined in trackball.h)\r
- */\r
-static float tb_project_to_sphere(float, float, float);\r
-static void normalize_quat(float [4]);\r
-\r
-void\r
-vzero(float *v)\r
-{\r
- v[0] = 0.0;\r
- v[1] = 0.0;\r
- v[2] = 0.0;\r
-}\r
-\r
-void\r
-vset(float *v, float x, float y, float z)\r
-{\r
- v[0] = x;\r
- v[1] = y;\r
- v[2] = z;\r
-}\r
-\r
-void\r
-vsub(const float *src1, const float *src2, float *dst)\r
-{\r
- dst[0] = src1[0] - src2[0];\r
- dst[1] = src1[1] - src2[1];\r
- dst[2] = src1[2] - src2[2];\r
-}\r
-\r
-void\r
-vcopy(const float *v1, float *v2)\r
-{\r
- register int i;\r
- for (i = 0 ; i < 3 ; i++)\r
- v2[i] = v1[i];\r
-}\r
-\r
-void\r
-vcross(const float *v1, const float *v2, float *cross)\r
-{\r
- float temp[3];\r
-\r
- temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);\r
- temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);\r
- temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);\r
- vcopy(temp, cross);\r
-}\r
-\r
-float\r
-vlength(const float *v)\r
-{\r
- return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);\r
-}\r
-\r
-void\r
-vscale(float *v, float div)\r
-{\r
- v[0] *= div;\r
- v[1] *= div;\r
- v[2] *= div;\r
-}\r
-\r
-void\r
-vnormal(float *v)\r
-{\r
- vscale(v,1.0/vlength(v));\r
-}\r
-\r
-float\r
-vdot(const float *v1, const float *v2)\r
-{\r
- return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];\r
-}\r
-\r
-void\r
-vadd(const float *src1, const float *src2, float *dst)\r
-{\r
- dst[0] = src1[0] + src2[0];\r
- dst[1] = src1[1] + src2[1];\r
- dst[2] = src1[2] + src2[2];\r
-}\r
-\r
-/*\r
- * Ok, simulate a track-ball. Project the points onto the virtual\r
- * trackball, then figure out the axis of rotation, which is the cross\r
- * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)\r
- * Note: This is a deformed trackball-- is a trackball in the center,\r
- * but is deformed into a hyperbolic sheet of rotation away from the\r
- * center. This particular function was chosen after trying out\r
- * several variations.\r
- *\r
- * It is assumed that the arguments to this routine are in the range\r
- * (-1.0 ... 1.0)\r
- */\r
-void\r
-trackball(float q[4], float p1x, float p1y, float p2x, float p2y)\r
-{\r
- float a[3]; /* Axis of rotation */\r
- float phi; /* how much to rotate about axis */\r
- float p1[3], p2[3], d[3];\r
- float t;\r
-\r
- if (p1x == p2x && p1y == p2y) {\r
- /* Zero rotation */\r
- vzero(q);\r
- q[3] = 1.0;\r
- return;\r
- }\r
-\r
- /*\r
- * First, figure out z-coordinates for projection of P1 and P2 to\r
- * deformed sphere\r
- */\r
- vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));\r
- vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));\r
-\r
- /*\r
- * Now, we want the cross product of P1 and P2\r
- */\r
- vcross(p2,p1,a);\r
-\r
- /*\r
- * Figure out how much to rotate around that axis.\r
- */\r
- vsub(p1,p2,d);\r
- t = vlength(d) / (2.0*TRACKBALLSIZE);\r
-\r
- /*\r
- * Avoid problems with out-of-control values...\r
- */\r
- if (t > 1.0) t = 1.0;\r
- if (t < -1.0) t = -1.0;\r
- phi = 2.0 * asin(t);\r
-\r
- axis_to_quat(a,phi,q);\r
-}\r
-\r
-/*\r
- * Given an axis and angle, compute quaternion.\r
- */\r
-void\r
-axis_to_quat(float a[3], float phi, float q[4])\r
-{\r
- vnormal(a);\r
- vcopy(a,q);\r
- vscale(q,sin(phi/2.0));\r
- q[3] = cos(phi/2.0);\r
-}\r
-\r
-/*\r
- * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet\r
- * if we are away from the center of the sphere.\r
- */\r
-static float\r
-tb_project_to_sphere(float r, float x, float y)\r
-{\r
- float d, t, z;\r
-\r
- d = sqrt(x*x + y*y);\r
- if (d < r * 0.70710678118654752440) { /* Inside sphere */\r
- z = sqrt(r*r - d*d);\r
- } else { /* On hyperbola */\r
- t = r / 1.41421356237309504880;\r
- z = t*t / d;\r
- }\r
- return z;\r
-}\r
-\r
-/*\r
- * Given two rotations, e1 and e2, expressed as quaternion rotations,\r
- * figure out the equivalent single rotation and stuff it into dest.\r
- *\r
- * This routine also normalizes the result every RENORMCOUNT times it is\r
- * called, to keep error from creeping in.\r
- *\r
- * NOTE: This routine is written so that q1 or q2 may be the same\r
- * as dest (or each other).\r
- */\r
-\r
-#define RENORMCOUNT 97\r
-\r
-void\r
-add_quats(float q1[4], float q2[4], float dest[4])\r
-{\r
- static int count=0;\r
- float t1[4], t2[4], t3[4];\r
- float tf[4];\r
-\r
- vcopy(q1,t1);\r
- vscale(t1,q2[3]);\r
-\r
- vcopy(q2,t2);\r
- vscale(t2,q1[3]);\r
-\r
- vcross(q2,q1,t3);\r
- vadd(t1,t2,tf);\r
- vadd(t3,tf,tf);\r
- tf[3] = q1[3] * q2[3] - vdot(q1,q2);\r
-\r
- dest[0] = tf[0];\r
- dest[1] = tf[1];\r
- dest[2] = tf[2];\r
- dest[3] = tf[3];\r
-\r
- if (++count > RENORMCOUNT) {\r
- count = 0;\r
- normalize_quat(dest);\r
- }\r
-}\r
-\r
-/*\r
- * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0\r
- * If they don't add up to 1.0, dividing by their magnitued will\r
- * renormalize them.\r
- *\r
- * Note: See the following for more information on quaternions:\r
- *\r
- * - Shoemake, K., Animating rotation with quaternion curves, Computer\r
- * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.\r
- * - Pletinckx, D., Quaternion calculus as a basic tool in computer\r
- * graphics, The Visual Computer 5, 2-13, 1989.\r
- */\r
-static void\r
-normalize_quat(float q[4])\r
-{\r
- int i;\r
- float mag;\r
-\r
- mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);\r
- for (i = 0; i < 4; i++) q[i] /= mag;\r
-}\r
-\r
-/*\r
- * Build a rotation matrix, given a quaternion rotation.\r
- *\r
- */\r
-void\r
-build_rotmatrix(float m[4][4], float q[4])\r
-{\r
- m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);\r
- m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);\r
- m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);\r
- m[0][3] = 0.0;\r
-\r
- m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);\r
- m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);\r
- m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);\r
- m[1][3] = 0.0;\r
-\r
- m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);\r
- m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);\r
- m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);\r
- m[2][3] = 0.0;\r
-\r
- m[3][0] = 0.0;\r
- m[3][1] = 0.0;\r
- m[3][2] = 0.0;\r
- m[3][3] = 1.0;\r
-}\r
-\r