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.

bounce.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Bouncing ball demo.
  3. *
  4. * This program is in the public domain
  5. *
  6. * Brian Paul
  7. *
  8. * Conversion to GLUT by Mark J. Kilgard
  9. */
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <GL/glut.h>
  14. #define COS(X) cos( (X) * 3.14159/180.0 )
  15. #define SIN(X) sin( (X) * 3.14159/180.0 )
  16. #define RED 1
  17. #define WHITE 2
  18. #define CYAN 3
  19. GLboolean IndexMode = GL_FALSE;
  20. GLuint Ball;
  21. GLenum Mode;
  22. GLfloat Zrot = 0.0, Zstep = 180.0;
  23. GLfloat Xpos = 0.0, Ypos = 1.0;
  24. GLfloat Xvel = 2.0, Yvel = 0.0;
  25. GLfloat Xmin = -4.0, Xmax = 4.0;
  26. GLfloat Ymin = -3.8, Ymax = 4.0;
  27. GLfloat G = -9.8;
  28. static GLuint
  29. make_ball(void)
  30. {
  31. GLuint list;
  32. GLfloat a, b;
  33. GLfloat da = 18.0, db = 18.0;
  34. GLfloat radius = 1.0;
  35. GLuint color;
  36. GLfloat x, y, z;
  37. list = glGenLists(1);
  38. glNewList(list, GL_COMPILE);
  39. color = 0;
  40. for (a = -90.0; a + da <= 90.0; a += da) {
  41. glBegin(GL_QUAD_STRIP);
  42. for (b = 0.0; b <= 360.0; b += db) {
  43. if (color) {
  44. glIndexi(RED);
  45. glColor3f(1, 0, 0);
  46. } else {
  47. glIndexi(WHITE);
  48. glColor3f(1, 1, 1);
  49. }
  50. x = radius * COS(b) * COS(a);
  51. y = radius * SIN(b) * COS(a);
  52. z = radius * SIN(a);
  53. glVertex3f(x, y, z);
  54. x = radius * COS(b) * COS(a + da);
  55. y = radius * SIN(b) * COS(a + da);
  56. z = radius * SIN(a + da);
  57. glVertex3f(x, y, z);
  58. color = 1 - color;
  59. }
  60. glEnd();
  61. }
  62. glEndList();
  63. return list;
  64. }
  65. static void
  66. reshape(int width, int height)
  67. {
  68. float aspect = (float) width / (float) height;
  69. glViewport(0, 0, (GLint) width, (GLint) height);
  70. glMatrixMode(GL_PROJECTION);
  71. glLoadIdentity();
  72. glOrtho(-6.0 * aspect, 6.0 * aspect, -6.0, 6.0, -6.0, 6.0);
  73. glMatrixMode(GL_MODELVIEW);
  74. }
  75. /* ARGSUSED1 */
  76. static void
  77. key(unsigned char k, int x, int y)
  78. {
  79. switch (k) {
  80. case 27: /* Escape */
  81. exit(0);
  82. }
  83. }
  84. static void
  85. draw(void)
  86. {
  87. GLint i;
  88. glClear(GL_COLOR_BUFFER_BIT);
  89. glIndexi(CYAN);
  90. glColor3f(0, 1, 1);
  91. glBegin(GL_LINES);
  92. for (i = -5; i <= 5; i++) {
  93. glVertex2i(i, -5);
  94. glVertex2i(i, 5);
  95. }
  96. for (i = -5; i <= 5; i++) {
  97. glVertex2i(-5, i);
  98. glVertex2i(5, i);
  99. }
  100. for (i = -5; i <= 5; i++) {
  101. glVertex2i(i, -5);
  102. glVertex2f(i * 1.15, -5.9);
  103. }
  104. glVertex2f(-5.3, -5.35);
  105. glVertex2f(5.3, -5.35);
  106. glVertex2f(-5.75, -5.9);
  107. glVertex2f(5.75, -5.9);
  108. glEnd();
  109. glPushMatrix();
  110. glTranslatef(Xpos, Ypos, 0.0);
  111. glScalef(2.0, 2.0, 2.0);
  112. glRotatef(8.0, 0.0, 0.0, 1.0);
  113. glRotatef(90.0, 1.0, 0.0, 0.0);
  114. glRotatef(Zrot, 0.0, 0.0, 1.0);
  115. glCallList(Ball);
  116. glPopMatrix();
  117. glFlush();
  118. glutSwapBuffers();
  119. }
  120. static void
  121. idle(void)
  122. {
  123. static float vel0 = -100.0;
  124. static double t0 = -1.;
  125. double t, dt;
  126. t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
  127. if (t0 < 0.)
  128. t0 = t;
  129. dt = t - t0;
  130. t0 = t;
  131. Zrot += Zstep*dt;
  132. Xpos += Xvel*dt;
  133. if (Xpos >= Xmax) {
  134. Xpos = Xmax;
  135. Xvel = -Xvel;
  136. Zstep = -Zstep;
  137. }
  138. if (Xpos <= Xmin) {
  139. Xpos = Xmin;
  140. Xvel = -Xvel;
  141. Zstep = -Zstep;
  142. }
  143. Ypos += Yvel*dt;
  144. Yvel += G*dt;
  145. if (Ypos < Ymin) {
  146. Ypos = Ymin;
  147. if (vel0 == -100.0)
  148. vel0 = fabs(Yvel);
  149. Yvel = vel0;
  150. }
  151. glutPostRedisplay();
  152. }
  153. static void
  154. visible(int vis)
  155. {
  156. if (vis == GLUT_VISIBLE)
  157. glutIdleFunc(idle);
  158. else
  159. glutIdleFunc(NULL);
  160. }
  161. int main(int argc, char *argv[])
  162. {
  163. glutInit(&argc, argv);
  164. glutInitWindowPosition(0, 0);
  165. glutInitWindowSize(600, 450);
  166. IndexMode = argc > 1 && strcmp(argv[1], "-ci") == 0;
  167. if (IndexMode)
  168. glutInitDisplayMode(GLUT_INDEX | GLUT_DOUBLE);
  169. else
  170. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  171. glutCreateWindow("Bounce");
  172. Ball = make_ball();
  173. glCullFace(GL_BACK);
  174. glEnable(GL_CULL_FACE);
  175. glDisable(GL_DITHER);
  176. glShadeModel(GL_FLAT);
  177. glutDisplayFunc(draw);
  178. glutReshapeFunc(reshape);
  179. glutVisibilityFunc(visible);
  180. glutKeyboardFunc(key);
  181. if (IndexMode) {
  182. glutSetColor(RED, 1.0, 0.0, 0.0);
  183. glutSetColor(WHITE, 1.0, 1.0, 1.0);
  184. glutSetColor(CYAN, 0.0, 1.0, 1.0);
  185. }
  186. glutMainLoop();
  187. return 0; /* ANSI C requires main to return int. */
  188. }