home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / demos / bounce.c next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  2.7 KB  |  176 lines

  1. /* bounce.c */
  2.  
  3.  
  4. /*
  5.  * Bouncing ball demo.
  6.  */
  7.  
  8.  
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include "tk.h"
  12.  
  13.  
  14.  
  15. #define COS(X)   cos( (X) * 3.14159/180.0 )
  16. #define SIN(X)   sin( (X) * 3.14159/180.0 )
  17.  
  18.  
  19. GLuint Ball;
  20. GLenum Mode;
  21. GLfloat Zrot = 0.0, Zstep = 6.0;
  22. GLfloat Xpos = 0.0, Ypos = 1.0;
  23. GLfloat Xvel = 0.2, Yvel = 0.0;
  24. GLfloat Xmin=-4.0, Xmax=4.0;
  25. GLfloat Ymin=-4.0, Ymax=4.0;
  26. GLfloat G = -0.1;
  27.  
  28.  
  29.  
  30. static GLuint make_ball( void )
  31. {
  32.    GLuint list;
  33.    GLfloat a, b;
  34.    GLfloat da = 18.0, db = 18.0;
  35.    GLfloat radius = 1.0;
  36.    GLuint color;
  37.    GLfloat x, y, z;
  38.  
  39.    list = glGenLists( 1 );
  40.  
  41.    glNewList( list, GL_COMPILE );
  42.  
  43.    color = 0;
  44.    for (a=-90.0;a+da<=90.0;a+=da) {
  45.  
  46.       glBegin( GL_QUAD_STRIP );
  47.       for (b=0.0;b<=360.0;b+=db) {
  48.  
  49.      if (color) {
  50.         TK_SETCOLOR( Mode, TK_RED );
  51.      }
  52.      else {
  53.         TK_SETCOLOR( Mode, TK_WHITE );
  54.      }
  55.  
  56.      x = COS(b) * COS(a);
  57.      y = SIN(b) * COS(a);
  58.      z = SIN(a);
  59.      glVertex3f( x, y, z );
  60.  
  61.      x = radius * COS(b) * COS(a+da);
  62.      y = radius * SIN(b) * COS(a+da);
  63.      z = radius * SIN(a+da);
  64.      glVertex3f( x, y, z );
  65.  
  66.      color = 1-color;
  67.       }
  68.       glEnd();
  69.  
  70.    }
  71.  
  72.    glEndList();
  73.  
  74.    return list;
  75. }
  76.  
  77.  
  78.  
  79. static void reshape( int width, int height )
  80. {
  81.    glViewport(0, 0, (GLint)width, (GLint)height);
  82.    glMatrixMode(GL_PROJECTION);
  83.    glLoadIdentity();
  84.    glOrtho( -6.0, 6.0, -6.0, 6.0, -6.0, 6.0 );
  85.    glMatrixMode(GL_MODELVIEW);
  86. }
  87.  
  88.  
  89. static GLenum key(int k, GLenum mask)
  90. {
  91.    switch (k) {
  92.       case TK_ESCAPE:
  93.      tkQuit();
  94.    }
  95.    return GL_FALSE;
  96. }
  97.  
  98.  
  99. static void idle( void )
  100. {
  101.    static float vel0 = -100.0;
  102.  
  103.    Zrot += Zstep;
  104.  
  105.    Xpos += Xvel;
  106.    if (Xpos>=Xmax) {
  107.       Xpos = Xmax;
  108.       Xvel = -Xvel;
  109.       Zstep = -Zstep;
  110.    }
  111.    if (Xpos<=Xmin) {
  112.       Xpos = Xmin;
  113.       Xvel = -Xvel;
  114.       Zstep = -Zstep;
  115.    }
  116.  
  117.    Ypos += Yvel;
  118.    Yvel += G;
  119.    if (Ypos<Ymin) {
  120.       Ypos = Ymin;
  121.       if (vel0==-100.0)  vel0 = fabs(Yvel);
  122.       Yvel = vel0;
  123.    }
  124. }
  125.  
  126.  
  127.  
  128. static void draw( void )
  129. {
  130.    glClear( GL_COLOR_BUFFER_BIT );
  131.  
  132.    glPushMatrix();
  133.    glTranslatef( Xpos, Ypos, 0.0 );
  134.    glScalef( 2.0, 2.0, 2.0 );
  135.    glRotatef( 8.0, 0.0, 0.0, 1.0 );
  136.    glRotatef( 90.0, 1.0, 0.0, 0.0 );
  137.    glRotatef( Zrot, 0.0, 0.0, 1.0 );
  138.  
  139.  
  140.    glCallList( Ball );
  141.  
  142.    glPopMatrix();
  143.  
  144.    glFlush();
  145.    tkSwapBuffers();
  146. }
  147.  
  148.  
  149. main( int argc, char *argv[] )
  150. {
  151.  
  152.    tkInitPosition(0, 0, 300, 300);
  153.  
  154.    Mode = (GLenum) TK_INDEX;
  155.    Mode |= TK_DOUBLE;
  156.    Mode |= TK_DIRECT;
  157.    tkInitDisplayMode(Mode);
  158.  
  159.    if (tkInitWindow("Bounce") == GL_FALSE) {
  160.       tkQuit();
  161.    }
  162.  
  163.    Ball = make_ball();
  164.    glCullFace( GL_BACK );
  165.    glEnable( GL_CULL_FACE );
  166.    glDisable( GL_DITHER );
  167.    glShadeModel( GL_FLAT );
  168.  
  169.    tkExposeFunc( reshape );
  170.    tkReshapeFunc( reshape );
  171.    tkKeyDownFunc( key );
  172.    tkIdleFunc( idle );
  173.    tkDisplayFunc( draw );
  174.    tkExec();
  175. }
  176.