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

  1. /* spin.c */
  2.  
  3.  
  4. /*
  5.  * Spinning box.  This program is in the public domain.
  6.  *
  7.  * Brian Paul
  8.  */
  9.  
  10.  
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include "gltk.h"
  14.  
  15.  
  16.  
  17.  
  18. static GLfloat Xrot, Xstep;
  19. static GLfloat Yrot, Ystep;
  20. static GLfloat Zrot, Zstep;
  21. static GLfloat Step = 5.0;
  22. static GLfloat Scale = 1.0;
  23. static GLuint Object;
  24.  
  25.  
  26.  
  27.  
  28. static GLuint make_object( void )
  29. {
  30.    GLuint list;
  31.  
  32.    list = glGenLists( 1 );
  33.  
  34.    glNewList( list, GL_COMPILE );
  35.  
  36.    glBegin( GL_LINE_LOOP );
  37.    glVertex3f(  1.0,  0.5, -0.4 );
  38.    glVertex3f(  1.0, -0.5, -0.4 );
  39.    glVertex3f( -1.0, -0.5, -0.4 );
  40.    glVertex3f( -1.0,  0.5, -0.4 );
  41.    glEnd();
  42.  
  43.    glBegin( GL_LINE_LOOP );
  44.    glVertex3f(  1.0,  0.5, 0.4 );
  45.    glVertex3f(  1.0, -0.5, 0.4 );
  46.    glVertex3f( -1.0, -0.5, 0.4 );
  47.    glVertex3f( -1.0,  0.5, 0.4 );
  48.    glEnd();
  49.  
  50.    glBegin( GL_LINES );
  51.    glVertex3f(  1.0,  0.5, -0.4 );   glVertex3f(  1.0,  0.5, 0.4 );
  52.    glVertex3f(  1.0, -0.5, -0.4 );   glVertex3f(  1.0, -0.5, 0.4 );
  53.    glVertex3f( -1.0, -0.5, -0.4 );   glVertex3f( -1.0, -0.5, 0.4 );
  54.    glVertex3f( -1.0,  0.5, -0.4 );   glVertex3f( -1.0,  0.5, 0.4 );
  55.    glEnd();
  56.  
  57.  
  58.    glEndList();
  59.  
  60.    return list;
  61. }
  62.  
  63.  
  64.  
  65. static void reshape( int width, int height )
  66. {
  67.    glViewport(0, 0, (GLint)width, (GLint)height);
  68.    glMatrixMode(GL_PROJECTION);
  69.    glLoadIdentity();
  70.    glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
  71.    glMatrixMode(GL_MODELVIEW);
  72. }
  73.  
  74.  
  75. static GLenum key(int k, GLenum mask)
  76. {
  77.    switch (k) {
  78.       case TK_ESCAPE:
  79.      tkQuit();
  80.    }
  81.    return GL_FALSE;
  82. }
  83.  
  84.  
  85. static void draw( void )
  86. {
  87.    glClear( GL_COLOR_BUFFER_BIT );
  88.  
  89.    glPushMatrix();
  90.  
  91.    glTranslatef( 0.0, 0.0, -10.0 );
  92.    glScalef( Scale, Scale, Scale );
  93.    if (Xstep) {
  94.       glRotatef( Xrot, 1.0, 0.0, 0.0 );
  95.    }
  96.    else if (Ystep) {
  97.       glRotatef( Yrot, 0.0, 1.0, 0.0 );
  98.    }
  99.    else {
  100.       glRotatef( Zrot, 0.0, 0.0, 1.0 );
  101.    }
  102.  
  103.    glCallList( Object );
  104.  
  105.    glPopMatrix();
  106.  
  107.    glFlush();
  108.    tkSwapBuffers();
  109. }
  110.  
  111.  
  112. static void idle( void )
  113. {
  114.    Xrot += Xstep;
  115.    Yrot += Ystep;
  116.    Zrot += Zstep;
  117.  
  118.    if (Xrot>=360.0) {
  119.       Xrot = Xstep = 0.0;
  120.       Ystep = Step;
  121.    }
  122.    else if (Yrot>=360.0) {
  123.       Yrot = Ystep = 0.0;
  124.       Zstep = Step;
  125.    }
  126.    else if (Zrot>=360.0) {
  127.       Zrot = Zstep = 0.0;
  128.       Xstep = Step;
  129.    }
  130.    draw();
  131. }
  132.  
  133.  
  134.  
  135.  
  136. main( int argc, char *argv[] )
  137. {
  138.    tkInitPosition(0, 0, 300, 300);
  139.  
  140.    tkInitDisplayMode( TK_DOUBLE | TK_DIRECT | TK_RGB );
  141.  
  142.    if (tkInitWindow("Spin") == GL_FALSE) {
  143.       tkQuit();
  144.    }
  145.  
  146.    Object = make_object();
  147.    glCullFace( GL_BACK );
  148. /*   glEnable( GL_CULL_FACE );*/
  149.    glDisable( GL_DITHER );
  150.    glShadeModel( GL_FLAT );
  151.  
  152.    glColor3f( 1.0, 1.0, 1.0 );
  153.  
  154.    Xrot = Yrot = Zrot = 0.0;
  155.    Xstep = Step;
  156.    Ystep = Zstep = 0.0;
  157.  
  158.    tkExposeFunc( reshape );
  159.    tkReshapeFunc( reshape );
  160.    tkKeyDownFunc( key );
  161.    tkIdleFunc( idle );
  162.    tkDisplayFunc( draw );
  163.    tkExec();
  164. }
  165.