home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / demos / bounce.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  4KB  |  241 lines

  1. /* $Id: bounce.c,v 1.1.1.1 1999/08/19 00:55:40 jtg Exp $ */
  2.  
  3. /*
  4.  * Bouncing ball demo.  Color index mode only!
  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. /*
  14.  * $Log: bounce.c,v $
  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.  
  33.  
  34. #include <math.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <GL/glut.h>
  38.  
  39. #define COS(X)   cos( (X) * 3.14159/180.0 )
  40. #define SIN(X)   sin( (X) * 3.14159/180.0 )
  41.  
  42. #define RED 1
  43. #define WHITE 2
  44. #define CYAN 3
  45.  
  46. GLboolean IndexMode = GL_FALSE;
  47. GLuint Ball;
  48. GLenum Mode;
  49. GLfloat Zrot = 0.0, Zstep = 6.0;
  50. GLfloat Xpos = 0.0, Ypos = 1.0;
  51. GLfloat Xvel = 0.2, Yvel = 0.0;
  52. GLfloat Xmin = -4.0, Xmax = 4.0;
  53. GLfloat Ymin = -3.8, Ymax = 4.0;
  54. GLfloat G = -0.1;
  55.  
  56. static GLuint 
  57. make_ball(void)
  58. {
  59.   GLuint list;
  60.   GLfloat a, b;
  61.   GLfloat da = 18.0, db = 18.0;
  62.   GLfloat radius = 1.0;
  63.   GLuint color;
  64.   GLfloat x, y, z;
  65.  
  66.   list = glGenLists(1);
  67.  
  68.   glNewList(list, GL_COMPILE);
  69.  
  70.   color = 0;
  71.   for (a = -90.0; a + da <= 90.0; a += da) {
  72.  
  73.     glBegin(GL_QUAD_STRIP);
  74.     for (b = 0.0; b <= 360.0; b += db) {
  75.  
  76.       if (color) {
  77.     glIndexi(RED);
  78.         glColor3f(1, 0, 0);
  79.       } else {
  80.     glIndexi(WHITE);
  81.         glColor3f(1, 1, 1);
  82.       }
  83.  
  84.       x = COS(b) * COS(a);
  85.       y = SIN(b) * COS(a);
  86.       z = SIN(a);
  87.       glVertex3f(x, y, z);
  88.  
  89.       x = radius * COS(b) * COS(a + da);
  90.       y = radius * SIN(b) * COS(a + da);
  91.       z = radius * SIN(a + da);
  92.       glVertex3f(x, y, z);
  93.  
  94.       color = 1 - color;
  95.     }
  96.     glEnd();
  97.  
  98.   }
  99.  
  100.   glEndList();
  101.  
  102.   return list;
  103. }
  104.  
  105. static void 
  106. reshape(int width, int height)
  107. {
  108.   float aspect = (float) width / (float) height;
  109.   glViewport(0, 0, (GLint) width, (GLint) height);
  110.   glMatrixMode(GL_PROJECTION);
  111.   glLoadIdentity();
  112.   glOrtho(-6.0 * aspect, 6.0 * aspect, -6.0, 6.0, -6.0, 6.0);
  113.   glMatrixMode(GL_MODELVIEW);
  114. }
  115.  
  116. /* ARGSUSED1 */
  117. static void
  118. key(unsigned char k, int x, int y)
  119. {
  120.   switch (k) {
  121.   case 27:  /* Escape */
  122.     exit(0);
  123.   }
  124. }
  125.  
  126. static void 
  127. draw(void)
  128. {
  129.   GLint i;
  130.  
  131.   glClear(GL_COLOR_BUFFER_BIT);
  132.  
  133.   glIndexi(CYAN);
  134.   glColor3f(0, 1, 1);
  135.   glBegin(GL_LINES);
  136.   for (i = -5; i <= 5; i++) {
  137.     glVertex2i(i, -5);
  138.     glVertex2i(i, 5);
  139.   }
  140.   for (i = -5; i <= 5; i++) {
  141.     glVertex2i(-5, i);
  142.     glVertex2i(5, i);
  143.   }
  144.   for (i = -5; i <= 5; i++) {
  145.     glVertex2i(i, -5);
  146.     glVertex2f(i * 1.15, -5.9);
  147.   }
  148.   glVertex2f(-5.3, -5.35);
  149.   glVertex2f(5.3, -5.35);
  150.   glVertex2f(-5.75, -5.9);
  151.   glVertex2f(5.75, -5.9);
  152.   glEnd();
  153.  
  154.   glPushMatrix();
  155.   glTranslatef(Xpos, Ypos, 0.0);
  156.   glScalef(2.0, 2.0, 2.0);
  157.   glRotatef(8.0, 0.0, 0.0, 1.0);
  158.   glRotatef(90.0, 1.0, 0.0, 0.0);
  159.   glRotatef(Zrot, 0.0, 0.0, 1.0);
  160.  
  161.   glCallList(Ball);
  162.  
  163.   glPopMatrix();
  164.  
  165.   glFlush();
  166.   glutSwapBuffers();
  167. }
  168.  
  169. static void 
  170. idle(void)
  171. {
  172.   static float vel0 = -100.0;
  173.  
  174.   Zrot += Zstep;
  175.  
  176.   Xpos += Xvel;
  177.   if (Xpos >= Xmax) {
  178.     Xpos = Xmax;
  179.     Xvel = -Xvel;
  180.     Zstep = -Zstep;
  181.   }
  182.   if (Xpos <= Xmin) {
  183.     Xpos = Xmin;
  184.     Xvel = -Xvel;
  185.     Zstep = -Zstep;
  186.   }
  187.   Ypos += Yvel;
  188.   Yvel += G;
  189.   if (Ypos < Ymin) {
  190.     Ypos = Ymin;
  191.     if (vel0 == -100.0)
  192.       vel0 = fabs(Yvel);
  193.     Yvel = vel0;
  194.   }
  195.   glutPostRedisplay();
  196. }
  197.  
  198. void 
  199. visible(int vis)
  200. {
  201.   if (vis == GLUT_VISIBLE)
  202.     glutIdleFunc(idle);
  203.   else
  204.     glutIdleFunc(NULL);
  205. }
  206.  
  207. int main(int argc, char *argv[])
  208. {
  209.   glutInit(&argc, argv);
  210.   glutInitWindowPosition(0, 0);
  211.   glutInitWindowSize(600, 450);
  212.  
  213.  
  214.   IndexMode = argc > 1 && strcmp(argv[1], "-ci") == 0;
  215.   if (IndexMode)
  216.      glutInitDisplayMode(GLUT_INDEX | GLUT_DOUBLE);
  217.   else
  218.      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  219.  
  220.   glutCreateWindow("Bounce");
  221.   Ball = make_ball();
  222.   glCullFace(GL_BACK);
  223.   glEnable(GL_CULL_FACE);
  224.   glDisable(GL_DITHER);
  225.   glShadeModel(GL_FLAT);
  226.  
  227.   glutDisplayFunc(draw);
  228.   glutReshapeFunc(reshape);
  229.   glutVisibilityFunc(visible);
  230.   glutKeyboardFunc(key);
  231.  
  232.   if (IndexMode) {
  233.     glutSetColor(RED, 1.0, 0.0, 0.0);
  234.     glutSetColor(WHITE, 1.0, 1.0, 1.0);
  235.     glutSetColor(CYAN, 0.0, 1.0, 1.0);
  236.   }
  237.  
  238.   glutMainLoop();
  239.   return 0;             /* ANSI C requires main to return int. */
  240. }
  241.