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

  1. /* vgears. */
  2.  
  3. /*
  4.  * Spinning gears demo for Linux SVGA/Mesa interface in 32K color mode.
  5.  *
  6.  * Compile with:  gcc vgears.c -I../include -L../lib -lMesaGL -lX11 -lXext
  7.  *   -lvga -lm -o vgears
  8.  *
  9.  * Brian Paul, January 1996
  10.  */
  11.  
  12.  
  13. #include <vga.h>
  14. #include <math.h>
  15. #include "GL/svgamesa.h"
  16. #include "GL/gl.h"
  17.  
  18.  
  19. int width = 800, height = 600;
  20.  
  21. SVGAMesaContext vmc;
  22.  
  23.  
  24.  
  25. /*
  26.  * Draw a gear wheel.  You'll probably want to call this function when
  27.  * building a display list since we do a lot of trig here.
  28.  *
  29.  * Input:  inner_radius - radius of hole at center
  30.  *         outer_radius - radius at center of teeth
  31.  *         width - width of gear
  32.  *         teeth - number of teeth
  33.  *         tooth_depth - depth of tooth
  34.  */
  35. static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  36.           GLint teeth, GLfloat tooth_depth )
  37. {
  38.    GLint i;
  39.    GLfloat r0, r1, r2;
  40.    GLfloat angle, da;
  41.    GLfloat u, v, len;
  42.  
  43.    r0 = inner_radius;
  44.    r1 = outer_radius - tooth_depth/2.0;
  45.    r2 = outer_radius + tooth_depth/2.0;
  46.  
  47.    da = 2.0*M_PI / teeth / 4.0;
  48.  
  49.    glShadeModel( GL_FLAT );
  50.  
  51.    glNormal3f( 0.0, 0.0, 1.0 );
  52.  
  53.    /* draw front face */
  54.    glBegin( GL_QUAD_STRIP );
  55.    for (i=0;i<=teeth;i++) {
  56.       angle = i * 2.0*M_PI / teeth;
  57.       glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  58.       glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  59.       glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  60.       glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  61.    }
  62.    glEnd();
  63.  
  64.    /* draw front sides of teeth */
  65.    glBegin( GL_QUADS );
  66.    da = 2.0*M_PI / teeth / 4.0;
  67.    for (i=0;i<teeth;i++) {
  68.       angle = i * 2.0*M_PI / teeth;
  69.  
  70.       glVertex3f( r1*cos(angle),      r1*sin(angle),      width*0.5 );
  71.       glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),   width*0.5 );
  72.       glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  73.       glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  74.    }
  75.    glEnd();
  76.  
  77.  
  78.    glNormal3f( 0.0, 0.0, -1.0 );
  79.  
  80.    /* draw back face */
  81.    glBegin( GL_QUAD_STRIP );
  82.    for (i=0;i<=teeth;i++) {
  83.       angle = i * 2.0*M_PI / teeth;
  84.       glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  85.       glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  86.       glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  87.       glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  88.    }
  89.    glEnd();
  90.  
  91.    /* draw back sides of teeth */
  92.    glBegin( GL_QUADS );
  93.    da = 2.0*M_PI / teeth / 4.0;
  94.    for (i=0;i<teeth;i++) {
  95.       angle = i * 2.0*M_PI / teeth;
  96.  
  97.       glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  98.       glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  99.       glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),   -width*0.5 );
  100.       glVertex3f( r1*cos(angle),      r1*sin(angle),      -width*0.5 );
  101.    }
  102.    glEnd();
  103.  
  104.  
  105.    /* draw outward faces of teeth */
  106.    glBegin( GL_QUAD_STRIP );
  107.    for (i=0;i<teeth;i++) {
  108.       angle = i * 2.0*M_PI / teeth;
  109.  
  110.       glVertex3f( r1*cos(angle),      r1*sin(angle),       width*0.5 );
  111.       glVertex3f( r1*cos(angle),      r1*sin(angle),      -width*0.5 );
  112.       u = r2*cos(angle+da) - r1*cos(angle);
  113.       v = r2*sin(angle+da) - r1*sin(angle);
  114.       len = sqrt( u*u + v*v );
  115.       u /= len;
  116.       v /= len;
  117.       glNormal3f( v, -u, 0.0 );
  118.       glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),    width*0.5 );
  119.       glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),   -width*0.5 );
  120.       glNormal3f( cos(angle), sin(angle), 0.0 );
  121.       glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da),  width*0.5 );
  122.       glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  123.       u = r1*cos(angle+3*da) - r2*cos(angle+2*da);
  124.       v = r1*sin(angle+3*da) - r2*sin(angle+2*da);
  125.       glNormal3f( v, -u, 0.0 );
  126.       glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da),  width*0.5 );
  127.       glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  128.       glNormal3f( cos(angle), sin(angle), 0.0 );
  129.    }
  130.  
  131.    glVertex3f( r1*cos(0), r1*sin(0), width*0.5 );
  132.    glVertex3f( r1*cos(0), r1*sin(0), -width*0.5 );
  133.  
  134.    glEnd();
  135.  
  136.  
  137.    glShadeModel( GL_SMOOTH );
  138.  
  139.    /* draw inside radius cylinder */
  140.    glBegin( GL_QUAD_STRIP );
  141.    for (i=0;i<=teeth;i++) {
  142.       angle = i * 2.0*M_PI / teeth;
  143.       glNormal3f( -cos(angle), -sin(angle), 0.0 );
  144.       glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  145.       glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  146.    }
  147.    glEnd();
  148.       
  149. }
  150.  
  151.  
  152. static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0;
  153. static GLint gear1, gear2, gear3;
  154. static GLfloat angle = 0.0;
  155.  
  156. static GLuint limit;
  157. static GLuint count = 1;
  158.  
  159.  
  160. static void draw( void )
  161. {
  162.    angle += 2.0;
  163.  
  164.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  165.  
  166.    glPushMatrix();
  167.    glRotatef( view_rotx, 1.0, 0.0, 0.0 );
  168.    glRotatef( view_roty, 0.0, 1.0, 0.0 );
  169.    glRotatef( view_rotz, 0.0, 0.0, 1.0 );
  170.  
  171.    glPushMatrix();
  172.    glTranslatef( -3.0, -2.0, 0.0 );
  173.    glRotatef( angle, 0.0, 0.0, 1.0 );
  174.    glCallList(gear1);
  175.    glPopMatrix();
  176.  
  177.    glPushMatrix();
  178.    glTranslatef( 3.1, -2.0, 0.0 );
  179.    glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
  180.    glCallList(gear2);
  181.    glPopMatrix();
  182.  
  183.    glPushMatrix();
  184.    glTranslatef( -3.1, 4.2, 0.0 );
  185.    glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 );
  186.    glCallList(gear3);
  187.    glPopMatrix();
  188.  
  189.    glPopMatrix();
  190.  
  191.    /*SVGAMesaSwapBuffers();*/
  192. }
  193.  
  194.  
  195. static void init( void )
  196. {
  197.    static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0 };
  198.    static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0 };
  199.    static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0 };
  200.    static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0 };
  201.  
  202.    GLfloat w = (float) width / (float) height;
  203.    GLfloat h = 1.0;
  204.  
  205.    glLightfv( GL_LIGHT0, GL_POSITION, pos );
  206.    glEnable( GL_CULL_FACE );
  207.    glEnable( GL_LIGHTING );
  208.    glEnable( GL_LIGHT0 );
  209.    glEnable( GL_DEPTH_TEST );
  210.  
  211.    /* make the gears */
  212.    gear1 = glGenLists(1);
  213.    glNewList(gear1, GL_COMPILE);
  214.    glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red );
  215.    gear( 1.0, 4.0, 1.0, 20, 0.7 );
  216.    glEndList();
  217.  
  218.    gear2 = glGenLists(1);
  219.    glNewList(gear2, GL_COMPILE);
  220.    glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
  221.    gear( 0.5, 2.0, 2.0, 10, 0.7 );
  222.    glEndList();
  223.  
  224.    gear3 = glGenLists(1);
  225.    glNewList(gear3, GL_COMPILE);
  226.    glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue );
  227.    gear( 1.3, 2.0, 0.5, 10, 0.7 );
  228.    glEndList();
  229.  
  230.    glEnable( GL_NORMALIZE );
  231.  
  232.  
  233. /*   glViewport(0, 0, (GLint)width, (GLint)height);*/
  234.    glViewport( 0, 0, width, height );
  235.    glMatrixMode(GL_PROJECTION);
  236.    glLoadIdentity();
  237.    glFrustum( -w, w, -h, h, 5.0, 60.0 );
  238.    glMatrixMode(GL_MODELVIEW);
  239.    glLoadIdentity();
  240.    glTranslatef( 0.0, 0.0, -40.0 );
  241. }
  242.  
  243. void setup( void )
  244. {
  245.    vga_init();
  246.  
  247.    vga_setmode(G800x600x32K);
  248. /*   gl_setcontextvga(G800x600x32K);*/
  249.  
  250.    vmc = SVGAMesaCreateContext();
  251.    SVGAMesaMakeCurrent( vmc );
  252. }
  253.  
  254.  
  255. void end( void )
  256. {
  257.    SVGAMesaDestroyContext( vmc );
  258.  
  259.    vga_setmode( TEXT );
  260. }
  261.  
  262.  
  263. int main( int argc, char *argv[] )
  264. {
  265.    int i;
  266.  
  267.    setup();
  268.    init();
  269.    for (i=0;i<10;i++) {
  270.       draw();
  271.    }
  272.    end();
  273.    return 0;
  274. }
  275.