home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / EXAMPLES / SHOWG.C < prev    next >
C/C++ Source or Header  |  2000-02-11  |  5KB  |  295 lines

  1. #include <stdio.h>
  2. #include "vogle.h"
  3.  
  4.  
  5. #define        GEOM        1
  6. #define        TOPLEFT        2
  7. #define        TOPRIGHT    3
  8. #define        BOTTOMLEFT    4
  9. #define        BOTTOMRIGHT    5
  10. #define        MIN(x, y)    ((x) < (y) ? (x) : (y))
  11. #define        MAX(x, y)    ((x) > (y) ? (x) : (y))
  12.  
  13. typedef struct v {
  14.     float    x, y, z;
  15. } vertex;
  16.  
  17. static    char    title[128] = "G'day there mate";
  18.  
  19. FILE     *fp;
  20.  
  21.  
  22. /*
  23.  * Demonstrate just how much you can put in an object
  24.  * this program will read geometry files in the off format.
  25.  */
  26. main(argc, argv)
  27.     int    argc;
  28.     char    **argv;
  29. {
  30.     int    i, backface_on = 0, direction = 0;
  31.     char    *filename = (char *)NULL;
  32.  
  33.     if (argc < 2) {
  34.         fprintf(stderr,"Usage: %s [-b] [-a] filename\n", argv[0]);
  35.         exit(1);
  36.     }
  37.  
  38.     for (i = 1; i < argc; i++) {
  39.         if (strcmp(argv[i], "-b") == 0)
  40.             backface_on = 1;
  41.         if (strcmp(argv[i], "-a") == 0)
  42.             direction = 1;
  43.         else
  44.             filename = argv[i];
  45.     }
  46.         
  47.     /*
  48.      * Uses the environment variable VDEVICE to find device....
  49.      */
  50.     vinit(0);
  51.  
  52.     backface(backface_on);
  53.     backfacedir(direction);
  54.  
  55.     font("times.r");
  56.     textsize(0.3, 0.6);
  57.  
  58.     if (filename) {
  59.         if ((fp = fopen(filename, "r")) == (FILE *)NULL) {
  60.             vexit();
  61.             fprintf(stderr, "Couldn't open %s for reading\n", filename);
  62.             exit(1);
  63.         }
  64.         makeobject();
  65.     } else 
  66.         makecube();
  67.  
  68.     color(BLACK);
  69.     clear();
  70.  
  71.     /*
  72.      * set up an object which draws in the top left of the screen.
  73.      */
  74.     makeobj(TOPLEFT);
  75.         viewport(-1.0, 0.0, 0.0, 1.0);
  76.         ortho2(-5.0, 5.0, -5.0, 5.0);
  77.  
  78.         color(RED);
  79.  
  80.         rect(-5.0, -5.0, 5.0, 5.0);
  81.         move2(-4.8, -4.8);
  82.         color(WHITE);
  83.         drawstr(title);
  84.  
  85.         perspective(50.0, 1.0, 0.1, 1000.0);
  86.         lookat(5.0, 8.0, 5.0, 0.0, 0.0, 0.0, 0.0);
  87.  
  88.         color(RED);
  89.         callobj(GEOM);
  90.  
  91.     closeobj();
  92.  
  93.     /*
  94.      * now set up one which draws in the top right of the screen
  95.      */
  96.     makeobj(TOPRIGHT);
  97.         viewport(0.0, 1.0, 0.0, 1.0);
  98.         ortho2(-5.0, 5.0, -5.0, 5.0);
  99.  
  100.         color(GREEN);
  101.  
  102.         rect(-5.0, -5.0, 5.0, 5.0);
  103.         move2(-4.8, -4.8);
  104.         color(WHITE);
  105.         drawstr(title);
  106.  
  107.         window(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
  108.         lookat(5.0, 8.0, 5.0, 0.0, 0.0, 0.0, 90.0);
  109.  
  110.         color(GREEN);
  111.         callobj(GEOM);
  112.  
  113.         color(RED);
  114.  
  115.     closeobj();
  116.  
  117.     /*
  118.      * try the bottom left
  119.      */
  120.     makeobj(BOTTOMLEFT);
  121.         viewport(-1.0, 0.0, -1.0, 0.0);
  122.         ortho2(-5.0, 5.0, -5.0, 5.0);
  123.  
  124.         color(MAGENTA);
  125.  
  126.         rect(-5.0, -5.0, 5.0, 5.0);
  127.         move2(-4.8, -4.8);
  128.         color(WHITE);
  129.         drawstr(title);
  130.  
  131.         perspective(40.0, 1.0, 0.1, 1000.0);
  132.         polarview(15.0, 30.0, 30.0, 30.0);
  133.  
  134.         color(MAGENTA);
  135.         callobj(GEOM);
  136.  
  137.         color(YELLOW);
  138.  
  139.     closeobj();
  140.  
  141.     /*
  142.      * and the bottom right
  143.      */
  144.     makeobj(BOTTOMRIGHT);
  145.         viewport(0.0, 1.0, -1.0, 0.0);
  146.         ortho2(-5.0, 5.0, -5.0, 5.0);
  147.  
  148.         color(CYAN);
  149.  
  150.         rect(-5.0, -5.0, 5.0, 5.0);
  151.         move2(-4.8, -4.8);
  152.         color(WHITE);
  153.         drawstr(title);
  154.  
  155.         window(-6.0, 6.0, -6.0, 6.0, -6.0, 6.0);
  156.         polarview(18.0, -38.0, -19.0, 18.0);
  157.  
  158.         color(CYAN);
  159.         callobj(GEOM);
  160.  
  161.         color(BLUE);
  162.  
  163.     closeobj();
  164.  
  165.     /*
  166.      * now draw them
  167.      */
  168.     callobj(TOPLEFT);
  169.     callobj(TOPRIGHT);
  170.     callobj(BOTTOMLEFT);
  171.     callobj(BOTTOMRIGHT);
  172.  
  173.     getkey();
  174.  
  175.     vexit();
  176. }
  177.  
  178. /*
  179.  * makeobject
  180.  *
  181.  *    read in and set up set up the object
  182.  */
  183. makeobject()
  184. {
  185.     float        fmax, fmin;
  186.     int        i, j, verts, connections, edges, npoints, pointno;
  187.     vertex        *vlist;
  188.  
  189.     makeobj(GEOM);
  190.  
  191.         if (fscanf(fp, "%d %d %d\n", &verts, &connections, &edges) != 3)
  192.             read_err("nverts npoly nedges");
  193.  
  194.         vlist = (vertex *)malloc(sizeof(vertex) * verts);
  195.  
  196.         fmax = -1.0e20;
  197.         fmin = 1.0e20;
  198.  
  199.         for (i = 0; i != verts; i++) {
  200.             if (fscanf(fp, "%f %f %f\n", &vlist[i].x, &vlist[i].y, &vlist[i].z) != 3)
  201.                 read_err("coodinate information");
  202.  
  203.             fmin = MIN(fmin, vlist[i].x);
  204.             fmin = MIN(fmin, vlist[i].y);
  205.             fmin = MIN(fmin, vlist[i].z);
  206.  
  207.             fmax = MAX(fmax, vlist[i].x);
  208.             fmax = MAX(fmax, vlist[i].y);
  209.             fmax = MAX(fmax, vlist[i].z);
  210.         }
  211.  
  212.         /* 
  213.          * Scale the object.
  214.          */
  215.         fmax = fmax - fmin;
  216.         scale(6.0 / fmax, 6.0 / fmax, 6.0 / fmax);
  217.  
  218.         for (i = 0; i != connections; i++) {
  219.             if (fscanf(fp, "%d", &npoints) != 1)
  220.                 read_err("connectivity 1");
  221.  
  222.             makepoly();
  223.                 if (fscanf(fp, "%d", &pointno) != 1)
  224.                     read_err("connectivity 2");
  225.  
  226.                 pointno--;
  227.                 move(vlist[pointno].x, vlist[pointno].y, vlist[pointno].z);
  228.                 for (j = 1; j != npoints; j++) {
  229.                     if (fscanf(fp, "%d", &pointno) != 1)
  230.                         read_err("connectivity 3");
  231.  
  232.                     pointno--;
  233.                     draw(vlist[pointno].x, vlist[pointno].y, vlist[pointno].z);
  234.                 }
  235.             closepoly();
  236.         }
  237.  
  238.     closeobj();
  239. }
  240.  
  241. /*
  242.  * makecube
  243.  *
  244.  *    set up a cube
  245.  */
  246. makecube()
  247. {
  248.  
  249.     makeobj(GEOM);
  250.  
  251.         /*
  252.          * The border around the cube
  253.          */
  254.         rect(-5.0, -5.0, 10.0, 10.0);
  255.  
  256.         /*
  257.          * Make the cube from 4 squares
  258.          */
  259.         pushmatrix();
  260.             side();
  261.             rotate(90.0, 'x');
  262.             side();
  263.             rotate(90.0, 'x');
  264.             side();
  265.             rotate(90.0, 'x');
  266.             side();
  267.         popmatrix();
  268.  
  269.         move2(-4.5, -4.5);
  270.         drawstr(title);
  271.     closeobj();
  272. }
  273.  
  274. /*
  275.  * side
  276.  *
  277.  *    define a face for the cube
  278.  */
  279. side()
  280. {
  281.     pushmatrix();
  282.         translate(0.0, 0.0, 1.0);
  283.         rect(-1.0, -1.0, 1.0, 1.0);
  284.     popmatrix();
  285. }
  286.  
  287.  
  288. read_err(s)
  289.     char *s;
  290. {
  291.     vexit();
  292.     fprintf(stderr,"Error reading input file near %s\n", s);
  293.     exit(1);
  294. }
  295.