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

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