Source code of Windows XP (NT5)
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.

288 lines
6.3 KiB

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