home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / nff2art1.0.c < prev    next >
C/C++ Source or Header  |  1990-10-15  |  8KB  |  411 lines

  1. /*
  2.  * Convert NFF files to art input files
  3.  * (A small problem - art wants the background keyword BEFORE
  4.  * the lights - but some NFF files have them after the lights).
  5.  * 
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. /*
  12.  * List of lights so we can output them AFTER background...
  13.  */
  14. typedef struct l {
  15.     float    x, y, z;
  16.     struct l    *nxt;
  17. } Light;
  18.  
  19.  
  20. /*
  21.  * The current colour
  22.  */
  23. float    R, G, B;
  24. float    aR, aG, aB;
  25. float    bR, bG, bB;
  26.  
  27. /*
  28.  * Material properties
  29.  */
  30. float    Kd, Ks, Ksexp, Ir;
  31. float    Refl = 0.0, Trans;
  32.  
  33. /*
  34.  * A global  scale factor
  35.  */
  36. float    s = 1.0;
  37.  
  38. int    done_background = 0;
  39.  
  40. char    buf[128];
  41.  
  42. main(argc, argv)
  43.     int    argc;
  44.     char    **argv;
  45. {
  46.     int    c;
  47.     float    x, y, z;
  48.     Light    *l, *lights = (Light *)NULL;
  49.  
  50.     if (argc == 2)    /* Then it should be a global scale factor */
  51.         s = atof(argv[1]);
  52.  
  53.     printf("maxhitlevel 3\n");
  54.  
  55.     while ((c = getchar()) != EOF) {
  56.         switch (c) {
  57.         case '\t':
  58.             break;
  59.  
  60.         case 'v':
  61.             view();
  62.             break;
  63.  
  64.         case 'l':    /* Lights */
  65.             read_three("%g %g %g\n", &x, &y, &z);
  66.             x *= s;
  67.             y *= s;
  68.             z *= s;
  69.  
  70.             if (done_background) {
  71.                 printf("light {\n");
  72.                 printf("\tcolor 0.5, 0.5, 0.5\n");
  73.                 printf("\tlocation (%f, %f, %f)\n", x, y, z);
  74.                 printf("}\n\n");
  75.             } else {
  76.                 l = (Light *)malloc(sizeof(Light));
  77.                 l->x = x;
  78.                 l->y = y;
  79.                 l->z = z;
  80.                 l->nxt = lights;
  81.                 lights = l;
  82.             }
  83.  
  84.             break;
  85.  
  86.         case 'b':    /* Background color */
  87.             read_three("%g %g %g\n", &bR, &bG, &bB);
  88.             printf("background %g, %g, %g\n\n", bR, bG, bB);
  89.  
  90.             if (!done_background) {
  91.                 /*
  92.                  * Output the lights now....
  93.                  */
  94.                 for (l = lights; l != (Light *)NULL; l = l->nxt) {
  95.                     printf("light {\n");
  96.                     printf("\tcolor 0.5, 0.5, 0.5\n");
  97.                     printf("\tlocation (%f, %f, %f)\n", x, y, z);
  98.                     printf("}\n\n");
  99.                 }
  100.                 done_background = 1;
  101.             }
  102.  
  103.             break;
  104.  
  105.         case 'f':    /* Fill color and shading stuff */
  106.             if (scanf("%g %g %g %g %g %g %g %g\n", 
  107.                 &R, &G, &B, 
  108.                 &Kd, &Ks, &Ksexp,
  109.                 &Trans, &Ir
  110.             ) != 8)
  111.                 thunk("what happened");
  112.  
  113.             /*
  114.              * Give us some ambient...
  115.              */
  116.             aR = 0.05 * R;
  117.             aG = 0.05 * G;
  118.             aB = 0.05 * B;
  119.  
  120.             /*
  121.              * Set Refl to min(Ks, 1 - T) as Craig Kolb does
  122.              * in his nff2rayshade awk script.
  123.              */
  124.             Refl = Ks;
  125.             if (1.0 - Trans < Ks)
  126.                 Refl = 1.0 - Trans;
  127.  
  128.             break;
  129.         case 'c':    /* Cylinder or Cone */
  130.             cone();
  131.             break;
  132.  
  133.         case 's':    /* Sphere */
  134.             sphere();
  135.             break;
  136.  
  137.         case 'p':    /* Polygon or polygonal patch */
  138.             c = getchar();
  139.             if (c == 'p')
  140.                 polypatch();
  141.             else
  142.                 poly();
  143.  
  144.             break;
  145.  
  146.         case '#':    /* Comment */
  147.             printf("/* ");
  148.             while ((c = getchar()) != '\n' && c != EOF)
  149.                 putchar(c);
  150.  
  151.             printf(" */\n");
  152.  
  153.                 if (c == EOF)
  154.                     exit(0);
  155.  
  156.             break;
  157.  
  158.         default:
  159.             sprintf(buf, "Unknown  key character '%c'.", c);
  160.             thunk(buf);
  161.         }
  162.     }
  163. }
  164.  
  165. /*
  166.  * view
  167.  *
  168.  *    Read in and write the viewing parameters
  169.  */
  170. view()
  171. {
  172.     float    Fx, Fy, Fz;
  173.     float    Ax, Ay, Az;
  174.     float    Ux, Uy, Uz;
  175.     float    angle;
  176.     float    hither, yon;
  177.     int    xres, yres;
  178.  
  179.     read_three("\nfrom %g %g %g", &Fx, &Fy, &Fz);
  180.     read_three("\nat %g %g %g", &Ax, &Ay, &Az);
  181.     read_three("\nup %g %g %g", &Ux, &Uy, &Uz);
  182.  
  183.     if (scanf("\nangle %g", &angle) != 1)
  184.         thunk("wanted a number for fov");
  185.  
  186.     if (scanf("\nhither %g", &hither) != 1)
  187.         thunk("wanted a number for hither");
  188.  
  189.     if (scanf("\nresolution %d %d\n", &xres, &yres) != 2)
  190.         thunk("wanted two numbers for resolution");
  191.  
  192.     printf("up (%g, %g, %g)\n", Ux, Uy, Uz);
  193.  
  194.     Fx *= s;
  195.     Fy *= s;
  196.     Fz *= s;
  197.  
  198.     Ax *= s;
  199.     Ay *= s;
  200.     Az *= s;
  201.  
  202.     printf("lookat (%g, %g, %g, %g, %g, %g, 0.0)\n",
  203.         Fx, Fy, Fz, Ax, Ay, Az);
  204.  
  205.     printf("fieldofview %g\n\n", angle);
  206.  
  207. }
  208.  
  209. /*
  210.  * sphere
  211.  *
  212.  *    Read and write a sphere
  213.  */
  214. sphere()
  215. {
  216.     float    x, y, z, r;
  217.  
  218.     read_four("%g %g %g %g\n", &x, &y, &z, &r);
  219.  
  220.     x *= s;
  221.     y *= s;
  222.     z *= s;
  223.     r *= s;
  224.     
  225.     printf("sphere {\n");
  226.     printf("\tcenter (%f, %f, %f)\n", x, y, z);
  227.     printf("\tradius  %f\n", r);
  228.     printf("\tmaterial %f, %f, %f, %f\n", Ir, Kd, Ks, Ksexp);
  229.     if (Refl != 0.0)
  230.         printf("\treflectance %f\n", Refl);
  231.     if (Trans != 0.0)
  232.         printf("\ttransparency %f\n", Trans);
  233.     printf("\tcolor %f, %f, %f\n", R, G, B);
  234.     printf("\tambient %f, %f, %f\n", aR, aG, aB);
  235.     printf("}\n\n");
  236. }
  237.  
  238. /*
  239.  * cone
  240.  *
  241.  *    Read in and write a cone or a cylinder
  242.  *
  243.  */
  244. cone()
  245. {
  246.     float    apex_x, apex_y, apex_z, apex_r;
  247.     float    base_x, base_y, base_z, base_r;
  248.  
  249.     read_four("%g %g %g %g\n", &base_x, &base_y, &base_z, &base_r);
  250.     read_four("%g %g %g %g\n", &apex_x, &apex_y, &apex_z, &apex_r);
  251.  
  252.     base_x *= s;
  253.     base_y *= s;
  254.     base_z *= s;
  255.     base_r *= s;
  256.  
  257.     apex_x *= s;
  258.     apex_y *= s;
  259.     apex_z *= s;
  260.     apex_r *= s;
  261.  
  262.     if (fabs(apex_r - base_r) < 1.0e-10) {
  263.         printf("cylinder {\n");
  264.         printf("\tcenter (%f, %f, %f)\n", base_x, base_y, base_z);
  265.         printf("\tcenter (%f, %f, %f)\n", apex_x, apex_y, apex_z);
  266.         printf("\tradius %f\n", base_r);
  267.     } else {
  268.         printf("cone {\n");
  269.         printf("\tcenter (%f, %f, %f)\n", base_x, base_y, base_z);
  270.         printf("\tradius %f\n", base_r);
  271.         printf("\tcenter (%f, %f, %f)\n", apex_x, apex_y, apex_z);
  272.         printf("\tradius %f\n", apex_r);
  273.     }
  274.  
  275.     printf("\tmaterial %f, %f, %f, %f\n", Ir, Kd, Ks, Ksexp);
  276.     if (Refl != 0.0)
  277.         printf("\treflectance %f\n", Refl);
  278.     if (Trans != 0.0)
  279.         printf("\ttransparency %f\n", Trans);
  280.     printf("\tcolor %f, %f, %f\n", R, G, B);
  281.     printf("\tambient %f, %f, %f\n", aR, aG, aB);
  282.     printf("}\n\n");
  283. }
  284.  
  285. /*
  286.  * poly
  287.  *
  288.  *     Read in and write a polygon.
  289.  */
  290. poly()
  291. {
  292.     float    x, y, z;
  293.     int    i, nv;
  294.  
  295.     if (scanf("%d\n", &nv) != 1)
  296.         thunk("wanted an integer for number of verticies");
  297.  
  298.     if (nv <= 0)
  299.         thunk("p: silly value for number of verticies");
  300.     
  301.     printf("polygon {\n");
  302.  
  303.     for (i = 0; i < nv; i++) {
  304.         read_three("%g %g %g\n", &x, &y, &z);
  305.         x *= s;
  306.         y *= s;
  307.         z *= s;
  308.         printf("\tvertex (%f, %f, %f)\n", x, y, z);
  309.     }
  310.  
  311.     printf("\tmaterial %f, %f, %f, %f\n", Ir, Kd, Ks, Ksexp);
  312.     if (Refl != 0.0)
  313.         printf("\treflectance %f\n", Refl);
  314.     if (Trans != 0.0)
  315.         printf("\ttransparency %f\n", Trans);
  316.     printf("\tcolor %f, %f, %f\n", R, G, B);
  317.     printf("\tambient %f, %f, %f\n", aR, aG, aB);
  318.     printf("}\n\n");
  319. }
  320.  
  321. /*
  322.  * polypatch
  323.  *
  324.  *    Read in and write a "poly patch" thingo (ie a polygon with
  325.  *    vertex normals)
  326.  */
  327. polypatch()
  328. {
  329.     float    x, y, z, nx, ny, nz;
  330.     int    i, nv;
  331.  
  332.     if (scanf("%d\n", &nv) != 1)
  333.         thunk("wanted an integer for number of verticies");
  334.  
  335.     if (nv <= 0)
  336.         thunk("p: silly value for number of verticies");
  337.     
  338.     printf("polygon {\n");
  339.  
  340.     for (i = 0; i < nv; i++) {
  341.         read_six("%g %g %g %g %g %g\n", &x, &y, &z, &nx, &ny, &nz);
  342.         x *= s;
  343.         y *= s;
  344.         z *= s;
  345.         printf("\tvertex (%f, %f, %f), (%f, %f, %f)\n", x, y, z, nx, ny, nz);
  346.     }
  347.  
  348.     printf("\tmaterial %f, %f, %f, %f\n", Ir, Kd, Ks, Ksexp);
  349.     if (Refl != 0.0)
  350.         printf("\treflectance %f\n", Refl);
  351.     if (Trans != 0.0)
  352.         printf("\ttransparency %f\n", Trans);
  353.     printf("\tcolor %f, %f, %f\n", R, G, B);
  354.     printf("\tambient %f, %f, %f\n", aR, aG, aB);
  355.     printf("}\n\n");
  356. }
  357.  
  358. /*
  359.  * thunk
  360.  *
  361.  *    Go thunk! (die)
  362.  */
  363. thunk(s)
  364.     char    *s;
  365. {
  366.     fprintf(stderr, "%s\n", s);
  367.     exit(1);
  368. }
  369.  
  370. /*
  371.  * We seem to be reading lots of sets of 3 or 4 or 6 numbers....
  372.  */
  373. read_three(f, a, b, c)
  374.     char    *f;
  375.     float    *a, *b, *c;
  376. {
  377.     int    n;
  378.  
  379.     if ((n = scanf(f, a, b, c)) != 3) {
  380.         sprintf(buf, "expected to read 3 numbers with '%s' format but got %d\n", n);
  381.         thunk(buf);
  382.     }
  383.  
  384. }
  385.  
  386. read_four(f, a, b, c, d)
  387.     char    *f;
  388.     float    *a, *b, *c, *d;
  389. {
  390.     int    n;
  391.  
  392.     if ((n = scanf(f, a, b, c, d)) != 4) {
  393.         sprintf(buf, "expected to read 4 numbers with '%s' format but got %d\n", f, n);
  394.         thunk(buf);
  395.     }
  396.  
  397. }
  398.  
  399. read_six(f, a, b, c, d, e, g)
  400.     char    *f;
  401.     float    *a, *b, *c, *d, *e, *g;
  402. {
  403.     int    n;
  404.  
  405.     if ((n = scanf(f, a, b, c, d, e, g)) != 6) {
  406.         sprintf(buf, "expected to read 6 numbers with '%s' format but got %d\n", f, n);
  407.         thunk(buf);
  408.     }
  409.  
  410. }
  411.