Clone of mesa.
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.

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