home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / voglew.zip / PATCHES.C < prev    next >
C/C++ Source or Header  |  1993-05-28  |  5KB  |  259 lines

  1.  
  2. #include <stdio.h>
  3. #include "vogle.h"
  4.  
  5. /*
  6.  *    Draws patches of various bases
  7.  */
  8.  
  9. /*
  10.  * patch basis types
  11.  */
  12.  
  13. Matrix    bezier = {
  14.     {-1.0,    3.0,    -3.0,    1.0},
  15.     {3.0,    -6.0,    3.0,    0.0},
  16.     {-3.0,    3.0,    0.0,    0.0},
  17.     {1.0,    0.0,    0.0,    0.0} 
  18. };
  19.  
  20. Matrix    cardinal = { /* Or catmull-rom */
  21.     {-0.5,    1.5,    -1.5,    0.5},
  22.     {1.0,    -2.5,    2.0,    -0.5},
  23.     {-0.5,    0.0,    0.5,    0.0},
  24.     {0.0,    1.0,    0.0,    0.0}
  25. };
  26.  
  27. Matrix    bspline = {
  28.     {-1.0 / 6.0,    3.0 / 6.0,    -3.0 / 6.0,    1.0 / 6.0},
  29.     {3.0 / 6.0,    -6.0 / 6.0,    3.0 / 6.0,    0.0},
  30.     {-3.0 / 6.0,    0.0,        3.0 / 6.0,    0.0},
  31.     {1.0 / 6.0,    4.0 / 6.0,    1.0 / 6.0,    0.0}    
  32. };
  33.  
  34. Matrix    power = {
  35.     {1.0, 0.0, 0.0, 0.0},
  36.     {0.0, 1.0, 0.0, 0.0},
  37.     {0.0, 0.0, 1.0, 0.0},
  38.     {0.0, 0.0, 0.0, 1.0}
  39. };
  40.  
  41. float *basis[] = {    (float *)bezier,
  42.             (float *)cardinal,
  43.             (float *)bspline,
  44.             (float *)power
  45.         };
  46.  
  47. Matrix    x1 = {
  48.     {0.0,   0.2588,   0.5,   0.7071},
  49.     {0.0,   0.51764,  1.0,   1.4142},
  50.     {0.0,   0.51764,  1.0,   1.4142},
  51.     {0.0,   0.2588,   0.5,   0.7071}
  52. };
  53.  
  54. Matrix    y1 = {
  55.     {1.0,   0.966,   0.866,  0.7071},
  56.     {2.0,   1.9318,  1.732,  1.4142},
  57.     {2.0,   1.9318,  1.732,  1.4142},
  58.     {1.0,   0.966,   0.866,  0.7071}
  59. };
  60.  
  61. Matrix    z1 = {
  62.     {1.0,   1.0,     1.0,    1.0},
  63.     {1.0,   1.0,     1.0,    1.0},
  64.     {0.0,   0.0,     0.0,    0.0},
  65.     {0.0,   0.0,     0.0,    0.0}
  66. };
  67.  
  68. Matrix    x2 = {
  69.     {0.7071, 0.8660, 0.9660, 1.0},
  70.     {1.4142, 1.7320, 1.932,  2.0},
  71.     {1.4142, 1.7320, 1.932,  2.0},
  72.     {0.7071, 0.8660, 0.9660, 1.0}
  73. };
  74.  
  75. Matrix    y2 = {
  76.     {0.7071, 0.5,    0.2588, 0.0},
  77.     {1.4142, 1.0,    0.5176, 0.0},
  78.     {1.4142, 1.0,    0.5176, 0.0},
  79.     {0.7071, 0.5,    0.2588, 0.0}
  80. };
  81.  
  82. Matrix    z2 = {
  83.     {1.0,   1.0,     1.0,    1.0},
  84.     {1.0,   1.0,     1.0,    1.0},
  85.     {0.0,   0.0,     0.0,    0.0},
  86.     {0.0,   0.0,     0.0,    0.0}
  87. };
  88.  
  89. char *labels[] = {
  90.         "Bezier Patch(es)",
  91.         "Cardinal Patch(es)",
  92.         "B-Spline Patch(es)",
  93.         "'Power' Patch(es)"
  94.         };
  95.  
  96. /*
  97.  * demonstrate patches
  98.  */
  99.  
  100. main()
  101. {
  102.     int    i;
  103.  
  104.     vinit("mswin");
  105.  
  106.     color(BLACK);
  107.     clear();
  108.  
  109.     /*
  110.      * Set up two viewports (They actually overlap)
  111.      */
  112.  
  113.     viewport(-1.0, 0.3, -1.0, 0.3);
  114.     ortho(-2.0, 5.0, -2.0, 5.0, -2.0, 5.0);
  115.     lookat(0.0, 0.0, 0.0, -3.0, 2.0, -4.0, 0.0);
  116.     /*
  117.      * Save it 
  118.      */
  119.     pushviewport();
  120.     pushmatrix();
  121.  
  122.     viewport(-0.3, 1.0, -0.3, 1.0);
  123.     ortho(-2.0, 5.0, -2.0, 5.0, -2.0, 5.0);
  124.     lookat(0.0, 0.0, 0.0, 3.0, 2.0, -4.0, 0.0);
  125.  
  126.     textsize(0.4, 0.4);
  127.  
  128.     /*
  129.      * patchcurves provides a number of curves in the t and u
  130.      * directions. patchprecision gives the minimum number of line
  131.      * segments making up the curves in the t and u directions. The
  132.      * actual number of linesegments in t or u is equal to the closest
  133.      * integer multiple of the number of curves, > nsegs, in t or u,
  134.      * greater than or equal to the number set by patchprecision in u or
  135.      * t. eg. curves in t will be made up of 21 line segments so that we
  136.      * can match up the 7 curves in u; curves in u will have 24 as 4 by 5
  137.      * gives 20.
  138.      */
  139.     patchcurves(4, 7);
  140.     patchprecision(20, 20);
  141.  
  142.     for (i = 0; i < 4; i++) {
  143.  
  144.         axes();
  145.  
  146.  
  147.         /*
  148.          * patchbasis sets the basis matrices for the t and u
  149.          * functions
  150.          * 
  151.          */
  152.         patchbasis(basis[i], basis[i]);
  153.  
  154.         /* 
  155.          * Draw with viewport 2
  156.          */
  157.         move(0.0, 4.0, 0.0);
  158.         drawstr(labels[i]);
  159.  
  160.         /*
  161.          * now draw the patches according to the geometry matrices in
  162.          * x1, y1, and z1, x2, y2, z2.
  163.          */
  164.         drawhull(x1, y1, z1);
  165.         patch(x1, y1, z1);
  166.  
  167.         drawhull(x2, y2, z2);
  168.         patch(x2, y2, z2);
  169.  
  170.         /*
  171.          * Now with viewport 1
  172.          */
  173.         popviewport();
  174.         popmatrix();
  175.  
  176.         axes();
  177.  
  178.         move(0.0, 4.0, 0.0);
  179.         drawstr(labels[i]);
  180.  
  181.         /*
  182.          * now draw the patches according to the geometry matrices in
  183.          * x1, y1, and z1, x2, y2, z2.
  184.          */
  185.         drawhull(x1, y1, z1);
  186.         patch(x1, y1, z1);
  187.  
  188.         drawhull(x2, y2, z2);
  189.         patch(x2, y2, z2);
  190.  
  191.         getkey();
  192.  
  193.         /*
  194.          * Save viewport 1 again and reset to viewport 2
  195.          */
  196.         pushviewport();
  197.         pushmatrix();
  198.  
  199.         viewport(-0.3, 1.0, -0.3, 1.0);
  200.         ortho(-1.5, 5.0, -1.5, 5.0, -1.5, 5.0);
  201.         lookat(0.0, 0.0, 0.0, 3.0, 2.0, -4.0, 0.0);
  202.  
  203.         color(BLACK);
  204.         clear();
  205.     }
  206.  
  207.     vexit();
  208. }
  209.  
  210. /*
  211.  * drawhull
  212.  *
  213.  *    draw the hull for x, y, and z.
  214.  */
  215. drawhull(x, y, z)
  216.     Matrix    x, y, z;
  217. {
  218.     int    i, j;
  219.     char    buf[2];
  220.  
  221.     color(MAGENTA);    
  222.  
  223.     for (i = 0; i < 4; i++) {
  224.         move(x[i][0], y[i][0], z[i][0]);
  225.         for (j = 1; j < 4; j++)
  226.             draw(x[i][j], y[i][j], z[i][j]);
  227.     }
  228.  
  229.     for (i = 0; i < 4; i++) {
  230.         move(x[0][i], y[0][i], z[0][i]);
  231.         for (j = 1; j < 4; j++) 
  232.             draw(x[j][i], y[j][i], z[j][i]);
  233.     }
  234.  
  235.     /* 
  236.       * Set color for The patch
  237.      */
  238.     color(GREEN);
  239. }
  240.  
  241. /*
  242.  * axes
  243.  *
  244.  *    draw the axes
  245.  */
  246.  
  247. axes()
  248. {
  249.     color(YELLOW);
  250.     move(0.0, 0.0, 0.0);
  251.     draw(4.0, 0.0, 0.0);
  252.  
  253.     move(0.0, 0.0, 0.0);
  254.     draw(0.0, 4.0, 0.0);
  255.  
  256.     move(0.0, 0.0, 0.0);
  257.     draw(0.0, 0.0, 4.0);
  258. }
  259.