home *** CD-ROM | disk | FTP | other *** search
Lex Description | 1990-12-28 | 9.1 KB | 387 lines |
- %{
- #include <stdio.h>
- #include <math.h>
-
- typedef double Flt ;
- typedef Flt Vec[3] ;
- typedef Vec Point ;
- typedef Vec Color ;
-
- char *Progname;
- int yylinecount = 1;
- extern char yytext[] ;
- extern FILE * yyin ;
- Vec * pl, * plist ;
- %}
-
- %token VIEWPOINT FROM AT UP ANGLE HITHER RESOLUTION LIGHT
- %token BACKGROUND SURFACE CONE SPHERE POLYGON PATCH NUM TOKEN
-
- %union {
- Vec vec ;
- Vec * vecl ;
- double flt ;
- } ;
-
- %type <vec> point primcolor TOKEN
- %type <obj> cone sphere polygon ppatch
- %type <flt> num
-
- %%
-
- scene:
- camera elementlist ;
-
- elementlist:
- elementlist element
- | ;
-
- element:
- light
- | background
- | surface
- | object ;
-
- object: cone
- | sphere
- | polygon
- | ppatch ;
-
- camera:
- VIEWPOINT /* $1 */
- FROM point /* $2-$3 */
- AT point /* $4-$5 */
- UP point /* $6-$7 */
- ANGLE num /* $8-$9 */
- HITHER num /* $10-$11 */
- RESOLUTION num num /* $12-$14 */
- {
- printf("from %g %g %g\n", $3[0], $3[1], $3[2]);
- printf("at %g %g %g\n", $5[0], $5[1], $5[2]);
- printf("up %g %g %g\n", $7[0], $7[1], $7[2]);
- printf("angle %g\n", $11);
- printf("resolution %d %d\n", (int) $13, (int) $14);
- } ;
-
- light:
- LIGHT point
- {
- printf("light %g %g %g\n", $2[0], $2[1], $2[2]);
- } ;
-
- background:
- BACKGROUND primcolor
- {
- printf("background %g %g %g n\n", $2[0], $2[1], $2[2]);
- } ;
-
- surface:
- SURFACE primcolor num num num num num
- {
- printf("surface 1 1 1 %g 1 1 1 %g ", $4, $6);
- printf("%g %g %g %g %g %g 1 1 1 %g %g\n",
- $2[0] * 0.3, $2[1] * 0.3, $2[2] * 0.3,
- $2[0] * 0.7, $2[1] * 0.7, $2[2] * 0.7,
- $5, $7);
- } ;
-
- cone:
- CONE point num point num
- {
- printf("cone %g %g %g %g %g %g %g %g\n",
- $2[0], $2[1], $2[2], $3, $4[0], $4[1], $4[2], $5);
- } ;
-
- sphere:
- SPHERE point num
- {
- printf("sphere %g %g %g %g\n",
- $2[0], $2[1], $2[2], $3);
- } ;
-
- polygon:
- POLYGON num
- {
- printf("polygon %d\n", (int) $2);
- }
- pointlist
- {
-
- } ;
-
- ppatch:
- PATCH num
- {
- fprintf(stderr, "%s: sorry, rt doesn't support polygon patches.\n",
- Progname);
- } ;
-
- primcolor:
- num num num
- {
- $$[0] = $1 ;
- $$[1] = $2 ;
- $$[2] = $3 ;
- }
- | TOKEN
- {
- char buf[80] ;
-
- if (LookupColorByName(yytext, $$) == 0) {
- sprintf(buf, "cannot find color \"%s\"\n",
- yytext) ;
- yyerror(buf) ;
- }
- } ;
-
-
- point:
- num num num
- {
- $$[0] = $1 ;
- $$[1] = $2 ;
- $$[2] = $3 ;
- } ;
-
- pointlist:
- pointlist point
- {
- printf("%g %g %g\n", $2[0], $2[1], $2[2]);
- }
- | ;
-
-
- num:
- NUM
- {
- $$ = atof(yytext) ;
- } ;
-
- %%
-
- yyerror(str)
- char * str ;
- {
- fprintf(stderr, "%s: error at line %d\n",
- Progname, yylinecount) ;
- fprintf(stderr, "%s: %s\n", Progname, str) ;
- exit(-1) ;
- }
-
-
- main(argc, argv)
- int argc;
- char *argv[] ;
- {
- Progname = argv[0];
-
- yyin = stdin ;
- if (yyparse() == 1) {
- fprintf(stderr, "%s: invalid input specification\n", Progname);
- exit(-1) ;
- }
- }
-
-
- #define NCOLORS (142)
-
- typedef struct t_color_entry {
- char * ce_name ;
- Vec ce_color ;
- } ColorEntry ;
-
- #define LESS_THAN -1
- #define GREATER_THAN 1
- #define EQUAL_TO 0
-
- /*
- * Note: These colors must be in sorted order, because we binary search
- * for them.
- *
- * They were swiped from the X-11 distribution. Sorry....
- */
-
- ColorEntry Colors[] = {
- "Aquamarine", {.439216, .858824, .576471},
- "Black", {0, 0, 0},
- "Blue", {0, 0, 1},
- "BlueViolet", {.623529, .372549, .623529},
- "Brown", {.647059, .164706, .164706},
- "CadetBlue", {.372549, .623529, .623529},
- "Coral", {1, .498039, 0},
- "CornflowerBlue", {.258824, .258824, .435294},
- "Cyan", {0, 1, 1},
- "DarkGreen", {.184314, .309804, .184314},
- "DarkOliveGreen", {.309804, .309804, .184314},
- "DarkOrchid", {.6, .196078, .8},
- "DarkSlateBlue", {.419608, .137255, .556863},
- "DarkSlateGray", {.184314, .309804, .309804},
- "DarkSlateGrey", {.184314, .309804, .309804},
- "DarkTurquoise", {.439216, .576471, .858824},
- "DimGray", {.329412, .329412, .329412},
- "DimGrey", {.329412, .329412, .329412},
- "Firebrick", {.556863, .137255, .137255},
- "ForestGreen", {.137255, .556863, .137255},
- "Gold", {.8, .498039, .196078},
- "Goldenrod", {.858824, .858824, .439216},
- "Gray", {.752941, .752941, .752941},
- "Green", {0, 1, 0},
- "GreenYellow", {.576471, .858824, .439216},
- "Grey", {.752941, .752941, .752941},
- "IndianRed", {.309804, .184314, .184314},
- "Khaki", {.623529, .623529, .372549},
- "LightBlue", {.74902, .847059, .847059},
- "LightGray", {.658824, .658824, .658824},
- "LightGrey", {.658824, .658824, .658824},
- "LightSteelBlue", {.560784, .560784, .737255},
- "LimeGreen", {.196078, .8, .196078},
- "Magenta", {1, 0, 1},
- "Maroon", {.556863, .137255, .419608},
- "MediumAquamarine", {.196078, .8, .6},
- "MediumBlue", {.196078, .196078, .8},
- "MediumForestGreen", {.419608, .556863, .137255},
- "MediumGoldenrod", {.917647, .917647, .678431},
- "MediumOrchid", {.576471, .439216, .858824},
- "MediumSeaGreen", {.258824, .435294, .258824},
- "MediumSlateBlue", {.498039, 0, 1},
- "MediumSpringGreen", {.498039, 1, 0},
- "MediumTurquoise", {.439216, .858824, .858824},
- "MediumVioletRed", {.858824, .439216, .576471},
- "MidnightBlue", {.184314, .184314, .309804},
- "Navy", {.137255, .137255, .556863},
- "NavyBlue", {.137255, .137255, .556863},
- "Orange", {.8, .196078, .196078},
- "OrangeRed", {1, 0, .498039},
- "Orchid", {.858824, .439216, .858824},
- "PaleGreen", {.560784, .737255, .560784},
- "Pink", {.737255, .560784, .560784},
- "Plum", {.917647, .678431, .917647},
- "Red", {1, 0, 0},
- "Salmon", {.435294, .258824, .258824},
- "SeaGreen", {.137255, .556863, .419608},
- "Sienna", {.556863, .419608, .137255},
- "SkyBlue", {.196078, .6, .8},
- "SlateBlue", {0, .498039, 1},
- "SpringGreen", {0, 1, .498039},
- "SteelBlue", {.137255, .419608, .556863},
- "Tan", {.858824, .576471, .439216},
- "Thistle", {.847059, .74902, .847059},
- "Turquoise", {.678431, .917647, .917647},
- "Violet", {.309804, .184314, .309804},
- "VioletRed", {.8, .196078, .6},
- "Wheat", {.847059, .847059, .74902},
- "White", {.988235, .988235, .988235},
- "Yellow", {1, 1, 0},
- "YellowGreen", {.6, .8, .196078},
- "aquamarine", {.439216, .858824, .576471},
- "black", {0, 0, 0},
- "blue", {0, 0, 1},
- "blue_violet", {.623529, .372549, .623529},
- "brown", {.647059, .164706, .164706},
- "cadet_blue", {.372549, .623529, .623529},
- "coral", {1, .498039, 0},
- "cornflower_blue", {.258824, .258824, .435294},
- "cyan", {0, 1, 1},
- "dark_green", {.184314, .309804, .184314},
- "dark_olive_green", {.309804, .309804, .184314},
- "dark_orchid", {.6, .196078, .8},
- "dark_slate_blue", {.419608, .137255, .556863},
- "dark_slate_gray", {.184314, .309804, .309804},
- "dark_slate_grey", {.184314, .309804, .309804},
- "dark_turquoise", {.439216, .576471, .858824},
- "dim_gray", {.329412, .329412, .329412},
- "dim_grey", {.329412, .329412, .329412},
- "firebrick", {.556863, .137255, .137255},
- "forest_green", {.137255, .556863, .137255},
- "gold", {.8, .498039, .196078},
- "goldenrod", {.858824, .858824, .439216},
- "gray", {.752941, .752941, .752941},
- "green", {0, 1, 0},
- "green_yellow", {.576471, .858824, .439216},
- "grey", {.752941, .752941, .752941},
- "indian_red", {.309804, .184314, .184314},
- "khaki", {.623529, .623529, .372549},
- "light_blue", {.74902, .847059, .847059},
- "light_gray", {.658824, .658824, .658824},
- "light_grey", {.658824, .658824, .658824},
- "light_steel_blue", {.560784, .560784, .737255},
- "lime_green", {.196078, .8, .196078},
- "magenta", {1, 0, 1},
- "maroon", {.556863, .137255, .419608},
- "medium_aquamarine", {.196078, .8, .6},
- "medium_blue", {.196078, .196078, .8},
- "medium_forest_green", {.419608, .556863, .137255},
- "medium_goldenrod", {.917647, .917647, .678431},
- "medium_orchid", {.576471, .439216, .858824},
- "medium_sea_green", {.258824, .435294, .258824},
- "medium_slate_blue", {.498039, 0, 1},
- "medium_spring_green", {.498039, 1, 0},
- "medium_turquoise", {.439216, .858824, .858824},
- "medium_violet_red", {.858824, .439216, .576471},
- "midnight_blue", {.184314, .184314, .309804},
- "navy", {.137255, .137255, .556863},
- "navy_blue", {.137255, .137255, .556863},
- "orange", {.8, .196078, .196078},
- "orange_red", {1, 0, .498039},
- "orchid", {.858824, .439216, .858824},
- "pale_green", {.560784, .737255, .560784},
- "pink", {.737255, .560784, .560784},
- "plum", {.917647, .678431, .917647},
- "red", {1, 0, 0},
- "salmon", {.435294, .258824, .258824},
- "sea_green", {.137255, .556863, .419608},
- "sienna", {.556863, .419608, .137255},
- "sky_blue", {.196078, .6, .8},
- "slate_blue", {0, .498039, 1},
- "spring_green", {0, 1, .498039},
- "steel_blue", {.137255, .419608, .556863},
- "tan", {.858824, .576471, .439216},
- "thistle", {.847059, .74902, .847059},
- "turquoise", {.678431, .917647, .917647},
- "violet", {.309804, .184314, .309804},
- "violet_red", {.8, .196078, .6},
- "wheat", {.847059, .847059, .74902},
- "white", {.988235, .988235, .988235},
- "yellow", {1, 1, 0},
- "yellow_green", {.6, .8, .196078}
- } ;
-
- int
- LookupColorByName(name, color)
- char * name ;
- Vec color ;
- {
- int rc ;
- rc = BinarySearch(name, 0, NCOLORS - 1 , Colors) ;
- if (rc < 0) {
- return(0) ;
- }
-
- color[0] = Colors[rc].ce_color[0];
- color[1] = Colors[rc].ce_color[1];
- color[2] = Colors[rc].ce_color[2];
- return 1 ;
- }
-
-
- int
- BinarySearch(name, l, h, array)
- char * name ;
- int l, h ;
- ColorEntry array[] ;
- {
- int m, rc ;
- if (l > h)
- return(-1) ;
-
- m = (l + h) / 2 ;
-
- rc = strcmp(name, array[m].ce_name) ;
- if (rc == 0)
- return m ;
- else if (rc < 0)
- return BinarySearch(name, l, m-1, array) ;
- else
- return BinarySearch(name, m + 1, h, array) ;
- }
-