home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / SPD.3.0.shar.gz / SPD.3.0.shar / teapot.c < prev    next >
C/C++ Source or Header  |  1991-01-13  |  15KB  |  625 lines

  1. /*
  2.  * teapot.c - Tessellate the famous teapot into triangular patches, and sit the
  3.  *    object on top of a checkerboard which is meshed to the same degree.  In
  4.  *    other words, the number of squares on the checkerboard is the same as
  5.  *    the amount of meshing for each of the 32 patches on the teapot, with
  6.  *    each mesh quadrilateral on the teapot further split into two triangles.
  7.  *    Two light sources.
  8.  *
  9.  *    Note that the teapot should always be rendered as a double sided object
  10.  *    (since for some patches both sides can be seen).  (See IEEE CG&A
  11.  *    January 1987 for a history of the teapot.  Note that their vertex list
  12.  *    has duplicates, e.g. 93 and 271, as well as unused vertices, e.g.
  13.  *    205,206,216,223).  Also, note that the bottom (the last four patches)
  14.  *    is not flat - blame Frank Crow, not me.
  15.  *
  16.  * Author:  Eric Haines, 3D/Eye, Inc.
  17.  *
  18.  * SizeFactor determines the number of objects output.
  19.  *    Total patches = 32*2*n*n - 8*n     [degenerates are deleted]
  20.  *    Total squares = n*n
  21.  *
  22.  *    SizeFactor    # patches    # squares
  23.  *         1            56             1        [has more degenerates!]
  24.  *         2           240             4
  25.  *         3           552             9
  26.  *
  27.  *        12          9120           144
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include "def.h"
  33. #include "lib.h"
  34.  
  35. static    int    SizeFactor = 12 ;
  36.  
  37. /* comment out the next line to generate all patches except the bottom,
  38.  * i.e. the original Newell teapot
  39.  */
  40. #define BOTTOM
  41.  
  42. #ifdef    BOTTOM
  43. #define    NUM_PATCHES    32
  44. #else
  45. #define    NUM_PATCHES    28
  46. #endif
  47.  
  48. int    check_for_cusp() ;
  49.  
  50. main(argc,argv)
  51. int    argc ;
  52. char    *argv[] ;
  53. {
  54. COORD3    back_color ;
  55. COORD3    light ;
  56. COORD3    from, at, up ;
  57.  
  58.     if ( !lib_get_size( argc, argv, &SizeFactor ) ) {
  59.     fprintf( stderr, "usage: %s [size]\n", *argv ) ;
  60.     exit(EXIT_FAIL) ;
  61.     }
  62.  
  63.     if ( SizeFactor == 1 ) {
  64.     fprintf( stderr,
  65.       "warning: a size of 1 is not supported - use at your own risk\n" ) ;
  66.     }
  67.  
  68.     /* output viewpoint */
  69.     SET_COORD3( from, 4.86, 7.2, 5.4 ) ;
  70.     SET_COORD3( at, 0.0, 0.0, 0.0 ) ;
  71.     SET_COORD3( up, 0.0, 0.0, 1.0 ) ;
  72.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 512, 512 ) ;
  73.  
  74.     /* output background color - UNC sky blue */
  75.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  76.     lib_output_background_color( back_color ) ;
  77.  
  78.     /* output light sources */
  79.     SET_COORD3( light, -3.1, 9.8, 12.1 ) ;
  80.     lib_output_light( light ) ;
  81.     SET_COORD3( light, 11.3, 5.1, 8.8 ) ;
  82.     lib_output_light( light ) ;
  83.  
  84.     output_checkerboard() ;
  85.     output_teapot() ;
  86.     exit(EXIT_SUCCESS) ;
  87. }
  88.  
  89. output_checkerboard()
  90. {
  91. int    sstep, tstep ;
  92. COORD3    vert[4] ;
  93. COORD3    obj_color ;
  94.  
  95.     SET_COORD3( obj_color, 1.0, 1.0, 1.0 ) ;
  96.     lib_output_color( obj_color, 0.5, 0.5, 3.0, 0.0, 0.0 ) ;
  97.     for ( sstep = 0 ; sstep < SizeFactor ; sstep++ ) {
  98.     for ( tstep = 0 ; tstep < SizeFactor ; tstep++ ) {
  99.         if ( ( sstep + tstep ) % 2 ) {
  100.         loc_to_square( sstep, tstep, vert ) ;
  101.         lib_output_polygon( 4, vert ) ;
  102.         }
  103.     }
  104.     }
  105.  
  106.     SET_COORD3( obj_color, 0.5, 0.5, 0.5 ) ;
  107.     lib_output_color( obj_color, 0.5, 0.5, 3.0, 0.0, 0.0 ) ;
  108.     for ( sstep = 0 ; sstep < SizeFactor ; sstep++ ) {
  109.     for ( tstep = 0 ; tstep < SizeFactor ; tstep++ ) {
  110.         if ( !(( sstep + tstep ) % 2) ) {
  111.         loc_to_square( sstep, tstep, vert ) ;
  112.         lib_output_polygon( 4, vert ) ;
  113.         }
  114.     }
  115.     }
  116. }
  117.  
  118. loc_to_square( sstep, tstep, vert )
  119. int    sstep ;
  120. int    tstep ;
  121. COORD3    vert[4] ;
  122. {
  123. int    i ;
  124.  
  125.     for ( i = 0 ; i < 4 ; i++ ) {
  126.     /* vertex 0 & 3 are x.low, 1 & 2 are x.high */
  127.     vert[i][X] = 4.0 * ( 2.0 * (double)(sstep + (i%3 ? 1 : 0) ) /
  128.         (double)SizeFactor - 1.0 ) ;
  129.  
  130.     /* vertex 0 & 1 are y.low, 2 & 3 are y.high */
  131.     vert[i][Y] = 4.0 * ( 2.0 * (double)(tstep + (i/2 ? 1 : 0) ) /
  132.         (double)SizeFactor - 1.0 ) ;
  133.  
  134.     vert[i][Z] = 0.0 ;
  135.     }
  136. }
  137.  
  138. static    int    Patches[32][4][4] = {
  139. /* rim */
  140. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  141. 3, 16, 17, 18, 7, 19, 20, 21, 11, 22, 23, 24, 15, 25, 26, 27,
  142. 18, 28, 29, 30, 21, 31, 32, 33, 24, 34, 35, 36, 27, 37, 38, 39,
  143. 30, 40, 41, 0, 33, 42, 43, 4, 36, 44, 45, 8, 39, 46, 47, 12,
  144. /* body */
  145. 12, 13, 14, 15, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  146. 15, 25, 26, 27, 51, 60, 61, 62, 55, 63, 64, 65, 59, 66, 67, 68,
  147. 27, 37, 38, 39, 62, 69, 70, 71, 65, 72, 73, 74, 68, 75, 76, 77,
  148. 39, 46, 47, 12, 71, 78, 79, 48, 74, 80, 81, 52, 77, 82, 83, 56,
  149. 56, 57, 58, 59, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  150. 59, 66, 67, 68, 87, 96, 97, 98, 91, 99, 100, 101, 95, 102, 103, 104,
  151. 68, 75, 76, 77, 98, 105, 106, 107, 101, 108, 109, 110, 104, 111, 112, 113,
  152. 77, 82, 83, 56, 107, 114, 115, 84, 110, 116, 117, 88, 113, 118, 119, 92,
  153. /* handle */
  154. 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
  155. 123, 136, 137, 120, 127, 138, 139, 124, 131, 140, 141, 128, 135, 142, 143, 132,
  156. 132, 133, 134, 135, 144, 145, 146, 147, 148, 149, 150, 151, 68, 152, 153, 154,
  157. 135, 142, 143, 132, 147, 155, 156, 144, 151, 157, 158, 148, 154, 159, 160, 68,
  158. /* spout */
  159. 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
  160. 164, 177, 178, 161, 168, 179, 180, 165, 172, 181, 182, 169, 176, 183, 184, 173,
  161. 173, 174, 175, 176, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
  162. 176, 183, 184, 173, 188, 197, 198, 185, 192, 199, 200, 189, 196, 201, 202, 193,
  163. /* lid */
  164. 203, 203, 203, 203, 204, 205, 206, 207, 208, 208, 208, 208, 209, 210, 211, 212,
  165. 203, 203, 203, 203, 207, 213, 214, 215, 208, 208, 208, 208, 212, 216, 217, 218,
  166. 203, 203, 203, 203, 215, 219, 220, 221, 208, 208, 208, 208, 218, 222, 223, 224,
  167. 203, 203, 203, 203, 221, 225, 226, 204, 208, 208, 208, 208, 224, 227, 228, 209,
  168. 209, 210, 211, 212, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
  169. 212, 216, 217, 218, 232, 241, 242, 243, 236, 244, 245, 246, 240, 247, 248, 249,
  170. 218, 222, 223, 224, 243, 250, 251, 252, 246, 253, 254, 255, 249, 256, 257, 258,
  171. 224, 227, 228, 209, 252, 259, 260, 229, 255, 261, 262, 233, 258, 263, 264, 237,
  172. /* bottom */
  173. 265, 265, 265, 265, 266, 267, 268, 269, 270, 271, 272, 273, 92, 119, 118, 113,
  174. 265, 265, 265, 265, 269, 274, 275, 276, 273, 277, 278, 279, 113, 112, 111, 104,
  175. 265, 265, 265, 265, 276, 280, 281, 282, 279, 283, 284, 285, 104, 103, 102, 95,
  176. 265, 265, 265, 265, 282, 286, 287, 266, 285, 288, 289, 270, 95, 94, 93, 92
  177. } ;
  178.  
  179. static    COORD3    Verts[290] = {
  180.  1.4, 0, 2.4,
  181.  1.4, -0.784, 2.4,
  182.  0.784, -1.4, 2.4,
  183.  0, -1.4, 2.4,
  184.  1.3375, 0, 2.53125,
  185.  1.3375, -0.749, 2.53125,
  186.  0.749, -1.3375, 2.53125,
  187.  0, -1.3375, 2.53125,
  188.  1.4375, 0, 2.53125,
  189.  1.4375, -0.805, 2.53125,
  190.  0.805, -1.4375, 2.53125,
  191.  0, -1.4375, 2.53125,
  192.  1.5, 0, 2.4,
  193.  1.5, -0.84, 2.4,
  194.  0.84, -1.5, 2.4,
  195.  0, -1.5, 2.4,
  196.  -0.784, -1.4, 2.4,
  197.  -1.4, -0.784, 2.4,
  198.  -1.4, 0, 2.4,
  199.  -0.749, -1.3375, 2.53125,
  200.  -1.3375, -0.749, 2.53125,
  201.  -1.3375, 0, 2.53125,
  202.  -0.805, -1.4375, 2.53125,
  203.  -1.4375, -0.805, 2.53125,
  204.  -1.4375, 0, 2.53125,
  205.  -0.84, -1.5, 2.4,
  206.  -1.5, -0.84, 2.4,
  207.  -1.5, 0, 2.4,
  208.  -1.4, 0.784, 2.4,
  209.  -0.784, 1.4, 2.4,
  210.  0, 1.4, 2.4,
  211.  -1.3375, 0.749, 2.53125,
  212.  -0.749, 1.3375, 2.53125,
  213.  0, 1.3375, 2.53125,
  214.  -1.4375, 0.805, 2.53125,
  215.  -0.805, 1.4375, 2.53125,
  216.  0, 1.4375, 2.53125,
  217.  -1.5, 0.84, 2.4,
  218.  -0.84, 1.5, 2.4,
  219.  0, 1.5, 2.4,
  220.  0.784, 1.4, 2.4,
  221.  1.4, 0.784, 2.4,
  222.  0.749, 1.3375, 2.53125,
  223.  1.3375, 0.749, 2.53125,
  224.  0.805, 1.4375, 2.53125,
  225.  1.4375, 0.805, 2.53125,
  226.  0.84, 1.5, 2.4,
  227.  1.5, 0.84, 2.4,
  228.  1.75, 0, 1.875,
  229.  1.75, -0.98, 1.875,
  230.  0.98, -1.75, 1.875,
  231.  0, -1.75, 1.875,
  232.  2, 0, 1.35,
  233.  2, -1.12, 1.35,
  234.  1.12, -2, 1.35,
  235.  0, -2, 1.35,
  236.  2, 0, 0.9,
  237.  2, -1.12, 0.9,
  238.  1.12, -2, 0.9,
  239.  0, -2, 0.9,
  240.  -0.98, -1.75, 1.875,
  241.  -1.75, -0.98, 1.875,
  242.  -1.75, 0, 1.875,
  243.  -1.12, -2, 1.35,
  244.  -2, -1.12, 1.35,
  245.  -2, 0, 1.35,
  246.  -1.12, -2, 0.9,
  247.  -2, -1.12, 0.9,
  248.  -2, 0, 0.9,
  249.  -1.75, 0.98, 1.875,
  250.  -0.98, 1.75, 1.875,
  251.  0, 1.75, 1.875,
  252.  -2, 1.12, 1.35,
  253.  -1.12, 2, 1.35,
  254.  0, 2, 1.35,
  255.  -2, 1.12, 0.9,
  256.  -1.12, 2, 0.9,
  257.  0, 2, 0.9,
  258.  0.98, 1.75, 1.875,
  259.  1.75, 0.98, 1.875,
  260.  1.12, 2, 1.35,
  261.  2, 1.12, 1.35,
  262.  1.12, 2, 0.9,
  263.  2, 1.12, 0.9,
  264.  2, 0, 0.45,
  265.  2, -1.12, 0.45,
  266.  1.12, -2, 0.45,
  267.  0, -2, 0.45,
  268.  1.5, 0, 0.225,
  269.  1.5, -0.84, 0.225,
  270.  0.84, -1.5, 0.225,
  271.  0, -1.5, 0.225,
  272.  1.5, 0, 0.15,
  273.  1.5, -0.84, 0.15,
  274.  0.84, -1.5, 0.15,
  275.  0, -1.5, 0.15,
  276.  -1.12, -2, 0.45,
  277.  -2, -1.12, 0.45,
  278.  -2, 0, 0.45,
  279.  -0.84, -1.5, 0.225,
  280.  -1.5, -0.84, 0.225,
  281.  -1.5, 0, 0.225,
  282.  -0.84, -1.5, 0.15,
  283.  -1.5, -0.84, 0.15,
  284.  -1.5, 0, 0.15,
  285.  -2, 1.12, 0.45,
  286.  -1.12, 2, 0.45,
  287.  0, 2, 0.45,
  288.  -1.5, 0.84, 0.225,
  289.  -0.84, 1.5, 0.225,
  290.  0, 1.5, 0.225,
  291.  -1.5, 0.84, 0.15,
  292.  -0.84, 1.5, 0.15,
  293.  0, 1.5, 0.15,
  294.  1.12, 2, 0.45,
  295.  2, 1.12, 0.45,
  296.  0.84, 1.5, 0.225,
  297.  1.5, 0.84, 0.225,
  298.  0.84, 1.5, 0.15,
  299.  1.5, 0.84, 0.15,
  300.  -1.6, 0, 2.025,
  301.  -1.6, -0.3, 2.025,
  302.  -1.5, -0.3, 2.25,
  303.  -1.5, 0, 2.25,
  304.  -2.3, 0, 2.025,
  305.  -2.3, -0.3, 2.025,
  306.  -2.5, -0.3, 2.25,
  307.  -2.5, 0, 2.25,
  308.  -2.7, 0, 2.025,
  309.  -2.7, -0.3, 2.025,
  310.  -3, -0.3, 2.25,
  311.  -3, 0, 2.25,
  312.  -2.7, 0, 1.8,
  313.  -2.7, -0.3, 1.8,
  314.  -3, -0.3, 1.8,
  315.  -3, 0, 1.8,
  316.  -1.5, 0.3, 2.25,
  317.  -1.6, 0.3, 2.025,
  318.  -2.5, 0.3, 2.25,
  319.  -2.3, 0.3, 2.025,
  320.  -3, 0.3, 2.25,
  321.  -2.7, 0.3, 2.025,
  322.  -3, 0.3, 1.8,
  323.  -2.7, 0.3, 1.8,
  324.  -2.7, 0, 1.575,
  325.  -2.7, -0.3, 1.575,
  326.  -3, -0.3, 1.35,
  327.  -3, 0, 1.35,
  328.  -2.5, 0, 1.125,
  329.  -2.5, -0.3, 1.125,
  330.  -2.65, -0.3, 0.9375,
  331.  -2.65, 0, 0.9375,
  332.  -2, -0.3, 0.9,
  333.  -1.9, -0.3, 0.6,
  334.  -1.9, 0, 0.6,
  335.  -3, 0.3, 1.35,
  336.  -2.7, 0.3, 1.575,
  337.  -2.65, 0.3, 0.9375,
  338.  -2.5, 0.3, 1.125,
  339.  -1.9, 0.3, 0.6,
  340.  -2, 0.3, 0.9,
  341.  1.7, 0, 1.425,
  342.  1.7, -0.66, 1.425,
  343.  1.7, -0.66, 0.6,
  344.  1.7, 0, 0.6,
  345.  2.6, 0, 1.425,
  346.  2.6, -0.66, 1.425,
  347.  3.1, -0.66, 0.825,
  348.  3.1, 0, 0.825,
  349.  2.3, 0, 2.1,
  350.  2.3, -0.25, 2.1,
  351.  2.4, -0.25, 2.025,
  352.  2.4, 0, 2.025,
  353.  2.7, 0, 2.4,
  354.  2.7, -0.25, 2.4,
  355.  3.3, -0.25, 2.4,
  356.  3.3, 0, 2.4,
  357.  1.7, 0.66, 0.6,
  358.  1.7, 0.66, 1.425,
  359.  3.1, 0.66, 0.825,
  360.  2.6, 0.66, 1.425,
  361.  2.4, 0.25, 2.025,
  362.  2.3, 0.25, 2.1,
  363.  3.3, 0.25, 2.4,
  364.  2.7, 0.25, 2.4,
  365.  2.8, 0, 2.475,
  366.  2.8, -0.25, 2.475,
  367.  3.525, -0.25, 2.49375,
  368.  3.525, 0, 2.49375,
  369.  2.9, 0, 2.475,
  370.  2.9, -0.15, 2.475,
  371.  3.45, -0.15, 2.5125,
  372.  3.45, 0, 2.5125,
  373.  2.8, 0, 2.4,
  374.  2.8, -0.15, 2.4,
  375.  3.2, -0.15, 2.4,
  376.  3.2, 0, 2.4,
  377.  3.525, 0.25, 2.49375,
  378.  2.8, 0.25, 2.475,
  379.  3.45, 0.15, 2.5125,
  380.  2.9, 0.15, 2.475,
  381.  3.2, 0.15, 2.4,
  382.  2.8, 0.15, 2.4,
  383.  0, 0, 3.15,
  384.  0.8, 0, 3.15,
  385.  0.8, -0.45, 3.15,
  386.  0.45, -0.8, 3.15,
  387.  0, -0.8, 3.15,
  388.  0, 0, 2.85,
  389.  0.2, 0, 2.7,
  390.  0.2, -0.112, 2.7,
  391.  0.112, -0.2, 2.7,
  392.  0, -0.2, 2.7,
  393.  -0.45, -0.8, 3.15,
  394.  -0.8, -0.45, 3.15,
  395.  -0.8, 0, 3.15,
  396.  -0.112, -0.2, 2.7,
  397.  -0.2, -0.112, 2.7,
  398.  -0.2, 0, 2.7,
  399.  -0.8, 0.45, 3.15,
  400.  -0.45, 0.8, 3.15,
  401.  0, 0.8, 3.15,
  402.  -0.2, 0.112, 2.7,
  403.  -0.112, 0.2, 2.7,
  404.  0, 0.2, 2.7,
  405.  0.45, 0.8, 3.15,
  406.  0.8, 0.45, 3.15,
  407.  0.112, 0.2, 2.7,
  408.  0.2, 0.112, 2.7,
  409.  0.4, 0, 2.55,
  410.  0.4, -0.224, 2.55,
  411.  0.224, -0.4, 2.55,
  412.  0, -0.4, 2.55,
  413.  1.3, 0, 2.55,
  414.  1.3, -0.728, 2.55,
  415.  0.728, -1.3, 2.55,
  416.  0, -1.3, 2.55,
  417.  1.3, 0, 2.4,
  418.  1.3, -0.728, 2.4,
  419.  0.728, -1.3, 2.4,
  420.  0, -1.3, 2.4,
  421.  -0.224, -0.4, 2.55,
  422.  -0.4, -0.224, 2.55,
  423.  -0.4, 0, 2.55,
  424.  -0.728, -1.3, 2.55,
  425.  -1.3, -0.728, 2.55,
  426.  -1.3, 0, 2.55,
  427.  -0.728, -1.3, 2.4,
  428.  -1.3, -0.728, 2.4,
  429.  -1.3, 0, 2.4,
  430.  -0.4, 0.224, 2.55,
  431.  -0.224, 0.4, 2.55,
  432.  0, 0.4, 2.55,
  433.  -1.3, 0.728, 2.55,
  434.  -0.728, 1.3, 2.55,
  435.  0, 1.3, 2.55,
  436.  -1.3, 0.728, 2.4,
  437.  -0.728, 1.3, 2.4,
  438.  0, 1.3, 2.4,
  439.  0.224, 0.4, 2.55,
  440.  0.4, 0.224, 2.55,
  441.  0.728, 1.3, 2.55,
  442.  1.3, 0.728, 2.55,
  443.  0.728, 1.3, 2.4,
  444.  1.3, 0.728, 2.4,
  445.  0, 0, 0,
  446.  1.425, 0, 0,
  447.  1.425, 0.798, 0,
  448.  0.798, 1.425, 0,
  449.  0, 1.425, 0,
  450.  1.5, 0, 0.075,
  451.  1.5, 0.84, 0.075,
  452.  0.84, 1.5, 0.075,
  453.  0, 1.5, 0.075,
  454.  -0.798, 1.425, 0,
  455.  -1.425, 0.798, 0,
  456.  -1.425, 0, 0,
  457.  -0.84, 1.5, 0.075,
  458.  -1.5, 0.84, 0.075,
  459.  -1.5, 0, 0.075,
  460.  -1.425, -0.798, 0,
  461.  -0.798, -1.425, 0,
  462.  0, -1.425, 0,
  463.  -1.5, -0.84, 0.075,
  464.  -0.84, -1.5, 0.075,
  465.  0, -1.5, 0.075,
  466.  0.798, -1.425, 0,
  467.  1.425, -0.798, 0,
  468.  0.84, -1.5, 0.075,
  469.  1.5, -0.84, 0.075
  470. } ;
  471.  
  472. /* Compute points on each spline surface of teapot by brute force.
  473.  * Forward differencing would be faster, but this is compact & simple.
  474.  */
  475. output_teapot()
  476. {
  477. /* bezier form */
  478. static MATRIX ms = { -1.0,  3.0, -3.0,  1.0,
  479.               3.0, -6.0,  3.0,  0.0,
  480.              -3.0,  3.0,  0.0,  0.0,
  481.               1.0,  0.0,  0.0,  0.0 } ;
  482. int    surf, i, r, c, sstep, tstep, num_tri, num_vert, num_tri_vert ;
  483. double    s[3], t[3] ;
  484. COORD3    vert[4], norm[4] ;
  485. COORD3    obj_color ;
  486. MATRIX    mst, g, mgm[3], tmtx ;
  487.  
  488.     SET_COORD3( obj_color, 1.0, 0.5, 0.1 ) ;
  489.     lib_output_color( obj_color, 0.75, 0.25, 20.0, 0.0, 0.0 ) ;
  490.  
  491.     lib_transpose_matrix( mst, ms ) ;
  492.  
  493.     for ( surf = 0 ; surf < NUM_PATCHES ; surf++ ) {
  494.  
  495.     /* get M * G * M matrix for x,y,z */
  496.     for ( i = 0 ; i < 3 ; i++ ) {
  497.         /* get control patches */
  498.         for ( r = 0 ; r < 4 ; r++ ) {
  499.         for ( c = 0 ; c < 4 ; c++ ) {
  500.             g[r][c] = Verts[Patches[surf][r][c]][i] ;
  501.         }
  502.         }
  503.  
  504.         lib_matrix_multiply( tmtx, ms, g ) ;
  505.         lib_matrix_multiply( mgm[i], tmtx, mst ) ;
  506.     }
  507.  
  508.     /* step along, get points, and output */
  509.     for ( sstep = 0 ; sstep < SizeFactor ; sstep++ ) {
  510.         for ( tstep = 0 ; tstep < SizeFactor ; tstep++ ) {
  511.         for ( num_tri = 0 ; num_tri < 2 ; num_tri++ ) {
  512.             for ( num_vert = 0 ; num_vert < 3 ; num_vert++ ) {
  513.             num_tri_vert = ( num_vert + num_tri * 2 ) % 4 ;
  514.             /* trickiness: add 1 to sstep if 1 or 2 */
  515.             s[num_vert] = (double)(sstep + (num_tri_vert/2 ? 1:0) )
  516.                     / (double)SizeFactor ;
  517.             /* trickiness: add 1 to tstep if 2 or 3 */
  518.             t[num_vert] = (double)(tstep + (num_tri_vert%3 ? 1:0) )
  519.                     / (double)SizeFactor ;
  520.             }
  521.             points_from_basis( 3, s, t, mgm,
  522.                 vert, norm ) ;
  523.             /* don't output degenerate polygons */
  524.             if ( check_for_cusp( 3, vert, norm ) ) {
  525.             lib_output_polypatch( 3, vert, norm ) ;
  526.             }
  527.         }
  528.         }
  529.     }
  530.     }
  531. }
  532.  
  533. points_from_basis( tot_vert, s, t, mgm, vert, norm )
  534. int    tot_vert ;
  535. double    s[] ;
  536. double    t[] ;
  537. MATRIX    mgm[3] ;
  538. COORD3    vert[] ;
  539. COORD3    norm[] ;
  540. {
  541. int    i, num_vert, p ;
  542. double    sval, tval, dsval, dtval, sxyz, txyz ;
  543. COORD3    sdir, tdir ;
  544. COORD4    sp, tp, dsp, dtp, tcoord ;
  545.  
  546.     for ( num_vert = 0 ; num_vert < tot_vert ; num_vert++ ) {
  547.  
  548.     sxyz = s[num_vert] ;
  549.     txyz = t[num_vert] ;
  550.  
  551.     /* get power vectors and their derivatives */
  552.     for ( p = 4, sval = tval = 1.0 ; p-- ; ) {
  553.         sp[p] = sval ;
  554.         tp[p] = tval ;
  555.         sval *= sxyz ;
  556.         tval *= txyz ;
  557.  
  558.         if ( p == 3 ) {
  559.         dsp[p] = dtp[p] = 0.0 ;
  560.         dsval = dtval = 1.0 ;
  561.         } else {
  562.         dsp[p] = dsval * (double)(3-p) ;
  563.         dtp[p] = dtval * (double)(3-p) ;
  564.         dsval *= sxyz ;
  565.         dtval *= txyz ;
  566.         }
  567.     }
  568.  
  569.     /* do for x,y,z */
  570.     for ( i = 0 ; i < 3 ; i++ ) {
  571.         /* multiply power vectors times matrix to get value */
  572.         lib_transform_coord( tcoord, sp, mgm[i] ) ;
  573.         vert[num_vert][i] = DOT4( tcoord, tp ) ;
  574.  
  575.         /* get s and t tangent vectors */
  576.         lib_transform_coord( tcoord, dsp, mgm[i] ) ;
  577.         sdir[i] = DOT4( tcoord, tp ) ;
  578.  
  579.         lib_transform_coord( tcoord, sp, mgm[i] ) ;
  580.         tdir[i] = DOT4( tcoord, dtp ) ;
  581.     }
  582.  
  583.     /* find normal */
  584.     CROSS( norm[num_vert], tdir, sdir ) ;
  585.     (void)lib_normalize_vector( norm[num_vert] ) ;
  586.     }
  587. }
  588.  
  589. /* at the center of the lid's handle and at bottom are cusp points -
  590.  * check if normal is (0 0 0), if so, check that polygon is not degenerate.
  591.  * If degenerate, return FALSE, else set normal.
  592.  */
  593. int    check_for_cusp( tot_vert, vert, norm )
  594. int    tot_vert ;
  595. COORD3    vert[] ;
  596. COORD3    norm[] ;
  597. {
  598. int    count, i, nv ;
  599.  
  600.     for ( count = 0, i = tot_vert ; i-- ; ) {
  601.     /* check if vertex is at cusp */
  602.     if ( IS_VAL_ALMOST_ZERO( vert[i][X], 0.0001 ) &&
  603.          IS_VAL_ALMOST_ZERO( vert[i][Y], 0.0001 ) ) {
  604.         count++ ;
  605.         nv = i ;
  606.     }
  607.     }
  608.  
  609.     if ( count > 1 ) {
  610.     /* degenerate */
  611.     return( FALSE ) ;
  612.     }
  613.     if ( count == 1 ) {
  614.     /* check if point is somewhere above the middle of the teapot */
  615.     if ( vert[nv][Z] > 1.5 ) {
  616.         /* cusp at lid */
  617.         SET_COORD3( norm[nv], 0.0, 0.0, 1.0 ) ;
  618.     } else {
  619.         /* cusp at bottom */
  620.         SET_COORD3( norm[nv], 0.0, 0.0, -1.0 ) ;
  621.     }
  622.     }
  623.     return( TRUE ) ;
  624. }
  625.