home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / voglew.zip / TETRA.C < prev    next >
Text File  |  1993-05-28  |  3KB  |  184 lines

  1. /*
  2.  * Demonstrate a rotating translating tetrahedron.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #ifdef TC
  7. extern double sin(), cos();
  8. #else
  9. #include <math.h>
  10. #endif
  11. #include "vogle.h"
  12.  
  13. #define    TETRAHEDRON    1
  14. #define    NSIDES    3
  15. #define    NFACES    4
  16. #define    NPNTS    4
  17.  
  18. float    pnts[NPNTS][3] = {
  19.     {-0.5, 0.866, -0.667},
  20.     {-0.5, -0.866, -0.667},
  21.     { 1.0, 0.0, -0.667},
  22.     { 0.0, 0.0, 1.334}
  23. };
  24.  
  25. int    faces[NFACES][NSIDES] = {
  26.     {2, 1, 0},
  27.     {0, 1, 3},
  28.     {1, 2, 3},
  29.     {2, 0, 3}
  30. };
  31.  
  32. int    colface[NFACES] = {
  33.         GREEN,
  34.         YELLOW,
  35.         CYAN,
  36.         MAGENTA
  37. };
  38.  
  39. main(argc, argv)
  40.     int    argc;
  41.     char    **argv;
  42. {
  43.     char    c;
  44.     int    i;
  45.     float    rotval = 0.0, drotval = 5.0, zeye = 5.0;
  46.     float    R = 1.6, tx = 0.0, tz = R;
  47.     int    do_backface;
  48.     int    do_backfacedir;
  49.     int    do_fill;
  50.     char    device[10], *p;
  51.  
  52.     prefsize(200, 200);
  53.  
  54.     vinit("mswin");          /* set up device */
  55.  
  56.     do_fill = 1;
  57.     do_backface = 1;
  58.     do_backfacedir = 1;
  59.  
  60.     /*
  61.      * Make the tetrahedral object
  62.      */
  63.     maketheobject();
  64.  
  65.     /*
  66.      * If there is a command line arg to use filled polygons
  67.      */
  68.     polyfill(do_fill);
  69.     backface(do_backface);
  70.     backfacedir(do_backfacedir);
  71.  
  72.     /*
  73.      * set up a perspective projection with a field of view of
  74.      * 40.0 degrees, aspect ratio of 1.0, near clipping plane 0.1,
  75.      * and the far clipping plane at 1000.0.
  76.      */
  77.     perspective(40.0, 1.0, 0.001, 15.0);
  78.     lookat(0.0, 0.0, zeye, 0.0, 0.0, 0.0, 0.0);
  79.  
  80.     /*
  81.      * Setup drawing into the backbuffer....
  82.      */
  83.  
  84.     if (backbuffer() < 0) {
  85.         verror("The device  can't support doublebuffering\n");
  86.     }
  87.  
  88.     do {
  89.         for (rotval = 0.0; rotval < 360.0; rotval += drotval) {
  90.             color(BLACK);
  91.             clear();
  92.  
  93.             /*
  94.              * Rotate the whole scene...(this acumulates - hence
  95.              * drotval)
  96.              */
  97.             rotate(drotval * 0.1, 'x');
  98.             rotate(drotval * 0.1, 'z');
  99.  
  100.             color(RED);
  101.             pushmatrix();
  102.                 polyfill(0);
  103.                 rotate(90.0, 'x');
  104.                 circle(0.0, 0.0, R);
  105.                 polyfill(do_fill);
  106.             popmatrix();
  107.  
  108.             color(BLUE);
  109.             move(0.0, 0.0, 0.0);
  110.             draw(tx, 0.0, tz);
  111.             
  112.             /*
  113.              * Remember! The order of the transformations is
  114.              * the reverse of what is specified here in between
  115.              * the pushmatrix and the popmatrix. These ones don't
  116.              * accumulate because of the push and pop.
  117.              */
  118.             pushmatrix();
  119.                 translate(tx, 0.0, tz);
  120.                 rotate(rotval, 'x');
  121.                 rotate(rotval, 'y');
  122.                 rotate(rotval, 'z');
  123.                 scale(0.4, 0.4, 0.4);
  124.                 callobj(TETRAHEDRON);
  125.             popmatrix();
  126.  
  127.             tz = R * cos((double)(rotval * 3.1415926535 / 180));
  128.             tx = R * sin((double)(rotval * 3.1415926535 / 180));
  129.  
  130.             swapbuffers();
  131.  
  132.             c = checkkey();
  133.  
  134.             if (c == 'b') {        
  135.                 do_backface = !do_backface;
  136.                 backface(do_backface);
  137.             } else if (c == 'f') {
  138.                 do_fill = !do_fill;
  139.                 polyfill(do_fill);
  140.             } else if (c == 'd') {
  141.                 do_backfacedir = !do_backfacedir;
  142.                 backfacedir(do_backfacedir);
  143.             } else if (c != 0) {
  144.                 vexit();
  145.                 exit(0);
  146.             }
  147.         }
  148.  
  149.     } while (1);
  150.         
  151.     vexit();
  152. }
  153.  
  154. /*
  155.  * maketheobject
  156.  *
  157.  *    generate a tetraedron as a series of move draws
  158.  */
  159. maketheobject()
  160. {
  161.     int    i, j;
  162.     float    x, y, z;
  163.  
  164.     makeobj(TETRAHEDRON);
  165.  
  166.         for (i = 0; i < NFACES; i++) {
  167.             color(colface[i]);
  168.             makepoly();
  169.                 x = pnts[faces[i][0]][0];
  170.                 y = pnts[faces[i][0]][1];
  171.                 z = pnts[faces[i][0]][2];
  172.                 move(x, y, z);
  173.                 for (j = 1; j < NSIDES; j++) {
  174.                     x = pnts[faces[i][j]][0];
  175.                     y = pnts[faces[i][j]][1];
  176.                     z = pnts[faces[i][j]][2];
  177.                     draw(x, y, z);
  178.                 }
  179.             closepoly();
  180.         }
  181.  
  182.     closeobj();
  183. }
  184.