home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / vopengl / bounce / bounce.c < prev    next >
C/C++ Source or Header  |  1998-06-21  |  4KB  |  204 lines

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