[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
GL3.2 for AIX: Graphics Library (GL) Technical Reference

octahedron.c Example C Language Program

/* Example: octahedron.c */
/* This example program draws a multicolored octahedron spinning
 * like a top. The drawoctahedron subroutine demonstrates how
 * triangle meshes, including the swaptmesh() subroutine,
 * are used.
 * The c3f() subroutine sets vertex colors; you can ignore these
 * calls if you are studying the logic of mesh drawing.
 * All rotation and hidden surface removal are handled in the
 * main() routine.  The calculations of the rotation angles are
 * based on Euler's equations for a spinning top.
 * On adapters without a z-buffer, this program will not display
 * the octahedron correctly, since it employs the z-buffer to
 * remove hidden surfaces.
 */
#include <gl/gl.h>
float octdata[6][3] = {   /* positions of the six vertices of octahedron */
{
   1.0, 0.0, 0.0},
   {0.0, 1.0, 0.0},
   {0.0, 0.0, 1.0},
   {-1.0, 0.0, 0.0},
   {0.0, -1.0, 0.0},
   {0.0, 0.0, -1.0}
};
float colordata[6][3] = {  /* colors of the vertices */
   {1.0, 0.0, 0.0},         /* red */
   {0.0, 1.0, 0.0},         /* green */
   {0.0, 0.0, 1.0},         /* blue */
   {0.0, 1.0, 1.0},         /* cyan */
   {1.0, 0.0, 1.0},         /* magenta */
   {1.0, 1.0, 0.0}
};          /* yellow */
void drawoctahedron()
{
   bgntmesh();              /* use triangle mesh to draw the
                               octahedron */
   c3f (colordata[0]);
   v3f (octdata [0]);
   c3f (colordata[1]);
   v3f (octdata[1]);
   c3f (colordata[2]);
   v3f (octdata[2]);        /* vertices in first triangle are
                                   0, 1, 2 */
   c3f (colordata[3]);
   v3f (octdata[3]);        /* second triangle - 1, 2, 3 */
   swaptmesh();             /* swap vertex order (2,3) -> (3,2) */
        c3f (colordata[4]);
   v3f (octdata[4]);        /* third triangle - 3, 2, 4 */
   c3f (colordata[0]);
   v3f (octdata[0]);        /* fourth triangle - 2, 4, 0 */
   swaptmesh();             /* swap vertex order (4,0) -> (0,4) */
   c3f (colordata[5]);
   v3f (octdata[5]);        /* fifth triangle - 0, 4, 5 */
   c3f (colordata[3]);
   v3f (octdata[3]);        /* sixth triangle - 4, 5, 3 */
   swaptmesh();             /* swap vertex order (5,3) -> (3,5) */
   c3f (colordata[1]);
   v3f (octdata[1]);        /* seventh trinagle - 3, 5, 1 */
   c3f (colordata[0]);
   v3f (octdata [0]);       /* eigth triangle - 5, 1, 0 */
   endtmesh();
}
main()
{
   long prec=0, spin=0;
   long i;   
   prefposition(100, 500, 100, 500);   /* location of window to
                                          be opened */
   winopen("octahedron");   
   ortho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
   zbuffer(TRUE);      /* hidden surfaces removed with z buffer */
   doublebuffer();     /* for smooth motion */
   RGBmode();          /* direct color mode */
   gconfig();          /* reconfigure for RGBmode and
                          doublebuffer */
   for (i=0; i<1500; i++) {
      pushmatrix();                 /* save viewing transformation */
      translate (0.0, -1.0, 0.0);   /* make origin the
                                       bottom of the oct */
      rotate (prec, 'y');           /* precession of axis */
      rotate (323, 'x');            /* inclination of 32.3
                                       degrees */
      rotate (spin, 'y');           /* spin around axis */
      translate (0.0, 1.0, 0.0);    /* center in the middle of
                                       the window */
      prec += 15;
      spin += 45;
      if (prec > 3600) prec -= 3600;
      if (spin > 3600) spin -= 3600;      
      cpack(0);                     /* color black */
      clear();
      zclear();                     /* clear the z buffer */
      drawoctahedron();
      swapbuffers();                /* show completed drawing */
      popmatrix();                  /* restore viewing
                                       transformation */
   }
}

Related Information

Triangular Meshes in GL3.2 Version 4 for AIX: Programming Concepts explains how to specify three-dimensional objects that are composed of triangular faces.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]