home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / rayshade / part02 / nff2shade.awk
Encoding:
AWK Script  |  1990-03-21  |  3.1 KB  |  127 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. #    mountains | awk -f nff2shade.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 placing
  12. # all primitives but the ground pologon into an object stored as a Grid,
  13. # and then instantiating that object once.  This will decrease ray-tracing
  14. # time dramatically.
  15. #
  16. # $Id: nff2shade.awk,v 3.0 89/10/27 01:30:24 craig Exp $
  17. #
  18. # $Log:    nff2shade.awk,v $
  19. # Revision 3.0  89/10/27  01:30:24  craig
  20. # Baseline for first official release.
  21. # C. Kolb 9/21/89
  22. # Fixed comment bug; check first character rather than first field for "#".
  23. # Keep track of defined surfaces in associative array so if one is used
  24. # again we don't create another copy.
  25. # Halved ambient intensity of surfaces.
  26. # C. Kolb 10/12/89
  27. # Added "adaptive 0" and "cutoff 0" to BEGIN and removed "endfile" from END.
  28. # C. Kolb 2/89
  29. # First version.
  30. #
  31. BEGIN{
  32.     lights = 0;
  33.     surfs = 0;
  34.     print "maxdepth 4"
  35.     print "adaptive 0"
  36.     print "cutoff 0."
  37. }
  38. substr($1, 1, 1) == "#" { print "/* " $0 " */"; next;}
  39. $1 == "v" { next;}
  40. $1 == "from" { print "eyep " $2 " "$3 " " $4; next;}
  41. $1 == "at" {print "lookp " $2 " "$3 " "$4; next;}
  42. $1 == "up" {print; next;}
  43. $1 == "angle" { print "fov " $2; next;}
  44. $1 == "hither" {next;}
  45. $1 == "resolution" {print "screen " $2 " "$3; next;}
  46.  
  47. $1 == "l" { lightd[lights] = $2 " "$3 " "$4; lights++; next; }
  48. $1 == "b" {print "background " $2 " "$3 " "$4; next; }
  49. $1 == "f" {
  50.     if (surfaces[$2 $3 $4 $5 $6 $7 $8] != 0) {
  51.         cursurf = surfaces[$2 $3 $4 $5 $6 $7 $8];
  52.         next;
  53.     }
  54.     surfs++;
  55.     surfaces[$2 $3 $4 $5 $6 $7 $8] = surfs;
  56.     cursurf = surfs;
  57.     aintens = sqrt(lights) / (4*lights);
  58.     dr = $2*$5;
  59.     dg = $3*$5;
  60.     db = $4*$5;
  61. # this is a good guess....
  62.     ar = aintens*dr;
  63.     ag = aintens*dg;
  64.     ab = aintens*db;
  65. #
  66. # We set "reflectance" to the minimum of Ks and 1. - T, as
  67. # Eric Haines' SPD includes several objects which have Ks + T > 1.
  68. #
  69.     if ($6 < 1. - $8)
  70.         reflect = $6;
  71.     else
  72.         reflect = 1. - $8;
  73.     printf("surface s%d  %f %f %f  %f %f %f  %f %f %f  %f %f %f %f\n", \
  74.         cursurf, ar, ag, ab, dr, dg, db, $6, $6, $6, $7, reflect, $8, $9);
  75.     next;
  76. }
  77.  
  78. $1 == "c" {
  79.     getline;
  80.     x1 = $1;
  81.     y1 = $2;
  82.     z1 = $3;
  83.     br = $4;
  84.     getline;
  85.     printf("cone s%d %f %f %f %f %f %f %f %f\n", \
  86.         cursurf, x1, y1, z1, $1, $2, $3, br, $4);
  87.     next;
  88. }
  89. $1 == "s" {
  90.     print "sphere s"cursurf " "$5 " "$2 " "$3 " "$4;
  91.     next;
  92. }
  93. $1 == "pp" {
  94.     if ($2 == 3)
  95.         print "triangle s"cursurf;
  96.     else
  97.         print "poly s"cursurf;
  98.     next;
  99. }
  100. $1 == "p" {
  101. #
  102. # Polygon -- the vertices will print out in the default statement.
  103. # If there are three vertices, make it a triangle.
  104. #
  105.     if ($2 == 3)
  106.         print "triangle s"cursurf;
  107.     else
  108.         print "poly s"cursurf;
  109.     next;
  110. }
  111. {
  112. # Matched nothing (or is a vertex data) -- print it.
  113.     print;
  114.     next;
  115. }
  116. END{
  117. #
  118. # Output light definitions.
  119. #
  120.     intens = sqrt(lights) / (lights);
  121.     for (i = 0; i < lights; i++) {
  122.         print "light " intens " point " lightd[i]
  123.     }
  124.     print "grid 22 22 22"
  125. }
  126.