You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
8.5 KiB

18 years ago
  1. /*
  2. * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN PAD_CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. /*
  38. * Trackball code:
  39. *
  40. * Implementation of a virtual trackball.
  41. * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
  42. * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
  43. *
  44. * Vector manip code:
  45. *
  46. * Original code from:
  47. * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  48. *
  49. * Much mucking with by:
  50. * Gavin Bell
  51. */
  52. #include <math.h>
  53. #include "wx/glcanvas.h" // used only to define GLfloat
  54. #include "trackball.h"
  55. /*
  56. * This size should really be based on the distance from the center of
  57. * rotation to the point on the object underneath the mouse. That
  58. * point would then track the mouse as closely as possible. This is a
  59. * simple example, though, so that is left as an Exercise for the
  60. * Programmer.
  61. */
  62. #define TRACKBALLSIZE (0.8f)
  63. /*
  64. * Local function prototypes (not defined in trackball.h)
  65. */
  66. static double tb_project_to_sphere(double, double, double);
  67. static void normalize_quat(double [4]);
  68. void
  69. vzero(double *v)
  70. {
  71. v[0] = 0.0;
  72. v[1] = 0.0;
  73. v[2] = 0.0;
  74. }
  75. void
  76. vset(double *v, double x, double y, double z)
  77. {
  78. v[0] = x;
  79. v[1] = y;
  80. v[2] = z;
  81. }
  82. void
  83. vsub(const double *src1, const double *src2, double *dst)
  84. {
  85. dst[0] = src1[0] - src2[0];
  86. dst[1] = src1[1] - src2[1];
  87. dst[2] = src1[2] - src2[2];
  88. }
  89. void
  90. vcopy(const double *v1, double *v2)
  91. {
  92. register int i;
  93. for (i = 0 ; i < 3 ; i++)
  94. v2[i] = v1[i];
  95. }
  96. void
  97. vcross(const double *v1, const double *v2, double *cross)
  98. {
  99. double temp[3];
  100. temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  101. temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  102. temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  103. vcopy(temp, cross);
  104. }
  105. double
  106. vlength(const double *v)
  107. {
  108. return (double) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  109. }
  110. void
  111. vscale(double *v, double div)
  112. {
  113. v[0] *= div;
  114. v[1] *= div;
  115. v[2] *= div;
  116. }
  117. void
  118. vnormal(double *v)
  119. {
  120. vscale(v, 1.0f/vlength(v));
  121. }
  122. double
  123. vdot(const double *v1, const double *v2)
  124. {
  125. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  126. }
  127. void
  128. vadd(const double *src1, const double *src2, double *dst)
  129. {
  130. dst[0] = src1[0] + src2[0];
  131. dst[1] = src1[1] + src2[1];
  132. dst[2] = src1[2] + src2[2];
  133. }
  134. /*
  135. * Ok, simulate a track-ball. Project the points onto the virtual
  136. * trackball, then figure out the axis of rotation, which is the cross
  137. * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  138. * Note: This is a deformed trackball-- is a trackball in the center,
  139. * but is deformed into a hyperbolic sheet of rotation away from the
  140. * center. This particular function was chosen after trying out
  141. * several variations.
  142. *
  143. * It is assumed that the arguments to this routine are in the range
  144. * (-1.0 ... 1.0)
  145. */
  146. void
  147. trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
  148. {
  149. double a[3]; /* Axis of rotation */
  150. double phi; /* how much to rotate about axis */
  151. double p1[3], p2[3], d[3];
  152. double t;
  153. if (p1x == p2x && p1y == p2y) {
  154. /* Zero rotation */
  155. vzero(q);
  156. q[3] = 1.0;
  157. return;
  158. }
  159. /*
  160. * First, figure out z-coordinates for projection of P1 and P2 to
  161. * deformed sphere
  162. */
  163. vset(p1, p1x, p1y, tb_project_to_sphere(TRACKBALLSIZE, p1x, p1y));
  164. vset(p2, p2x, p2y, tb_project_to_sphere(TRACKBALLSIZE, p2x, p2y));
  165. /*
  166. * Now, we want the cross product of P1 and P2
  167. */
  168. vcross(p2,p1,a);
  169. /*
  170. * Figure out how much to rotate around that axis.
  171. */
  172. vsub(p1, p2, d);
  173. t = vlength(d) / (2.0f*TRACKBALLSIZE);
  174. /*
  175. * Avoid problems with out-of-control values...
  176. */
  177. if (t > 1.0) t = 1.0;
  178. if (t < -1.0) t = -1.0;
  179. phi = 2.0f * (double) asin(t);
  180. axis_to_quat(a,phi,q);
  181. }
  182. /*
  183. * Given an axis and angle, compute quaternion.
  184. */
  185. void
  186. axis_to_quat(double a[3], double phi, double q[4])
  187. {
  188. vnormal(a);
  189. vcopy(a, q);
  190. vscale(q, (double) sin(phi/2.0));
  191. q[3] = (double) cos(phi/2.0);
  192. }
  193. /*
  194. * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
  195. * if we are away from the center of the sphere.
  196. */
  197. static double
  198. tb_project_to_sphere(double r, double x, double y)
  199. {
  200. double d, t, z;
  201. d = (double) sqrt(x*x + y*y);
  202. if (d < r * 0.70710678118654752440) { /* Inside sphere */
  203. z = (double) sqrt(r*r - d*d);
  204. } else { /* On hyperbola */
  205. t = r / 1.41421356237309504880f;
  206. z = t*t / d;
  207. }
  208. return z;
  209. }
  210. /*
  211. * Given two rotations, e1 and e2, expressed as quaternion rotations,
  212. * figure out the equivalent single rotation and stuff it into dest.
  213. *
  214. * This routine also normalizes the result every RENORMCOUNT times it is
  215. * called, to keep error from creeping in.
  216. *
  217. * NOTE: This routine is written so that q1 or q2 may be the same
  218. * as dest (or each other).
  219. */
  220. #define RENORMCOUNT 97
  221. void
  222. add_quats(double q1[4], double q2[4], double dest[4])
  223. {
  224. static int count=0;
  225. double t1[4], t2[4], t3[4];
  226. double tf[4];
  227. vcopy(q1,t1);
  228. vscale(t1,q2[3]);
  229. vcopy(q2,t2);
  230. vscale(t2,q1[3]);
  231. vcross(q2,q1,t3);
  232. vadd(t1,t2,tf);
  233. vadd(t3,tf,tf);
  234. tf[3] = q1[3] * q2[3] - vdot(q1,q2);
  235. dest[0] = tf[0];
  236. dest[1] = tf[1];
  237. dest[2] = tf[2];
  238. dest[3] = tf[3];
  239. if (++count > RENORMCOUNT) {
  240. count = 0;
  241. normalize_quat(dest);
  242. }
  243. }
  244. /*
  245. * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
  246. * If they don't add up to 1.0, dividing by their magnitued will
  247. * renormalize them.
  248. *
  249. * Note: See the following for more information on quaternions:
  250. *
  251. * - Shoemake, K., Animating rotation with quaternion curves, Computer
  252. * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
  253. * - Pletinckx, D., Quaternion calculus as a basic tool in computer
  254. * graphics, The Visual Computer 5, 2-13, 1989.
  255. */
  256. static void normalize_quat(double q[4])
  257. {
  258. int i;
  259. double mag;
  260. mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
  261. for (i = 0; i < 4; i++) q[i] /= mag;
  262. }
  263. /*
  264. * Build a rotation matrix, given a quaternion rotation.
  265. *
  266. */
  267. void build_rotmatrix(GLfloat m[4][4], double q[4])
  268. {
  269. m[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
  270. m[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
  271. m[0][2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
  272. m[0][3] = 0.0f;
  273. m[1][0] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
  274. m[1][1]= 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
  275. m[1][2] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
  276. m[1][3] = 0.0f;
  277. m[2][0] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
  278. m[2][1] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
  279. m[2][2] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
  280. m[2][3] = 0.0f;
  281. m[3][0] = 0.0f;
  282. m[3][1] = 0.0f;
  283. m[3][2] = 0.0f;
  284. m[3][3] = 1.0f;
  285. }