home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / nff2art2.0.c < prev    next >
C/C++ Source or Header  |  1993-07-23  |  6KB  |  353 lines

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