home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / etc / nff2shad.awk < prev   
Text File  |  1992-02-09  |  4KB  |  137 lines

  1. #
  2. # This awk script will convert an NFF-format input file (as output by
  3. # Eric Haines' SPD) to something rayshade can understand.
  4. # The World object will be enclosed in a single grid of 22*22*22 voxels.
  5. #
  6. # Example usage:
  7. #    mount | awk -f sped2shade.awk | rayshade > mountains.rle
  8. #
  9. # For best results, one should modify the output for all but the tetra
  10. # and mountain databases to provide a tighter bounding box around the
  11. # object of interest (tree, gears, etc.).  This is done by moving
  12. # ground polygons and the like outside of the topmost grid object.
  13. # This will decrease ray-tracing time dramatically.
  14. #
  15. # Note that we have to make sure that the viewing paramters are output
  16. # outside of the object definition block.  We do this by printing
  17. # the eye position, etc., at the very end of the output.
  18. #
  19. BEGIN{
  20.     init = 0;
  21.     lights = 0;
  22.     print "maxdepth 4"
  23.     print "sample 1 nojitter"
  24.     print "cutoff 0."
  25.     print "report verbose"
  26. }
  27. substr($1, 1, 1) == "#" { print "/* " $0 " */"; next;}
  28. $1 == "v" { next;}
  29. $1 == "from" { eyex = $2; eyey = $3; eyez = $4; next;}
  30. $1 == "at" { atx = $2; aty = $3; atz = $4; next;}
  31. $1 == "up" { upx = $2; upy = $3; upz = $4; next;}
  32. $1 == "angle" { fov = $2; next;}
  33. $1 == "hither" {next;}
  34. $1 == "resolution" {screenx = $2; screeny = $3; next;}
  35.  
  36. $1 == "l" { lightd[lights] = $2 " "$3 " "$4; lights++; next; }
  37. $1 == "b" {print "background " $2 " "$3 " "$4; next; }
  38. $1 == "f" {
  39.     if (init == 0) {
  40.         print "grid 22 22 22";
  41.         init = 1;
  42.     }
  43.     printf("applysurf ");
  44.     if (lights != 0)
  45.         aintens = sqrt(lights) / (4*lights);
  46.     else
  47.         aintens = .1;
  48.     dr = $2*$5;
  49.     dg = $3*$5;
  50.     db = $4*$5;
  51. # this is a good guess....
  52.     ar = aintens*dr;
  53.     ag = aintens*dg;
  54.     ab = aintens*db;
  55.     if (ar != 0 || ag != 0 || ab != 0)
  56.         printf("\tambient %f %f %f\n", ar, ag, ab);
  57.     if (dr != 0 || dg != 0 || db != 0)
  58.         printf("\tdiffuse %f %f %f\n", dr, dg, db);
  59. #
  60. # This gets a little strange.  We're given a color, Ks, and T.
  61. # We need a specular color, a specular reflectivity (for reflected
  62. # rays), and a transparency (for transmitted rays).
  63. # In rayshade, reflected rays have intensity proportional to
  64. # specular_color*reflectivity, transmitted proportaional to
  65. # specular_color*transparency, and specular hilights to
  66. # specular_color.  Also, Ks + T >1 for some SPDs.
  67. #
  68.     if ($6) {
  69.         sr = $2*$6;
  70.         sg = $3*$6;
  71.         sb = $4*$6;
  72.         printf("\tspecular %f %f %f specpow %f\n", sr, sg, sb, $7);
  73.     }
  74.     if ($6 < 1. - $8)
  75.         printf("\treflect 1.0\n");
  76.     else
  77.         printf("\treflect %f\n", 1. - $8);
  78.  
  79.     if ($8 || $9)
  80.         printf("\ttransp %f index %f\n", $8, $9);
  81.     next;
  82. }
  83.  
  84. $1 == "c" {
  85.     getline;
  86.     x1 = $1;
  87.     y1 = $2;
  88.     z1 = $3;
  89.     br = $4;
  90.     getline;
  91.     printf("cone %f %f %f %f %f %f %f %f\n", \
  92.         br, x1, y1, z1, $4, $1, $2, $3);
  93.     next;
  94. }
  95. $1 == "s" {
  96.     print "sphere "$5 " "$2 " "$3 " "$4;
  97.     next;
  98. }
  99. $1 == "pp" {
  100.     if ($2 == 3)
  101.         print "triangle ";
  102.     else
  103.         print "poly ";
  104.     next;
  105. }
  106. $1 == "p" {
  107. #
  108. # Polygon -- the vertices will print out in the default statement.
  109. # If there are three vertices, make it a triangle.
  110. #
  111.     if ($2 == 3)
  112.         print "triangle ";
  113.     else
  114.         print "poly ";
  115.     next;
  116. }
  117. {
  118. # Matched nothing (or is a vertex data) -- print it.
  119.     print;
  120.     next;
  121. }
  122. END{
  123.     print "end"
  124. #
  125. # Output light definitions.
  126. #
  127.     intens = sqrt(lights) / (lights);
  128.     for (i = 0; i < lights; i++) {
  129.         print "light " intens " point " lightd[i]
  130.     }
  131.     printf("eyep %g %g %g\n", eyex, eyey, eyez);
  132.     printf("lookp %g %g %g\n", atx, aty, atz);
  133.     printf("up %g %g %g\n", upx, upy, upz);
  134.     printf("fov %g\n", fov);
  135.     printf("screen %d %d\n", screenx, screeny);
  136. }
  137.