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.

trackball.c 8.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 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 "trackball.h"
  54. /*
  55. * This size should really be based on the distance from the center of
  56. * rotation to the point on the object underneath the mouse. That
  57. * point would then track the mouse as closely as possible. This is a
  58. * simple example, though, so that is left as an Exercise for the
  59. * Programmer.
  60. */
  61. #define TRACKBALLSIZE (0.4f)
  62. /*
  63. * Local function prototypes (not defined in trackball.h)
  64. */
  65. static float tb_project_to_sphere(float, float, float);
  66. static void normalize_quat(float [4]);
  67. static void
  68. vzero(float *v)
  69. {
  70. v[0] = 0.0;
  71. v[1] = 0.0;
  72. v[2] = 0.0;
  73. }
  74. static void
  75. vset(float *v, float x, float y, float z)
  76. {
  77. v[0] = x;
  78. v[1] = y;
  79. v[2] = z;
  80. }
  81. static void
  82. vsub(const float *src1, const float *src2, float *dst)
  83. {
  84. dst[0] = src1[0] - src2[0];
  85. dst[1] = src1[1] - src2[1];
  86. dst[2] = src1[2] - src2[2];
  87. }
  88. static void
  89. vcopy(const float *v1, float *v2)
  90. {
  91. register int i;
  92. for (i = 0 ; i < 3 ; i++)
  93. v2[i] = v1[i];
  94. }
  95. static void
  96. vcross(const float *v1, const float *v2, float *cross)
  97. {
  98. float temp[3];
  99. temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  100. temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  101. temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  102. vcopy(temp, cross);
  103. }
  104. static float
  105. vlength(const float *v)
  106. {
  107. return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  108. }
  109. static void
  110. vscale(float *v, float div)
  111. {
  112. v[0] *= div;
  113. v[1] *= div;
  114. v[2] *= div;
  115. }
  116. static void
  117. vnormal(float *v)
  118. {
  119. vscale(v,1.0f/vlength(v));
  120. }
  121. static float
  122. vdot(const float *v1, const float *v2)
  123. {
  124. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  125. }
  126. static void
  127. vadd(const float *src1, const float *src2, float *dst)
  128. {
  129. dst[0] = src1[0] + src2[0];
  130. dst[1] = src1[1] + src2[1];
  131. dst[2] = src1[2] + src2[2];
  132. }
  133. /*
  134. * Ok, simulate a track-ball. Project the points onto the virtual
  135. * trackball, then figure out the axis of rotation, which is the cross
  136. * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  137. * Note: This is a deformed trackball-- is a trackball in the center,
  138. * but is deformed into a hyperbolic sheet of rotation away from the
  139. * center. This particular function was chosen after trying out
  140. * several variations.
  141. *
  142. * It is assumed that the arguments to this routine are in the range
  143. * (-1.0 ... 1.0)
  144. */
  145. void
  146. trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
  147. {
  148. float a[3]; /* Axis of rotation */
  149. float phi; /* how much to rotate about axis */
  150. float p1[3], p2[3], d[3];
  151. float t;
  152. if (p1x == p2x && p1y == p2y) {
  153. /* Zero rotation */
  154. vzero(q);
  155. q[3] = 1.0;
  156. return;
  157. }
  158. /*
  159. * First, figure out z-coordinates for projection of P1 and P2 to
  160. * deformed sphere
  161. */
  162. vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  163. vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  164. /*
  165. * Now, we want the cross product of P1 and P2
  166. */
  167. vcross(p2,p1,a);
  168. /*
  169. * Figure out how much to rotate around that axis.
  170. */
  171. vsub(p1,p2,d);
  172. t = vlength(d) / (2.0f*TRACKBALLSIZE);
  173. /*
  174. * Avoid problems with out-of-control values...
  175. */
  176. if (t > 1.0f) t = 1.0f;
  177. if (t < -1.0f) t = -1.0f;
  178. phi = 2.0f * asin(t);
  179. axis_to_quat(a,phi,q);
  180. }
  181. /*
  182. * Given an axis and angle, compute quaternion.
  183. */
  184. void
  185. axis_to_quat(float a[3], float phi, float q[4])
  186. {
  187. vnormal(a);
  188. vcopy(a,q);
  189. vscale(q,sin(phi/2.0f));
  190. q[3] = cos(phi/2.0f);
  191. }
  192. /*
  193. * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
  194. * if we are away from the center of the sphere.
  195. */
  196. static float
  197. tb_project_to_sphere(float r, float x, float y)
  198. {
  199. float d, t, z;
  200. d = sqrt(x*x + y*y);
  201. if (d < r * 0.70710678118654752440f) { /* Inside sphere */
  202. z = sqrt(r*r - d*d);
  203. } else { /* On hyperbola */
  204. t = r / 1.41421356237309504880f;
  205. z = t*t / d;
  206. }
  207. return z;
  208. }
  209. /*
  210. * Given two rotations, e1 and e2, expressed as quaternion rotations,
  211. * figure out the equivalent single rotation and stuff it into dest.
  212. *
  213. * This routine also normalizes the result every RENORMCOUNT times it is
  214. * called, to keep error from creeping in.
  215. *
  216. * NOTE: This routine is written so that q1 or q2 may be the same
  217. * as dest (or each other).
  218. */
  219. #define RENORMCOUNT 97
  220. void
  221. add_quats(float q1[4], float q2[4], float dest[4])
  222. {
  223. static int count=0;
  224. float t1[4], t2[4], t3[4];
  225. float tf[4];
  226. vcopy(q1,t1);
  227. vscale(t1,q2[3]);
  228. vcopy(q2,t2);
  229. vscale(t2,q1[3]);
  230. vcross(q2,q1,t3);
  231. vadd(t1,t2,tf);
  232. vadd(t3,tf,tf);
  233. tf[3] = q1[3] * q2[3] - vdot(q1,q2);
  234. dest[0] = tf[0];
  235. dest[1] = tf[1];
  236. dest[2] = tf[2];
  237. dest[3] = tf[3];
  238. if (++count > RENORMCOUNT) {
  239. count = 0;
  240. normalize_quat(dest);
  241. }
  242. }
  243. /*
  244. * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
  245. * If they don't add up to 1.0, dividing by their magnitued will
  246. * renormalize them.
  247. *
  248. * Note: See the following for more information on quaternions:
  249. *
  250. * - Shoemake, K., Animating rotation with quaternion curves, Computer
  251. * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
  252. * - Pletinckx, D., Quaternion calculus as a basic tool in computer
  253. * graphics, The Visual Computer 5, 2-13, 1989.
  254. */
  255. static void
  256. normalize_quat(float q[4])
  257. {
  258. int i;
  259. float 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
  268. build_rotmatrix(float m[4][4], float q[4])
  269. {
  270. m[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
  271. m[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
  272. m[0][2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
  273. m[0][3] = 0.0f;
  274. m[1][0] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
  275. m[1][1]= 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
  276. m[1][2] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
  277. m[1][3] = 0.0f;
  278. m[2][0] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
  279. m[2][1] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
  280. m[2][2] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
  281. m[2][3] = 0.0f;
  282. m[3][0] = 0.0f;
  283. m[3][1] = 0.0f;
  284. m[3][2] = 0.0f;
  285. m[3][3] = 1.0f;
  286. }