spaceball.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Spaceball demo
  2. *
  3. * Written by John Tsiombikas <nuclear@member.fsf.org>
  4. * (converted from the libspnav cube example)
  5. *
  6. * Use the spaceball to move and rotate the colored cube.
  7. * Pressing any button will reset the cube at its original location.
  8. *
  9. * Press escape or q to exit.
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <GL/freeglut.h>
  15. #include "vmath.h"
  16. #ifndef M_PI
  17. #define M_PI 3.14159265358979323846264338327950
  18. #endif
  19. void draw_cube(void);
  20. /* callbacks */
  21. void disp(void);
  22. void reshape(int x, int y);
  23. void keyb(unsigned char key, int x, int y);
  24. void sbmot(int x, int y, int z); /* spaceball translation */
  25. void sbrot(int x, int y, int z); /* spaceball rotation */
  26. void sbbut(int bn, int state); /* spaceball button */
  27. vec3_t pos = {0, 0, -6};
  28. quat_t rot = {0, 0, 0, 1};
  29. int main(int argc, char **argv)
  30. {
  31. glutInit(&argc, argv);
  32. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  33. glutCreateWindow("spaceball demo");
  34. glutDisplayFunc(disp);
  35. glutReshapeFunc(reshape);
  36. glutKeyboardFunc(keyb);
  37. glutSpaceballMotionFunc(sbmot);
  38. glutSpaceballRotateFunc(sbrot);
  39. glutSpaceballButtonFunc(sbbut);
  40. glEnable(GL_CULL_FACE);
  41. glutMainLoop();
  42. return 0;
  43. }
  44. void disp(void)
  45. {
  46. mat4_t xform;
  47. quat_to_mat(xform, rot);
  48. glClear(GL_COLOR_BUFFER_BIT);
  49. glMatrixMode(GL_MODELVIEW);
  50. glLoadIdentity();
  51. glTranslatef(pos.x, pos.y, pos.z);
  52. glMultMatrixf((float*)xform);
  53. draw_cube();
  54. glutSwapBuffers();
  55. }
  56. void draw_cube(void)
  57. {
  58. glBegin(GL_QUADS);
  59. /* face +Z */
  60. glNormal3f(0, 0, 1);
  61. glColor3f(1, 0, 0);
  62. glVertex3f(-1, -1, 1);
  63. glVertex3f(1, -1, 1);
  64. glVertex3f(1, 1, 1);
  65. glVertex3f(-1, 1, 1);
  66. /* face +X */
  67. glNormal3f(1, 0, 0);
  68. glColor3f(0, 1, 0);
  69. glVertex3f(1, -1, 1);
  70. glVertex3f(1, -1, -1);
  71. glVertex3f(1, 1, -1);
  72. glVertex3f(1, 1, 1);
  73. /* face -Z */
  74. glNormal3f(0, 0, -1);
  75. glColor3f(0, 0, 1);
  76. glVertex3f(1, -1, -1);
  77. glVertex3f(-1, -1, -1);
  78. glVertex3f(-1, 1, -1);
  79. glVertex3f(1, 1, -1);
  80. /* face -X */
  81. glNormal3f(-1, 0, 0);
  82. glColor3f(1, 1, 0);
  83. glVertex3f(-1, -1, -1);
  84. glVertex3f(-1, -1, 1);
  85. glVertex3f(-1, 1, 1);
  86. glVertex3f(-1, 1, -1);
  87. /* face +Y */
  88. glNormal3f(0, 1, 0);
  89. glColor3f(0, 1, 1);
  90. glVertex3f(-1, 1, 1);
  91. glVertex3f(1, 1, 1);
  92. glVertex3f(1, 1, -1);
  93. glVertex3f(-1, 1, -1);
  94. /* face -Y */
  95. glNormal3f(0, -1, 0);
  96. glColor3f(1, 0, 1);
  97. glVertex3f(-1, -1, -1);
  98. glVertex3f(1, -1, -1);
  99. glVertex3f(1, -1, 1);
  100. glVertex3f(-1, -1, 1);
  101. glEnd();
  102. }
  103. /* 45deg fov */
  104. #define FOV (M_PI / 4.0)
  105. void reshape(int x, int y)
  106. {
  107. float aspect = (float)x / (float)y;
  108. float halfy = (float)tan(FOV / 2.0);
  109. float halfx = halfy * aspect;
  110. glViewport(0, 0, x, y);
  111. glMatrixMode(GL_PROJECTION);
  112. glLoadIdentity();
  113. glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
  114. }
  115. void keyb(unsigned char key, int x, int y)
  116. {
  117. switch(key) {
  118. case 'q':
  119. case 'Q':
  120. case 27:
  121. exit(0);
  122. case ' ':
  123. /* reset initial view */
  124. pos = v3_cons(0, 0, -6);
  125. rot = quat_cons(1, 0, 0, 0);
  126. glutPostRedisplay();
  127. default:
  128. break;
  129. }
  130. }
  131. void sbmot(int x, int y, int z)
  132. {
  133. pos.x += x * 0.001f;
  134. pos.y += y * 0.001f;
  135. pos.z -= z * 0.001f;
  136. glutPostRedisplay();
  137. }
  138. void sbrot(int x, int y, int z)
  139. {
  140. float axis_len = (float)sqrt(x * x + y * y + z * z);
  141. rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len);
  142. glutPostRedisplay();
  143. }
  144. void sbbut(int bn, int state)
  145. {
  146. if(state == GLUT_DOWN) {
  147. pos = v3_cons(0, 0, -6);
  148. rot = quat_cons(1, 0, 0, 0);
  149. glutPostRedisplay();
  150. }
  151. }