Ball.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /***** Ball.c *****/
  2. /* This file contains code required to implement a trackball user
  3. * interface for manipulating a 3D scene. This code was taken from
  4. * Graphics Gems 4, editted by Andrew Glassner
  5. */
  6. #include "Ball.h"
  7. #include "BallMath.h"
  8. #include <stdio.h>
  9. #define TRUE 1
  10. #define FALSE 0
  11. HMatrix mId = { {1, 0, 0, 0}
  12. , {0, 1, 0, 0}
  13. , {0, 0, 1, 0}
  14. , {0, 0, 0, 1}
  15. };
  16. float otherAxis[][4] = { {-0.48, 0.80, 0.36, 1} };
  17. /* Establish reasonable initial values for controller. */
  18. void Ball_Init(BallData * ball)
  19. {
  20. int i;
  21. ball->center = qOne;
  22. ball->radius = 1.0;
  23. ball->vDown = ball->vNow = qOne;
  24. ball->qDown = ball->qNow = qOne;
  25. for (i = 15; i >= 0; i--)
  26. ((float *)ball->mNow)[i] = ((float *)ball->mDown)[i] =
  27. ((float *)mId)[i];
  28. ball->dragging = 0;
  29. }
  30. /* Set the center and size of the controller. */
  31. void Ball_Place(BallData * ball, HVect center, double radius)
  32. {
  33. ball->center = center;
  34. ball->radius = radius;
  35. }
  36. /* Incorporate new mouse position. */
  37. void Ball_Mouse(BallData * ball, HVect vNow)
  38. {
  39. ball->vNow = vNow;
  40. }
  41. /* Using vDown, vNow, dragging, and axisSet, compute rotation etc. */
  42. void Ball_Update(BallData * ball)
  43. {
  44. Quat qout, qout2;
  45. ball->vFrom = MouseOnSphere(ball->vDown, ball->center, ball->radius);
  46. ball->vTo = MouseOnSphere(ball->vNow, ball->center, ball->radius);
  47. if (ball->dragging) {
  48. ball->qDrag = Qt_FromBallPoints(ball->vFrom, ball->vTo);
  49. ball->qNow = Qt_Mul(ball->qDrag, ball->qDown);
  50. }
  51. Qt_ToBallPoints(ball->qDown, &ball->vrFrom, &ball->vrTo);
  52. Qt_ToMatrix(Qt_Conj(ball->qNow), ball->mNow); /* Gives transpose for GL. */
  53. qout2 = Matrix_to_Qt(ball->mNow);
  54. qout = Qt_Conj(qout2);
  55. }
  56. void Ball_SetMatrix(BallData * ball, HMatrix mat)
  57. {
  58. int i, j;
  59. Quat qt;
  60. Ball_Init(ball);
  61. qt = Matrix_to_Qt(mat);
  62. ball->qNow = Qt_Conj(qt);
  63. ball->qDown = ball->qNow;
  64. for (i = 0; i < QuatLen; i++)
  65. for (j = 0; j < QuatLen; j++)
  66. ball->mNow[i][j] = mat[i][j];
  67. }
  68. /* Return rotation matrix defined by controller use. */
  69. void Ball_Value(BallData * ball, HMatrix mNow)
  70. {
  71. int i;
  72. for (i = 15; i >= 0; i--)
  73. ((float *)mNow)[i] = ((float *)ball->mNow)[i];
  74. }
  75. /* Begin drag sequence. */
  76. void Ball_BeginDrag(BallData * ball)
  77. {
  78. ball->dragging = 1;
  79. ball->vDown = ball->vNow;
  80. }
  81. /* Stop drag sequence. */
  82. void Ball_EndDrag(BallData * ball)
  83. {
  84. int i;
  85. ball->dragging = 0;
  86. ball->qDown = ball->qNow;
  87. for (i = 15; i >= 0; i--)
  88. ((float *)ball->mDown)[i] = ((float *)ball->mNow)[i];
  89. }