home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / demos / bounce.c next >
C/C++ Source or Header  |  1996-05-27  |  3KB  |  198 lines

  1. /* bounce.c */
  2.  
  3.  
  4. /*
  5.  * Bouncing ball demo.
  6.  *
  7.  * Brian Paul
  8.  */
  9.  
  10.  
  11.  
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include "gltk.h"
  15.  
  16.  
  17.  
  18. #define COS(X)   cos( (X) * 3.14159/180.0 )
  19. #define SIN(X)   sin( (X) * 3.14159/180.0 )
  20.  
  21.  
  22. GLuint Ball;
  23. GLenum Mode;
  24. GLfloat Zrot = 0.0, Zstep = 6.0;
  25. GLfloat Xpos = 0.0, Ypos = 1.0;
  26. GLfloat Xvel = 0.2, Yvel = 0.0;
  27. GLfloat Xmin=-4.0, Xmax=4.0;
  28. GLfloat Ymin=-3.8, Ymax=4.0;
  29. GLfloat G = -0.1;
  30.  
  31.  
  32.  
  33. static GLuint make_ball( void )
  34. {
  35.    GLuint list;
  36.    GLfloat a, b;
  37.    GLfloat da = 18.0, db = 18.0;
  38.    GLfloat radius = 1.0;
  39.    GLuint color;
  40.    GLfloat x, y, z;
  41.  
  42.    list = glGenLists( 1 );
  43.  
  44.    glNewList( list, GL_COMPILE );
  45.  
  46.    color = 0;
  47.    for (a=-90.0;a+da<=90.0;a+=da) {
  48.  
  49.       glBegin( GL_QUAD_STRIP );
  50.       for (b=0.0;b<=360.0;b+=db) {
  51.  
  52.      if (color) {
  53.         TK_SETCOLOR( Mode, TK_RED );
  54.      }
  55.      else {
  56.         TK_SETCOLOR( Mode, TK_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.  
  81.  
  82. static void reshape( int width, int height )
  83. {
  84.    glViewport(0, 0, (GLint)width, (GLint)height);
  85.    glMatrixMode(GL_PROJECTION);
  86.    glLoadIdentity();
  87.    glOrtho( -6.0, 6.0, -6.0, 6.0, -6.0, 6.0 );
  88.    glMatrixMode(GL_MODELVIEW);
  89. }
  90.  
  91.  
  92. static GLenum key(int k, GLenum mask)
  93. {
  94.    switch (k) {
  95.       case TK_ESCAPE:
  96.      tkQuit();
  97.    }
  98.    return GL_FALSE;
  99. }
  100.  
  101.  
  102. static void draw( void )
  103. {
  104.    GLint i, j;
  105.  
  106.    glClear( GL_COLOR_BUFFER_BIT );
  107.  
  108.    TK_SETCOLOR( Mode, TK_CYAN );
  109.    glBegin( GL_LINES );
  110.    for (i=-5;i<=5;i++) {
  111.       glVertex2i( i, -5 );   glVertex2i( i, 5 );
  112.    }
  113.    for (i=-5;i<=5;i++) {
  114.       glVertex2i( -5,i );   glVertex2i( 5,i );
  115.    }
  116.    for (i=-5;i<=5;i++) {
  117.       glVertex2i( i, -5 );  glVertex2f( i*1.15, -5.9 );
  118.    }
  119.    glVertex2f( -5.3, -5.35 );    glVertex2f( 5.3, -5.35 );
  120.    glVertex2f( -5.75, -5.9 );     glVertex2f( 5.75, -5.9 );
  121.    glEnd();
  122.  
  123.  
  124.    glPushMatrix();
  125.    glTranslatef( Xpos, Ypos, 0.0 );
  126.    glScalef( 2.0, 2.0, 2.0 );
  127.    glRotatef( 8.0, 0.0, 0.0, 1.0 );
  128.    glRotatef( 90.0, 1.0, 0.0, 0.0 );
  129.    glRotatef( Zrot, 0.0, 0.0, 1.0 );
  130.  
  131.  
  132.    glCallList( Ball );
  133.  
  134.    glPopMatrix();
  135.  
  136.    glFlush();
  137.    tkSwapBuffers();
  138. }
  139.  
  140.  
  141. static void idle( void )
  142. {
  143.    static float vel0 = -100.0;
  144.  
  145.    Zrot += Zstep;
  146.  
  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.  
  159.    Ypos += Yvel;
  160.    Yvel += G;
  161.    if (Ypos<Ymin) {
  162.       Ypos = Ymin;
  163.       if (vel0==-100.0)  vel0 = fabs(Yvel);
  164.       Yvel = vel0;
  165.    }
  166.    draw();
  167. }
  168.  
  169.  
  170.  
  171. main( int argc, char *argv[] )
  172. {
  173.  
  174.    tkInitPosition(0, 0, 300, 300);
  175.  
  176.    Mode = (GLenum) TK_INDEX;
  177.    Mode |= TK_DOUBLE;
  178.    Mode |= TK_DIRECT;
  179.    tkInitDisplayMode(Mode);
  180.  
  181.    if (tkInitWindow("Bounce") == GL_FALSE) {
  182.       tkQuit();
  183.    }
  184.  
  185.    Ball = make_ball();
  186.    glCullFace( GL_BACK );
  187.    glEnable( GL_CULL_FACE );
  188.    glDisable( GL_DITHER );
  189.    glShadeModel( GL_FLAT );
  190.  
  191.    tkExposeFunc( reshape );
  192.    tkReshapeFunc( reshape );
  193.    tkKeyDownFunc( key );
  194.    tkIdleFunc( idle );
  195.    tkDisplayFunc( draw );
  196.    tkExec();
  197. }
  198.