home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / readnff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  12.4 KB  |  507 lines  |  [TEXT/MMCC]

  1. /*
  2.  * ReadNFF.c - Simple NFF file importer.  Uses lib to output to
  3.  * many different raytracer formats.
  4.  *
  5.  * Author:  Eduard [esp] Schwan
  6.  *
  7.  * input file parameter...
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <string.h>    /* strchr */
  13. #include <stdlib.h>    /* atoi */
  14. #include "def.h"
  15. #include "drv.h"    /* display_close() */
  16. #include "lib.h"
  17.  
  18.  
  19. /* These may be read from the command line */
  20. static int raytracer_format = OUTPUT_RT_DEFAULT;
  21. static int output_format    = OUTPUT_CURVES;
  22.  
  23.  
  24. /*----------------------------------------------------------------------
  25. Handle an error
  26. ----------------------------------------------------------------------*/
  27. static void
  28. show_error(s)
  29. char    * s;
  30. {
  31.     /* SysBeep(1); */
  32.     lib_output_comment("### ERROR! ###\n");
  33.     lib_output_comment(s);
  34.     lib_close();
  35. }
  36.  
  37. /*----------------------------------------------------------------------
  38. Comment.  Description:
  39.     "#" [ string ]
  40.  
  41. Format:
  42.     # [ string ]
  43.  
  44.     As soon as a "#" character is detected, the rest of the line is considered
  45.     a comment.
  46. ----------------------------------------------------------------------*/
  47. static void
  48. do_comment(fp)
  49. FILE *fp;
  50. {
  51.     char    *cp;
  52.     char    comment[256];
  53.  
  54.     fgets(comment, 255, fp);
  55.     /* strip out newline */
  56.     cp = (char*)strchr(comment, '\n');
  57.     if (cp != NULL)
  58.         *cp = '\0';
  59.     lib_output_comment(comment);
  60. }
  61.  
  62.  
  63. /*----------------------------------------------------------------------
  64. Viewpoint location.  Description:
  65.     "v"
  66.     "from" Fx Fy Fz
  67.     "at" Ax Ay Az
  68.     "up" Ux Uy Uz
  69.     "angle" angle
  70.     "hither" hither
  71.     "resolution" xres yres
  72.  
  73. Format:
  74.  
  75.     v
  76.     from %g %g %g
  77.     at %g %g %g
  78.     up %g %g %g
  79.     angle %g
  80.     hither %g
  81.     resolution %d %d
  82.  
  83. The parameters are:
  84.  
  85.     From:  the eye location in XYZ.
  86.     At:    a position to be at the center of the image, in XYZ world
  87.        coordinates.  A.k.a. "lookat".
  88.     Up:    a vector defining which direction is up, as an XYZ vector.
  89.     Angle: in degrees, defined as from the center of top pixel row to
  90.        bottom pixel row and left column to right column.
  91.     Resolution: in pixels, in x and in y.
  92.  
  93.   Note that no assumptions are made about normalizing the data (e.g. the
  94.   from-at distance does not have to be 1).  Also, vectors are not
  95.   required to be perpendicular to each other.
  96.  
  97.   For all databases some viewing parameters are always the same:
  98.     Yon is "at infinity."
  99.     Aspect ratio is 1.0.
  100.  
  101.   A view entity must be defined before any objects are defined (this
  102.   requirement is so that NFF files can be used by hidden surface machines).
  103. ----------------------------------------------------------------------*/
  104. static void
  105. do_view(fp)
  106. FILE *fp;
  107. {
  108.     float    x,y,z;
  109.     COORD3 from;
  110.     COORD3 at;
  111.     COORD3 up;
  112.     float fov_angle;
  113.     float aspect_ratio;
  114.     float hither;
  115.     int resx;
  116.     int resy;
  117.  
  118.     if (fscanf(fp, " from %f %f %f", &x, &y, &z) != 3)
  119.         goto fmterr;
  120.     SET_COORD3(from, x,y,z);
  121.  
  122.     if (fscanf(fp, " at %f %f %f", &x, &y, &z) != 3)
  123.         goto fmterr;
  124.     SET_COORD3(at, x,y,z);
  125.  
  126.     if (fscanf(fp, " up %f %f %f", &x, &y, &z) != 3)
  127.         goto fmterr;
  128.     SET_COORD3(up, x,y,z);
  129.  
  130.     if (fscanf(fp, " angle %f", &fov_angle) != 1)
  131.         goto fmterr;
  132.  
  133.     fscanf(fp, " hither %f", &hither);
  134.  
  135.     aspect_ratio = 1.0;
  136.  
  137.     fscanf(fp, " resolution %d %d", &resx, &resy);
  138.  
  139.     lib_output_viewpoint(from, at, up,
  140.                   fov_angle, aspect_ratio,
  141.                   hither, resx, resy);
  142.     return;
  143. fmterr:
  144.     show_error("NFF view syntax error");
  145.     exit(1);
  146. }
  147.  
  148.  
  149. /*----------------------------------------------------------------------
  150. Positional light.  A light is defined by XYZ position.  Description:
  151.     "l" X Y Z
  152.  
  153. Format:
  154.     l %g %g %g
  155.  
  156.     All light entities must be defined before any objects are defined (this
  157.     requirement is so that NFF files can be used by hidden surface machines).
  158.     Lights have a non-zero intensity of no particular value [this definition
  159.     may change soon, with the addition of an intensity and/or color].
  160. ----------------------------------------------------------------------*/
  161. static void
  162. do_light(fp)
  163. FILE *fp;
  164. {
  165.     float    x, y, z;
  166.     COORD4 acenter;
  167.  
  168.    if (fscanf(fp, "%f %f %f",&x, &y, &z) != 3) {
  169.     show_error("Light source syntax error");
  170.     exit(1);
  171.     }
  172.  
  173.     SET_COORD4(acenter,x,y,z,0.0); /* intensity=0 */
  174.  
  175.     lib_output_light(acenter);
  176. }
  177.  
  178.  
  179. /*----------------------------------------------------------------------
  180. Background color.  A color is simply RGB with values between 0 and 1:
  181.     "b" R G B
  182.  
  183. Format:
  184.     b %g %g %g
  185.  
  186.     If no background color is set, assume RGB = {0,0,0}.
  187. ----------------------------------------------------------------------*/
  188. static void
  189. do_background(fp)
  190. FILE *fp;
  191. {
  192.     float    r, g, b;
  193.     COORD3 acolor;
  194.  
  195.    if (fscanf(fp, "%f %f %f",&r, &g, &b) != 3) {
  196.     show_error("background color syntax error");
  197.     exit(1);
  198.     }
  199.     SET_COORD3(acolor,r,g,b);
  200.  
  201.     lib_output_background_color(acolor);
  202. }
  203.  
  204.  
  205. /*----------------------------------------------------------------------
  206. Fill color and shading parameters.  Description:
  207.      "f" red green blue Kd Ks Shine T index_of_refraction
  208.  
  209. Format:
  210.     f %g %g %g %g %g %g %g %g
  211.  
  212.     RGB is in terms of 0.0 to 1.0.
  213.  
  214.     Kd is the diffuse component, Ks the specular, Shine is the Phong cosine
  215.     power for highlights, T is transmittance (fraction of light passed per
  216.     unit).  Usually, 0 <= Kd <= 1 and 0 <= Ks <= 1, though it is not required
  217.     that Kd + Ks == 1.  Note that transmitting objects ( T > 0 ) are considered
  218.     to have two sides for algorithms that need these (normally objects have
  219.     one side).
  220.  
  221.     The fill color is used to color the objects following it until a new color
  222.     is assigned.
  223. ----------------------------------------------------------------------*/
  224. static void
  225. do_fill(fp)
  226. FILE *fp;
  227. {
  228.     float    r, g, b, ka, kd, ks, shine, phong_pow, ang, t, ior;
  229.     COORD3 acolor;
  230.  
  231.    if (fscanf(fp, "%f %f %f",&r, &g, &b) != 3) {
  232.     show_error("fill color syntax error");
  233.     exit(1);
  234.     }
  235.     SET_COORD3(acolor,r,g,b);
  236.  
  237.     if (fscanf(fp, "%f %f %f %f %f", &kd, &ks, &phong_pow, &t, &ior) != 5) {
  238.         show_error("fill material syntax error");
  239.         exit(1);
  240.     }
  241.  
  242.     /* ooh yuck, some parms not input in NFF, so I hard-coded them for now [esp] */
  243.     ka = 0.1;
  244.     shine = 0.5;
  245.     /* convert phong_pow back into phong hilite angle. */
  246.     /* reciprocal of formula in libprm.c, lib_output_color() */
  247.     ang = (180.0/(2.0*PI)) * acos( exp((-log(2.0))/phong_pow) );
  248.     lib_output_color(NULL, acolor, ka, kd, ks, shine, ang, t, ior);
  249.  
  250. }
  251.  
  252.  
  253. /*----------------------------------------------------------------------
  254. Cylinder or cone.  A cylinder is defined as having a radius and an axis
  255.     defined by two points, which also define the top and bottom edge of the
  256.     cylinder.  A cone is defined similarly, the difference being that the apex
  257.     and base radii are different.  The apex radius is defined as being smaller
  258.     than the base radius.  Note that the surface exists without endcaps.  The
  259.     cone or cylinder description:
  260.  
  261.     "c"
  262.     base.x base.y base.z base_radius
  263.     apex.x apex.y apex.z apex_radius
  264.  
  265. Format:
  266.     c
  267.     %g %g %g %g
  268.     %g %g %g %g
  269.  
  270.     A negative value for both radii means that only the inside of the object is
  271.     visible (objects are normally considered one sided, with the outside
  272.     visible).  Note that the base and apex cannot be coincident for a cylinder
  273.     or cone.
  274. ----------------------------------------------------------------------*/
  275. static void
  276. do_cone(fp)
  277. FILE *fp;
  278. {
  279.     COORD4    base_pt;
  280.     COORD4    apex_pt;
  281.     float    x0, y0, z0, x1, y1, z1, r0, r1;
  282.  
  283.     if (fscanf(fp, " %f %f %f %f %f %f %f %f", &x0, &y0, &z0, &r0,
  284.             &x1, &y1, &z1, &r1) != 8) {
  285.         show_error("cylinder or cone syntax error");
  286.         exit(1);
  287.     }
  288.     if ( r0 < 0.0) {
  289.         r0 = -r0;
  290.         r1 = -r1;
  291.     }
  292.     SET_COORD4(base_pt,x0,y0,z0,r0);
  293.     SET_COORD4(apex_pt,x1,y1,z1,r1);
  294.  
  295.     lib_output_cylcone (base_pt, apex_pt, output_format);
  296. }
  297.  
  298.  
  299. /*----------------------------------------------------------------------
  300. Sphere.  A sphere is defined by a radius and center position:
  301.     "s" center.x center.y center.z radius
  302.  
  303. Format:
  304.     s %g %g %g %g
  305.  
  306.     If the radius is negative, then only the sphere's inside is visible
  307.     (objects are normally considered one sided, with the outside visible).
  308. ----------------------------------------------------------------------*/
  309. static void
  310. do_sphere(fp)
  311. FILE *fp;
  312. {
  313.     float    x, y, z, r;
  314.     COORD4    center_pt;
  315.  
  316.     if (fscanf(fp, "%f %f %f %f", &x, &y, &z, &r) != 4) {
  317.         show_error("sphere syntax error");
  318.         exit(1);
  319.     }
  320.  
  321.     SET_COORD4(center_pt,x,y,z,r);
  322.  
  323.     lib_output_sphere(center_pt, output_format);
  324. }
  325.  
  326.  
  327. /*----------------------------------------------------------------------
  328. Polygon.  A polygon is defined by a set of vertices.  With these databases,
  329.     a polygon is defined to have all points coplanar.  A polygon has only
  330.     one side, with the order of the vertices being counterclockwise as you
  331.     face the polygon (right-handed coordinate system).  The first two edges
  332.     must form a non-zero convex angle, so that the normal and side visibility
  333.     can be determined.  Description:
  334.  
  335.     "p" total_vertices
  336.     vert1.x vert1.y vert1.z
  337.     [etc. for total_vertices vertices]
  338.  
  339. Format:
  340.     p %d
  341.     [ %g %g %g ] <-- for total_vertices vertices
  342. ----------------------------------------------------------------------
  343. Polygonal patch.  A patch is defined by a set of vertices and their normals.
  344.     With these databases, a patch is defined to have all points coplanar.
  345.     A patch has only one side, with the order of the vertices being
  346.     counterclockwise as you face the patch (right-handed coordinate system).
  347.     The first two edges must form a non-zero convex angle, so that the normal
  348.     and side visibility can be determined.  Description:
  349.  
  350.     "pp" total_vertices
  351.     vert1.x vert1.y vert1.z norm1.x norm1.y norm1.z
  352.     [etc. for total_vertices vertices]
  353.  
  354. Format:
  355.     pp %d
  356.     [ %g %g %g %g %g %g ] <-- for total_vertices vertices
  357. ----------------------------------------------------------------------*/
  358. static void
  359. do_poly(fp)
  360. FILE *fp;
  361. {
  362.     int    ispatch;
  363.     int    nverts;
  364.     int    vertcount;
  365.     COORD3 *verts;
  366.     COORD3 *norms;
  367.     float    x, y, z;
  368.  
  369.     ispatch = getc(fp);
  370.     if (ispatch != 'p') {
  371.         ungetc(ispatch, fp);
  372.         ispatch = 0;
  373.     }
  374.  
  375.     if (fscanf(fp, "%d", &nverts) != 1)
  376.         goto fmterr;
  377.  
  378.     verts = (COORD3*)malloc(nverts*sizeof(COORD3));
  379.     if (verts == NULL)
  380.         goto memerr;
  381.  
  382.     if (ispatch) {
  383.         norms = (COORD3*)malloc(nverts*sizeof(COORD3));
  384.         if (norms == NULL)
  385.             goto memerr;
  386.     }
  387.  
  388.     /* read all the vertices into temp array */
  389.     for (vertcount = 0; vertcount < nverts; vertcount++) {
  390.         if (fscanf(fp, " %f %f %f", &x, &y, &z) != 3)
  391.             goto fmterr;
  392.         SET_COORD3(verts[vertcount],x,y,z);
  393.  
  394.         if (ispatch) {
  395.             if (fscanf(fp, " %f %f %f", &x, &y, &z) != 3)
  396.                 goto fmterr;
  397.             SET_COORD3(norms[vertcount],x,y,z);
  398.         }
  399.     }
  400.  
  401.     /* write output */
  402.     if (ispatch)
  403.         lib_output_polypatch(nverts, verts, norms);
  404.     else
  405.         lib_output_polygon(nverts, verts);
  406.  
  407.     free(verts);
  408.     if (ispatch)
  409.         free(norms);
  410.  
  411.     return;
  412. fmterr:
  413.     show_error("polygon or patch syntax error");
  414.     exit(1);
  415. memerr:
  416.     show_error("can't allocate memory for polygon or patch");
  417.     exit(1);
  418. }
  419.  
  420.  
  421. /*----------------------------------------------------------------------
  422. ----------------------------------------------------------------------*/
  423. static void
  424. parse_nff(fp)
  425. FILE *fp;
  426. {
  427.     int        c;
  428.  
  429.     while ( (c = getc(fp)) != EOF )
  430.         switch (c) {
  431.         case ' ':            /* white space */
  432.         case '\t':
  433.         case '\n':
  434.         case '\f':
  435.         case '\r':
  436.             continue;
  437.         case '#':            /* comment */
  438.             do_comment(fp);
  439.             break;
  440.         case 'v':            /* view point */
  441.             do_view(fp);
  442.             break;
  443.         case 'l':            /* light source */
  444.             do_light(fp);
  445.             break;
  446.         case 'b':            /* background color */
  447.             do_background(fp);
  448.             break;
  449.         case 'f':            /* fill material */
  450.             do_fill(fp);
  451.             break;
  452.         case 'c':            /* cylinder or cone */
  453.             do_cone(fp);
  454.             break;
  455.         case 's':            /* sphere */
  456.             do_sphere(fp);
  457.             break;
  458.         case 'p':            /* polygon or patch */
  459.             do_poly(fp);
  460.             break;
  461.         default:            /* unknown */
  462.             show_error("unknown NFF primitive code");
  463.             exit(1);
  464.         }
  465. } /* parse_nff */
  466.  
  467.  
  468. /*----------------------------------------------------------------------
  469. ----------------------------------------------------------------------*/
  470. int
  471. main(argc,argv)
  472.     int argc ;
  473.     char *argv[] ;
  474. {
  475.     char file_name[64];
  476.     FILE *fp;
  477.  
  478.     PLATFORM_INIT(SPD_READNFF);
  479.  
  480.     /* Start by defining which raytracer we will be using */
  481.     if ( lib_read_get_opts( argc, argv,
  482.             &raytracer_format, &output_format, file_name ) ) {
  483.     return EXIT_FAIL;
  484.     }
  485.  
  486.     if ( lib_open( raytracer_format, "ReadNFF.out" ) ) {
  487.     return EXIT_FAIL;
  488.     }
  489.  
  490.     fp = fopen(file_name, "r");
  491.     if (fp == NULL) {
  492.     fprintf(stderr, "Cannot open nff file: '%s'\n", file_name);
  493.     return EXIT_FAIL;
  494.     }
  495.  
  496.     lib_set_polygonalization(3, 3);
  497.  
  498.     parse_nff(fp);
  499.  
  500.     fclose(fp);
  501.  
  502.     lib_close();
  503.  
  504.     PLATFORM_SHUTDOWN();
  505.     return EXIT_SUCCESS;
  506. }
  507.