home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / mtvraytrace / part01 / nff.y < prev    next >
Encoding:
Lex Description  |  1989-03-26  |  3.8 KB  |  226 lines

  1. %{
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include "defs.h"
  5. #include "extern.h"
  6.  
  7. extern char yytext[] ;
  8. extern FILE * yyin ;
  9. Vec * pl, * plist ;
  10. %}
  11.  
  12. %token VIEWPOINT FROM AT UP ANGLE HITHER RESOLUTION LIGHT
  13. %token BACKGROUND SURFACE CONE SPHERE POLYGON PATCH NUM TOKEN
  14.  
  15. %union {
  16.     Vec    vec ;
  17.     Vec *    vecl ;
  18.     double    flt ;
  19.     Object * obj ;
  20. } ;
  21.  
  22. %type <vec>    point primcolor TOKEN
  23. %type <obj>    cone sphere polygon ppatch
  24. %type <flt>    num 
  25.  
  26. %%
  27.  
  28. scene:
  29.     camera elementlist 
  30.     {
  31.         int i, l ;
  32.         for (l = 0 ; l < nLights ; l++) {
  33.             Lights[l].light_brightness =  sqrt((Flt) nLights) / 
  34.                             ((Flt)nLights) ;
  35.             for (i = 0 ; i < MAXLEVEL ; i++) {
  36.                 Lights[l].light_obj_cache[i] = NULL ;
  37.             }
  38.         }
  39.     } ;
  40.  
  41. elementlist:
  42.     elementlist element
  43.     |  ;
  44.  
  45. element:
  46.     light
  47.     | background
  48.     | surface
  49.     | object 
  50.     {
  51.         char buf[80] ;
  52.         if (nPrims >= MAXPRIMS) {
  53.             sprintf(buf, "max objects = %d", MAXPRIMS) ;
  54.             yyerror(buf) ;
  55.         }
  56.     }    ;
  57.  
  58. object:      cone
  59.     | sphere
  60.     | polygon
  61.     | ppatch ;
  62.  
  63. camera:
  64.     VIEWPOINT            /* $1      */
  65.     FROM point            /* $2-$3   */
  66.     AT point            /* $4-$5   */
  67.     UP point            /* $6-$7   */
  68.     ANGLE num             /* $8-$9   */
  69.     HITHER num            /* $10-$11 */
  70.     RESOLUTION num num        /* $12-$14 */
  71.     {
  72.         VecCopy($3, Eye.view_from) ;
  73.         VecCopy($5, Eye.view_at) ;
  74.         VecCopy($7, Eye.view_up) ;
  75.         Eye.view_angle = degtorad($9/2.0 ) ;
  76.         Eye.view_dist = $11 ;
  77.         if (resolutionflag > 0) {
  78.             /* ignore the specified resolution... */
  79.             Xresolution = Yresolution = resolutionflag ;
  80.         } else {
  81.             Xresolution = (int) $13 ;
  82.             Yresolution = (int) $14 ;
  83.         }
  84.     } ;
  85.  
  86. light:
  87.     LIGHT point 
  88.     {
  89.         VecCopy($2, Lights[nLights].light_pos) ;
  90.         /* fill in brightness of the light, after we 
  91.          * know the number of lights sources in the scene
  92.          */
  93.         nLights ++ ;
  94.     } ;
  95.  
  96. background:
  97.     BACKGROUND primcolor
  98.     {
  99.         VecCopy($2, BackgroundColor) ;
  100.     } ;
  101.  
  102. surface:
  103.     SURFACE primcolor num num num num num 
  104.     {
  105.         CurrentSurface = (Surface *) malloc (sizeof(Surface)) ;
  106.         VecCopy($2, CurrentSurface -> surf_color) ;
  107.         CurrentSurface -> surf_kd = $3 ;
  108.         CurrentSurface -> surf_ks = $4 ;
  109.         CurrentSurface -> surf_shine = $5 ;
  110.         CurrentSurface -> surf_kt = $6 ;
  111.         CurrentSurface -> surf_ior = $7 ;
  112.     } ;
  113.  
  114. cone:
  115.     CONE point num point num 
  116.     {
  117.         $$ = MakeCone($2, $3, $4, $5) ;
  118.         Prims[nPrims++] = $$ ;
  119.  
  120.     } ;
  121. sphere:
  122.     SPHERE point num 
  123.     {
  124.         $$ = MakeSphere($2, $3) ;
  125.         Prims[nPrims++] = $$ ;
  126.     } ;
  127.  
  128. polygon:
  129.     POLYGON num 
  130.     {
  131.         plist = (Vec *) calloc((int) $2, sizeof(Vec)) ;
  132.         pl = plist ;
  133.     } 
  134.     pointlist 
  135.     {
  136.         $$ = MakePoly((int) $2, plist) ;
  137.         Prims[nPrims++] = $$ ;
  138.     } ;
  139. ppatch:
  140.     PATCH num
  141.         {
  142.         if ((int) $2 != 3)
  143.             fprintf(stderr, "patches must have 3 vertices...\n") ;
  144.         plist = (Vec *) calloc(2 * (int) $2, sizeof(Vec)) ;
  145.         pl = plist ;
  146.     }
  147.     pointlist
  148.     {
  149.         $$ = MakeTri(plist) ;
  150.         Prims[nPrims++] = $$ ;
  151.     } ;
  152.  
  153. primcolor:
  154.     num num num 
  155.     {
  156.         $$[0] = $1 ;
  157.         $$[1] = $2 ;
  158.         $$[2] = $3 ;
  159.     }  
  160.     | TOKEN
  161.     {
  162.         char buf[80] ;
  163.  
  164.         if (LookupColorByName(yytext, $$) == 0) {
  165.             sprintf(buf, "cannot find color \"%s\"\n",
  166.                 yytext) ;
  167.             yyerror(buf) ;
  168.         }
  169.     } ;
  170.  
  171. point:
  172.     num num num
  173.     {
  174.         $$[0] = $1 ;
  175.         $$[1] = $2 ;
  176.         $$[2] = $3 ;
  177.     } ;
  178.  
  179. pointlist:
  180.     pointlist point
  181.     {
  182.         VecCopy($2, (*pl)) ;
  183.         pl ++ ;
  184.     } 
  185.     | ;
  186.  
  187. num:
  188.     NUM
  189.         {
  190.             $$ = atof(yytext) ;
  191.         } ;
  192.  
  193. %%
  194.  
  195. yyerror(str)
  196.  char * str ;
  197. {
  198.     fprintf(stderr, "%s: error at line %d\n", 
  199.         Progname, yylinecount) ;
  200.     fprintf(stderr, "%s: %s\n", Progname, str) ;
  201.     exit(-1) ;
  202. }
  203.  
  204. ReadSceneFile(str)
  205.  char *str ;
  206. {
  207.     if (str == NULL) 
  208.         yyin = stdin ;
  209.     else {
  210.         if ((yyin = fopen(str, "r")) == NULL) {
  211.             fprintf(stderr, "%s: cannot open %s\n", Progname,
  212.                         str) ;
  213.             exit(-1) ;
  214.         }
  215.     }
  216.     if (yyparse() == 1) {
  217.         fprintf(stderr, "%s: invalid input specification\n", Progname);
  218.         exit(-1) ;
  219.     }
  220.     fprintf(stderr, "%s: %d prims, %d lights\n", 
  221.             Progname, nPrims, nLights) ;
  222.     fprintf(stderr, "%s: inputfile = \"%s\"\n", Progname, str) ;
  223.     fprintf(stderr, "%s: resolution %dx%d\n", Progname, Xresolution,
  224.         Yresolution) ;
  225. }
  226.