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.3KB

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