home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2214 / nff.y < prev    next >
Encoding:
Lex Description  |  1990-12-28  |  9.1 KB  |  387 lines

  1. %{
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. typedef double Flt ;
  6. typedef Flt Vec[3] ;
  7. typedef Vec Point ;
  8. typedef Vec Color ;
  9.  
  10. char *Progname;
  11. int yylinecount = 1;
  12. extern char yytext[] ;
  13. extern FILE * yyin ;
  14. Vec * pl, * plist ;
  15. %}
  16.  
  17. %token VIEWPOINT FROM AT UP ANGLE HITHER RESOLUTION LIGHT
  18. %token BACKGROUND SURFACE CONE SPHERE POLYGON PATCH NUM TOKEN
  19.  
  20. %union {
  21.     Vec    vec ;
  22.     Vec *    vecl ;
  23.     double    flt ;
  24. } ;
  25.  
  26. %type <vec>    point primcolor TOKEN
  27. %type <obj>    cone sphere polygon ppatch
  28. %type <flt>    num 
  29.  
  30. %%
  31.  
  32. scene:
  33.     camera elementlist ;
  34.  
  35. elementlist:
  36.     elementlist element
  37.     |  ;
  38.  
  39. element:
  40.     light
  41.     | background
  42.     | surface
  43.     | object ;
  44.  
  45. object:      cone
  46.     | sphere
  47.     | polygon
  48.     | ppatch ;
  49.  
  50. camera:
  51.     VIEWPOINT            /* $1      */
  52.     FROM point            /* $2-$3   */
  53.     AT point            /* $4-$5   */
  54.     UP point            /* $6-$7   */
  55.     ANGLE num             /* $8-$9   */
  56.     HITHER num            /* $10-$11 */
  57.     RESOLUTION num num        /* $12-$14 */
  58.     {
  59.         printf("from %g %g %g\n", $3[0], $3[1], $3[2]);
  60.         printf("at %g %g %g\n", $5[0], $5[1], $5[2]);
  61.         printf("up %g %g %g\n", $7[0], $7[1], $7[2]);
  62.         printf("angle %g\n", $11);
  63.         printf("resolution %d %d\n", (int) $13, (int) $14);
  64.     } ;
  65.  
  66. light:
  67.     LIGHT point 
  68.     {
  69.         printf("light %g %g %g\n", $2[0], $2[1], $2[2]);
  70.     } ;
  71.  
  72. background:
  73.     BACKGROUND primcolor
  74.     {
  75.         printf("background %g %g %g n\n", $2[0], $2[1], $2[2]);
  76.     } ;
  77.  
  78. surface:
  79.     SURFACE primcolor num num num num num 
  80.     {
  81.         printf("surface 1 1 1 %g  1 1 1 %g ", $4, $6);
  82.         printf("%g %g %g   %g %g %g  1 1 1  %g %g\n",
  83.             $2[0] * 0.3, $2[1] * 0.3, $2[2] * 0.3,
  84.             $2[0] * 0.7, $2[1] * 0.7, $2[2] * 0.7,
  85.             $5, $7);
  86.     } ;
  87.  
  88. cone:
  89.     CONE point num point num 
  90.     {
  91.         printf("cone %g %g %g  %g  %g %g %g  %g\n",
  92.             $2[0], $2[1], $2[2], $3, $4[0], $4[1], $4[2], $5);
  93.     } ;
  94.  
  95. sphere:
  96.     SPHERE point num 
  97.     {
  98.         printf("sphere %g %g %g  %g\n",
  99.             $2[0], $2[1], $2[2], $3);
  100.     } ;
  101.  
  102. polygon:
  103.     POLYGON num 
  104.     {
  105.         printf("polygon %d\n", (int) $2);
  106.     } 
  107.     pointlist 
  108.     {
  109.  
  110.     } ;
  111.  
  112. ppatch:
  113.     PATCH num
  114.         {
  115.         fprintf(stderr, "%s: sorry, rt doesn't support polygon patches.\n",
  116.             Progname);
  117.     } ;
  118.  
  119. primcolor:
  120.     num num num 
  121.     {
  122.         $$[0] = $1 ;
  123.         $$[1] = $2 ;
  124.         $$[2] = $3 ;
  125.     } 
  126.     | TOKEN
  127.     {
  128.         char buf[80] ;
  129.  
  130.         if (LookupColorByName(yytext, $$) == 0) {
  131.             sprintf(buf, "cannot find color \"%s\"\n",
  132.                 yytext) ;
  133.             yyerror(buf) ;
  134.         }
  135.     } ;
  136.  
  137.  
  138. point:
  139.     num num num
  140.     {
  141.         $$[0] = $1 ;
  142.         $$[1] = $2 ;
  143.         $$[2] = $3 ;
  144.     } ;
  145.  
  146. pointlist:
  147.     pointlist point
  148.     {
  149.         printf("%g %g %g\n", $2[0], $2[1], $2[2]);
  150.     }
  151.     | ;
  152.     
  153.  
  154. num:
  155.     NUM
  156.         {
  157.             $$ = atof(yytext) ;
  158.         } ;
  159.  
  160. %%
  161.  
  162. yyerror(str)
  163.  char * str ;
  164. {
  165.     fprintf(stderr, "%s: error at line %d\n", 
  166.         Progname, yylinecount) ;
  167.     fprintf(stderr, "%s: %s\n", Progname, str) ;
  168.     exit(-1) ;
  169. }
  170.  
  171.  
  172. main(argc, argv)
  173.  int argc;
  174.  char *argv[] ;
  175. {
  176.     Progname = argv[0];
  177.  
  178.     yyin = stdin ;
  179.     if (yyparse() == 1) {
  180.         fprintf(stderr, "%s: invalid input specification\n", Progname);
  181.         exit(-1) ;
  182.     }
  183. }
  184.  
  185.  
  186. #define        NCOLORS        (142)
  187.  
  188. typedef struct t_color_entry {
  189.     char *    ce_name ;
  190.     Vec     ce_color ;
  191. } ColorEntry ;
  192.  
  193. #define LESS_THAN -1
  194. #define GREATER_THAN 1
  195. #define EQUAL_TO 0
  196.  
  197. /*
  198.  * Note: These colors must be in sorted order, because we binary search
  199.  * for them.
  200.  *
  201.  * They were swiped from the X-11 distribution.  Sorry....
  202.  */
  203.  
  204. ColorEntry Colors[] = {
  205.     "Aquamarine", {.439216, .858824, .576471},
  206.     "Black", {0, 0, 0},
  207.     "Blue", {0, 0, 1},
  208.     "BlueViolet", {.623529, .372549, .623529},
  209.     "Brown", {.647059, .164706, .164706},
  210.     "CadetBlue", {.372549, .623529, .623529},
  211.     "Coral", {1, .498039, 0},
  212.     "CornflowerBlue", {.258824, .258824, .435294},
  213.     "Cyan", {0, 1, 1},
  214.     "DarkGreen", {.184314, .309804, .184314},
  215.     "DarkOliveGreen", {.309804, .309804, .184314},
  216.     "DarkOrchid", {.6, .196078, .8},
  217.     "DarkSlateBlue", {.419608, .137255, .556863},
  218.     "DarkSlateGray", {.184314, .309804, .309804},
  219.     "DarkSlateGrey", {.184314, .309804, .309804},
  220.     "DarkTurquoise", {.439216, .576471, .858824},
  221.     "DimGray", {.329412, .329412, .329412},
  222.     "DimGrey", {.329412, .329412, .329412},
  223.     "Firebrick", {.556863, .137255, .137255},
  224.     "ForestGreen", {.137255, .556863, .137255},
  225.     "Gold", {.8, .498039, .196078},
  226.     "Goldenrod", {.858824, .858824, .439216},
  227.     "Gray", {.752941, .752941, .752941},
  228.     "Green", {0, 1, 0},
  229.     "GreenYellow", {.576471, .858824, .439216},
  230.     "Grey", {.752941, .752941, .752941},
  231.     "IndianRed", {.309804, .184314, .184314},
  232.     "Khaki", {.623529, .623529, .372549},
  233.     "LightBlue", {.74902, .847059, .847059},
  234.     "LightGray", {.658824, .658824, .658824},
  235.     "LightGrey", {.658824, .658824, .658824},
  236.     "LightSteelBlue", {.560784, .560784, .737255},
  237.     "LimeGreen", {.196078, .8, .196078},
  238.     "Magenta", {1, 0, 1},
  239.     "Maroon", {.556863, .137255, .419608},
  240.     "MediumAquamarine", {.196078, .8, .6},
  241.     "MediumBlue", {.196078, .196078, .8},
  242.     "MediumForestGreen", {.419608, .556863, .137255},
  243.     "MediumGoldenrod", {.917647, .917647, .678431},
  244.     "MediumOrchid", {.576471, .439216, .858824},
  245.     "MediumSeaGreen", {.258824, .435294, .258824},
  246.     "MediumSlateBlue", {.498039, 0, 1},
  247.     "MediumSpringGreen", {.498039, 1, 0},
  248.     "MediumTurquoise", {.439216, .858824, .858824},
  249.     "MediumVioletRed", {.858824, .439216, .576471},
  250.     "MidnightBlue", {.184314, .184314, .309804},
  251.     "Navy", {.137255, .137255, .556863},
  252.     "NavyBlue", {.137255, .137255, .556863},
  253.     "Orange", {.8, .196078, .196078},
  254.     "OrangeRed", {1, 0, .498039},
  255.     "Orchid", {.858824, .439216, .858824},
  256.     "PaleGreen", {.560784, .737255, .560784},
  257.     "Pink", {.737255, .560784, .560784},
  258.     "Plum", {.917647, .678431, .917647},
  259.     "Red", {1, 0, 0},
  260.     "Salmon", {.435294, .258824, .258824},
  261.     "SeaGreen", {.137255, .556863, .419608},
  262.     "Sienna", {.556863, .419608, .137255},
  263.     "SkyBlue", {.196078, .6, .8},
  264.     "SlateBlue", {0, .498039, 1},
  265.     "SpringGreen", {0, 1, .498039},
  266.     "SteelBlue", {.137255, .419608, .556863},
  267.     "Tan", {.858824, .576471, .439216},
  268.     "Thistle", {.847059, .74902, .847059},
  269.     "Turquoise", {.678431, .917647, .917647},
  270.     "Violet", {.309804, .184314, .309804},
  271.     "VioletRed", {.8, .196078, .6},
  272.     "Wheat", {.847059, .847059, .74902},
  273.     "White", {.988235, .988235, .988235},
  274.     "Yellow", {1, 1, 0},
  275.     "YellowGreen", {.6, .8, .196078},
  276.     "aquamarine", {.439216, .858824, .576471},
  277.     "black", {0, 0, 0},
  278.     "blue", {0, 0, 1},
  279.     "blue_violet", {.623529, .372549, .623529},
  280.     "brown", {.647059, .164706, .164706},
  281.     "cadet_blue", {.372549, .623529, .623529},
  282.     "coral", {1, .498039, 0},
  283.     "cornflower_blue", {.258824, .258824, .435294},
  284.     "cyan", {0, 1, 1},
  285.     "dark_green", {.184314, .309804, .184314},
  286.     "dark_olive_green", {.309804, .309804, .184314},
  287.     "dark_orchid", {.6, .196078, .8},
  288.     "dark_slate_blue", {.419608, .137255, .556863},
  289.     "dark_slate_gray", {.184314, .309804, .309804},
  290.     "dark_slate_grey", {.184314, .309804, .309804},
  291.     "dark_turquoise", {.439216, .576471, .858824},
  292.     "dim_gray", {.329412, .329412, .329412},
  293.     "dim_grey", {.329412, .329412, .329412},
  294.     "firebrick", {.556863, .137255, .137255},
  295.     "forest_green", {.137255, .556863, .137255},
  296.     "gold", {.8, .498039, .196078},
  297.     "goldenrod", {.858824, .858824, .439216},
  298.     "gray", {.752941, .752941, .752941},
  299.     "green", {0, 1, 0},
  300.     "green_yellow", {.576471, .858824, .439216},
  301.     "grey", {.752941, .752941, .752941},
  302.     "indian_red", {.309804, .184314, .184314},
  303.     "khaki", {.623529, .623529, .372549},
  304.     "light_blue", {.74902, .847059, .847059},
  305.     "light_gray", {.658824, .658824, .658824},
  306.     "light_grey", {.658824, .658824, .658824},
  307.     "light_steel_blue", {.560784, .560784, .737255},
  308.     "lime_green", {.196078, .8, .196078},
  309.     "magenta", {1, 0, 1},
  310.     "maroon", {.556863, .137255, .419608},
  311.     "medium_aquamarine", {.196078, .8, .6},
  312.     "medium_blue", {.196078, .196078, .8},
  313.     "medium_forest_green", {.419608, .556863, .137255},
  314.     "medium_goldenrod", {.917647, .917647, .678431},
  315.     "medium_orchid", {.576471, .439216, .858824},
  316.     "medium_sea_green", {.258824, .435294, .258824},
  317.     "medium_slate_blue", {.498039, 0, 1},
  318.     "medium_spring_green", {.498039, 1, 0},
  319.     "medium_turquoise", {.439216, .858824, .858824},
  320.     "medium_violet_red", {.858824, .439216, .576471},
  321.     "midnight_blue", {.184314, .184314, .309804},
  322.     "navy", {.137255, .137255, .556863},
  323.     "navy_blue", {.137255, .137255, .556863},
  324.     "orange", {.8, .196078, .196078},
  325.     "orange_red", {1, 0, .498039},
  326.     "orchid", {.858824, .439216, .858824},
  327.     "pale_green", {.560784, .737255, .560784},
  328.     "pink", {.737255, .560784, .560784},
  329.     "plum", {.917647, .678431, .917647},
  330.     "red", {1, 0, 0},
  331.     "salmon", {.435294, .258824, .258824},
  332.     "sea_green", {.137255, .556863, .419608},
  333.     "sienna", {.556863, .419608, .137255},
  334.     "sky_blue", {.196078, .6, .8},
  335.     "slate_blue", {0, .498039, 1},
  336.     "spring_green", {0, 1, .498039},
  337.     "steel_blue", {.137255, .419608, .556863},
  338.     "tan", {.858824, .576471, .439216},
  339.     "thistle", {.847059, .74902, .847059},
  340.     "turquoise", {.678431, .917647, .917647},
  341.     "violet", {.309804, .184314, .309804},
  342.     "violet_red", {.8, .196078, .6},
  343.     "wheat", {.847059, .847059, .74902},
  344.     "white", {.988235, .988235, .988235},
  345.     "yellow", {1, 1, 0},
  346.     "yellow_green", {.6, .8, .196078}
  347. } ;
  348.  
  349. int
  350. LookupColorByName(name, color)
  351.  char * name ;
  352.  Vec color ;
  353. {
  354.     int rc ;
  355.     rc = BinarySearch(name, 0, NCOLORS - 1 , Colors) ;
  356.     if (rc < 0) {
  357.         return(0) ;
  358.     }
  359.  
  360.     color[0] = Colors[rc].ce_color[0];
  361.     color[1] = Colors[rc].ce_color[1];
  362.     color[2] = Colors[rc].ce_color[2];
  363.     return 1 ;
  364. }
  365.  
  366.  
  367. int 
  368. BinarySearch(name, l, h, array)
  369.  char * name ;
  370.  int l, h ;
  371.  ColorEntry array[] ;
  372. {
  373.     int m, rc ;
  374.     if (l > h)
  375.         return(-1) ;
  376.     
  377.     m = (l + h) / 2 ;
  378.  
  379.     rc = strcmp(name, array[m].ce_name) ;
  380.     if (rc == 0)
  381.         return m ;
  382.     else if (rc < 0)
  383.         return BinarySearch(name, l, m-1, array) ;
  384.     else
  385.         return BinarySearch(name, m + 1, h, array) ;
  386. }
  387.