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.

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