Leaked source code of windows server 2003
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.

414 lines
9.7 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. * Shell hacking courtesy of:
  17. * Reptilian Inhaleware
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <windows.h>
  23. #include <math.h>
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include "tk.h"
  27. #include "trackbal.h"
  28. /*
  29. * globals
  30. */
  31. static GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
  32. static GLenum (*MouseUpFunc)(int, int, GLenum) = NULL;
  33. static HWND ghwnd;
  34. GLint giWidth, giHeight;
  35. LONG glMouseDownX, glMouseDownY;
  36. BOOL gbLeftMouse = FALSE;
  37. BOOL gbSpinning = FALSE;
  38. float curquat[4], lastquat[4];
  39. /*
  40. * This size should really be based on the distance from the center of
  41. * rotation to the point on the object underneath the mouse. That
  42. * point would then track the mouse as closely as possible. This is a
  43. * simple example, though, so that is left as an Exercise for the
  44. * Programmer.
  45. */
  46. #define TRACKBALLSIZE (0.8f)
  47. /*
  48. * Local function prototypes (not defined in trackball.h)
  49. */
  50. static float tb_project_to_sphere(float, float, float);
  51. static void normalize_quat(float [4]);
  52. void
  53. trackball_Init( GLint width, GLint height )
  54. {
  55. ghwnd = tkGetHWND();
  56. giWidth = width;
  57. giHeight = height;
  58. trackball_calc_quat( curquat, 0.0f, 0.0f, 0.0f, 0.0f );
  59. }
  60. void
  61. trackball_Resize( GLint width, GLint height )
  62. {
  63. giWidth = width;
  64. giHeight = height;
  65. }
  66. GLenum
  67. trackball_MouseDown( int mouseX, int mouseY, GLenum button )
  68. {
  69. SetCapture(ghwnd);
  70. glMouseDownX = mouseX;
  71. glMouseDownY = mouseY;
  72. gbLeftMouse = TRUE;
  73. return GL_TRUE;
  74. }
  75. GLenum
  76. trackball_MouseUp( int mouseX, int mouseY, GLenum button )
  77. {
  78. ReleaseCapture();
  79. gbLeftMouse = FALSE;
  80. return GL_TRUE;
  81. }
  82. /* these 4 not used yet */
  83. void
  84. trackball_MouseDownEvent( int mouseX, int mouseY, GLenum button )
  85. {
  86. }
  87. void
  88. trackball_MouseUpEvent( int mouseX, int mouseY, GLenum button )
  89. {
  90. }
  91. void
  92. trackball_MouseDownFunc(GLenum (*Func)(int, int, GLenum))
  93. {
  94. MouseDownFunc = Func;
  95. }
  96. void
  97. trackball_MouseUpFunc(GLenum (*Func)(int, int, GLenum))
  98. {
  99. MouseUpFunc = Func;
  100. }
  101. void
  102. trackball_CalcRotMatrix( GLfloat matRot[4][4] )
  103. {
  104. POINT pt;
  105. if (gbLeftMouse)
  106. {
  107. tkGetMouseLoc( &pt.x, &pt.y );
  108. // If mouse has moved since button was pressed, change quaternion.
  109. if (pt.x != glMouseDownX || pt.y != glMouseDownY)
  110. {
  111. #if 1
  112. /* negate all params for proper operation with glTranslate(-z)
  113. */
  114. trackball_calc_quat(lastquat,
  115. -(2.0f * ( giWidth - glMouseDownX ) / giWidth - 1.0f),
  116. -(2.0f * glMouseDownY / giHeight - 1.0f),
  117. -(2.0f * ( giWidth - pt.x ) / giWidth - 1.0f),
  118. -(2.0f * pt.y / giHeight - 1.0f)
  119. );
  120. #else
  121. // now out-of-date
  122. trackball_calc_quat(lastquat,
  123. 2.0f * ( Width - glMouseDownX ) / Width - 1.0f,
  124. 2.0f * glMouseDownY / Height - 1.0f,
  125. 2.0f * ( Width - pt.x ) / Width - 1.0f,
  126. 2.0f * pt.y / Height - 1.0f );
  127. #endif
  128. gbSpinning = TRUE;
  129. }
  130. else
  131. gbSpinning = FALSE;
  132. glMouseDownX = pt.x;
  133. glMouseDownY = pt.y;
  134. }
  135. if (gbSpinning)
  136. trackball_add_quats(lastquat, curquat, curquat);
  137. trackball_build_rotmatrix(matRot, curquat);
  138. }
  139. void
  140. vzero(float *v)
  141. {
  142. v[0] = 0.0f;
  143. v[1] = 0.0f;
  144. v[2] = 0.0f;
  145. }
  146. void
  147. vset(float *v, float x, float y, float z)
  148. {
  149. v[0] = x;
  150. v[1] = y;
  151. v[2] = z;
  152. }
  153. void
  154. vsub(const float *src1, const float *src2, float *dst)
  155. {
  156. dst[0] = src1[0] - src2[0];
  157. dst[1] = src1[1] - src2[1];
  158. dst[2] = src1[2] - src2[2];
  159. }
  160. void
  161. vcopy(const float *v1, float *v2)
  162. {
  163. register int i;
  164. for (i = 0 ; i < 3 ; i++)
  165. v2[i] = v1[i];
  166. }
  167. void
  168. vcross(const float *v1, const float *v2, float *cross)
  169. {
  170. float temp[3];
  171. temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  172. temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  173. temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  174. vcopy(temp, cross);
  175. }
  176. float
  177. vlength(const float *v)
  178. {
  179. return (float) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  180. }
  181. void
  182. vscale(float *v, float div)
  183. {
  184. v[0] *= div;
  185. v[1] *= div;
  186. v[2] *= div;
  187. }
  188. void
  189. vnormal(float *v)
  190. {
  191. vscale(v,1.0f/vlength(v));
  192. }
  193. float
  194. vdot(const float *v1, const float *v2)
  195. {
  196. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  197. }
  198. void
  199. vadd(const float *src1, const float *src2, float *dst)
  200. {
  201. dst[0] = src1[0] + src2[0];
  202. dst[1] = src1[1] + src2[1];
  203. dst[2] = src1[2] + src2[2];
  204. }
  205. /*
  206. * Ok, simulate a track-ball. Project the points onto the virtual
  207. * trackball, then figure out the axis of rotation, which is the cross
  208. * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  209. * Note: This is a deformed trackball-- is a trackball in the center,
  210. * but is deformed into a hyperbolic sheet of rotation away from the
  211. * center. This particular function was chosen after trying out
  212. * several variations.
  213. *
  214. * It is assumed that the arguments to this routine are in the range
  215. * (-1.0 ... 1.0)
  216. */
  217. void
  218. trackball_calc_quat(float q[4], float p1x, float p1y, float p2x, float p2y)
  219. {
  220. float a[3]; /* Axis of rotation */
  221. float phi; /* how much to rotate about axis */
  222. float p1[3], p2[3], d[3];
  223. float t;
  224. if (p1x == p2x && p1y == p2y) {
  225. /* Zero rotation */
  226. vzero(q);
  227. q[3] = 1.0f;
  228. return;
  229. }
  230. /*
  231. * First, figure out z-coordinates for projection of P1 and P2 to
  232. * deformed sphere
  233. */
  234. vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  235. vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  236. /*
  237. * Now, we want the cross product of P1 and P2
  238. */
  239. vcross(p2,p1,a);
  240. /*
  241. * Figure out how much to rotate around that axis.
  242. */
  243. vsub(p1,p2,d);
  244. t = vlength(d) / (2.0f*TRACKBALLSIZE);
  245. /*
  246. * Avoid problems with out-of-control values...
  247. */
  248. if (t > 1.0f) t = 1.0f;
  249. if (t < -1.0f) t = -1.0f;
  250. phi = 2.0f * (float) asin(t);
  251. trackball_axis_to_quat(a,phi,q);
  252. }
  253. /*
  254. * Given an axis and angle, compute quaternion.
  255. */
  256. void
  257. trackball_axis_to_quat(float a[3], float phi, float q[4])
  258. {
  259. vnormal(a);
  260. vcopy(a,q);
  261. vscale(q,(float) sin(phi/2.0f));
  262. q[3] = (float) cos(phi/2.0f);
  263. }
  264. /*
  265. * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
  266. * if we are away from the center of the sphere.
  267. */
  268. static float
  269. tb_project_to_sphere(float r, float x, float y)
  270. {
  271. float d, t, z;
  272. d = (float) sqrt(x*x + y*y);
  273. if (d < r * 0.70710678118654752440f) { /* Inside sphere */
  274. z = (float) sqrt(r*r - d*d);
  275. } else { /* On hyperbola */
  276. t = r / 1.41421356237309504880f;
  277. z = t*t / d;
  278. }
  279. return z;
  280. }
  281. /*
  282. * Given two rotations, e1 and e2, expressed as quaternion rotations,
  283. * figure out the equivalent single rotation and stuff it into dest.
  284. *
  285. * This routine also normalizes the result every RENORMCOUNT times it is
  286. * called, to keep error from creeping in.
  287. *
  288. * NOTE: This routine is written so that q1 or q2 may be the same
  289. * as dest (or each other).
  290. */
  291. #define RENORMCOUNT 97
  292. void
  293. trackball_add_quats(float q1[4], float q2[4], float dest[4])
  294. {
  295. static int count=0;
  296. int i;
  297. float t1[4], t2[4], t3[4];
  298. float tf[4];
  299. vcopy(q1,t1);
  300. vscale(t1,q2[3]);
  301. vcopy(q2,t2);
  302. vscale(t2,q1[3]);
  303. vcross(q2,q1,t3);
  304. vadd(t1,t2,tf);
  305. vadd(t3,tf,tf);
  306. tf[3] = q1[3] * q2[3] - vdot(q1,q2);
  307. dest[0] = tf[0];
  308. dest[1] = tf[1];
  309. dest[2] = tf[2];
  310. dest[3] = tf[3];
  311. if (++count > RENORMCOUNT) {
  312. count = 0;
  313. normalize_quat(dest);
  314. }
  315. }
  316. /*
  317. * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
  318. * If they don't add up to 1.0, dividing by their magnitued will
  319. * renormalize them.
  320. *
  321. * Note: See the following for more information on quaternions:
  322. *
  323. * - Shoemake, K., Animating rotation with quaternion curves, Computer
  324. * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
  325. * - Pletinckx, D., Quaternion calculus as a basic tool in computer
  326. * graphics, The Visual Computer 5, 2-13, 1989.
  327. */
  328. static void
  329. normalize_quat(float q[4])
  330. {
  331. int i;
  332. float mag;
  333. mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
  334. for (i = 0; i < 4; i++) q[i] /= mag;
  335. }
  336. /*
  337. * Build a rotation matrix, given a quaternion rotation.
  338. *
  339. */
  340. void
  341. trackball_build_rotmatrix(float m[4][4], float q[4])
  342. {
  343. m[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
  344. m[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
  345. m[0][2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
  346. m[0][3] = 0.0f;
  347. m[1][0] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
  348. m[1][1]= 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
  349. m[1][2] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
  350. m[1][3] = 0.0f;
  351. m[2][0] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
  352. m[2][1] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
  353. m[2][2] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
  354. m[2][3] = 0.0f;
  355. m[3][0] = 0.0f;
  356. m[3][1] = 0.0f;
  357. m[3][2] = 0.0f;
  358. m[3][3] = 1.0f;
  359. }