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.

tb.c 2.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Simple trackball-like motion adapted (ripped off) from projtex.c
  3. * (written by David Yu and David Blythe). See the SIGGRAPH '96
  4. * Advanced OpenGL course notes.
  5. */
  6. #include <math.h>
  7. #include <assert.h>
  8. #include <GL/glut.h>
  9. #include "tb.h"
  10. #include "trackball.h"
  11. /* globals */
  12. static GLuint tb_lasttime;
  13. float curquat[4];
  14. float lastquat[4];
  15. int beginx, beginy;
  16. static GLuint tb_width;
  17. static GLuint tb_height;
  18. static GLint tb_button = -1;
  19. static GLboolean tb_tracking = GL_FALSE;
  20. static GLboolean tb_animate = GL_TRUE;
  21. static void
  22. _tbAnimate(void)
  23. {
  24. add_quats(lastquat, curquat, curquat);
  25. glutPostRedisplay();
  26. }
  27. static void
  28. _tbStartMotion(int x, int y, int time)
  29. {
  30. assert(tb_button != -1);
  31. glutIdleFunc(0);
  32. tb_tracking = GL_TRUE;
  33. tb_lasttime = time;
  34. beginx = x;
  35. beginy = y;
  36. }
  37. static void
  38. _tbStopMotion(unsigned time)
  39. {
  40. assert(tb_button != -1);
  41. tb_tracking = GL_FALSE;
  42. if (time == tb_lasttime && tb_animate) {
  43. glutIdleFunc(_tbAnimate);
  44. } else {
  45. if (tb_animate) {
  46. glutIdleFunc(0);
  47. }
  48. }
  49. }
  50. void
  51. tbAnimate(GLboolean animate)
  52. {
  53. tb_animate = animate;
  54. }
  55. void
  56. tbInit(GLuint button)
  57. {
  58. tb_button = button;
  59. trackball(curquat, 0.0, 0.0, 0.0, 0.0);
  60. }
  61. void
  62. tbMatrix(void)
  63. {
  64. GLfloat m[4][4];
  65. assert(tb_button != -1);
  66. build_rotmatrix(m, curquat);
  67. glMultMatrixf(&m[0][0]);
  68. }
  69. void
  70. tbReshape(int width, int height)
  71. {
  72. assert(tb_button != -1);
  73. tb_width = width;
  74. tb_height = height;
  75. }
  76. void
  77. tbMouse(int button, int state, int x, int y)
  78. {
  79. assert(tb_button != -1);
  80. if (state == GLUT_DOWN && button == tb_button)
  81. _tbStartMotion(x, y, glutGet(GLUT_ELAPSED_TIME));
  82. else if (state == GLUT_UP && button == tb_button)
  83. _tbStopMotion(glutGet(GLUT_ELAPSED_TIME));
  84. }
  85. void
  86. tbMotion(int x, int y)
  87. {
  88. if (tb_tracking) {
  89. trackball(lastquat,
  90. (2.0 * beginx - tb_width) / tb_width,
  91. (tb_height - 2.0 * beginy) / tb_height,
  92. (2.0 * x - tb_width) / tb_width,
  93. (tb_height - 2.0 * y) / tb_height
  94. );
  95. beginx = x;
  96. beginy = y;
  97. tb_animate = 1;
  98. tb_lasttime = glutGet(GLUT_ELAPSED_TIME);
  99. _tbAnimate();
  100. }
  101. }