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

  1. /*
  2.  * lib.c - a library of deferred object output routines.
  3.  *
  4.  * Author:  Eric Haines, 3D/Eye, Inc.
  5.  *
  6.  */
  7.  
  8. /*-----------------------------------------------------------------*/
  9. /* include section */
  10. /*-----------------------------------------------------------------*/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <string.h>
  16.  
  17. #include "lib.h"
  18. #include "drv.h"
  19.  
  20.  
  21. /*-----------------------------------------------------------------*/
  22. /* defines/constants section */
  23. /*-----------------------------------------------------------------*/
  24.  
  25.  
  26. /*-----------------------------------------------------------------*/
  27. void
  28. dump_plg_file PARAMS((void))
  29. {
  30.     object_ptr temp_obj;
  31.     int i;
  32.     unsigned int fcnt, vcnt;
  33.  
  34.     fcnt = 0;
  35.     vcnt = 0;
  36.     for (temp_obj = gPolygon_stack;
  37.      temp_obj != NULL;
  38.      temp_obj = temp_obj->next_object) {
  39.     fcnt++;
  40.     vcnt += temp_obj->object_data.polygon.tot_vert;
  41.     }
  42.  
  43.     fprintf(gOutfile, "objx %d %d\n", vcnt, fcnt);
  44.  
  45.     /* Dump all vertices */
  46.     for (temp_obj = gPolygon_stack;
  47.      temp_obj != NULL;
  48.      temp_obj = temp_obj->next_object) {
  49.  
  50.     PLATFORM_MULTITASK();
  51.     for (i=0;i<temp_obj->object_data.polygon.tot_vert;i++) {
  52.         fprintf(gOutfile, "%g %g %g\n",
  53.            temp_obj->object_data.polygon.vert[i][X],
  54.            temp_obj->object_data.polygon.vert[i][Y],
  55.            temp_obj->object_data.polygon.vert[i][Z]);
  56.     }
  57.     }
  58.  
  59.     /* Dump all faces */
  60.     vcnt = 0;
  61.     for (temp_obj = gPolygon_stack;
  62.      temp_obj != NULL;
  63.      temp_obj = temp_obj->next_object) {
  64.  
  65.     PLATFORM_MULTITASK();
  66.     fprintf(gOutfile, "0x11ff %d ", temp_obj->object_data.polygon.tot_vert);
  67.     for (i=0;i<temp_obj->object_data.polygon.tot_vert;i++)
  68.         fprintf(gOutfile, "%d ", vcnt + i);
  69.     fprintf(gOutfile, "\n");
  70.     vcnt += i;
  71.     }
  72. }
  73.  
  74. /*-----------------------------------------------------------------*/
  75. void
  76. dump_obj_file PARAMS((void))
  77. {
  78.     object_ptr temp_obj;
  79.     int i;
  80.     unsigned int vcnt;
  81.  
  82.     /* Dump all vertices */
  83.     for (temp_obj = gPolygon_stack;
  84.      temp_obj != NULL;
  85.      temp_obj = temp_obj->next_object) {
  86.  
  87.     PLATFORM_MULTITASK();
  88.     for (i=0;i<temp_obj->object_data.polygon.tot_vert;i++) {
  89.         fprintf(gOutfile, "v %g %g %g\n",
  90.            temp_obj->object_data.polygon.vert[i][X],
  91.            temp_obj->object_data.polygon.vert[i][Y],
  92.            temp_obj->object_data.polygon.vert[i][Z]);
  93.     }
  94.     }
  95.  
  96.     /* Dump all faces */
  97.     vcnt = 0;
  98.     for (temp_obj = gPolygon_stack;
  99.      temp_obj != NULL;
  100.      temp_obj = temp_obj->next_object) {
  101.  
  102.     PLATFORM_MULTITASK();
  103.     fprintf(gOutfile, "f ", temp_obj->object_data.polygon.tot_vert);
  104.     for (i=0;i<temp_obj->object_data.polygon.tot_vert;i++) {
  105.         fprintf(gOutfile, "%d", vcnt + i + 1);
  106.         if (i < temp_obj->object_data.polygon.tot_vert - 1)
  107.            fprintf(gOutfile, " ");
  108.         }
  109.     fprintf(gOutfile, "\n");
  110.     vcnt += i;
  111.     }
  112. }
  113.  
  114. /*-----------------------------------------------------------------*/
  115. void
  116. dump_all_objects PARAMS((void))
  117. {
  118.     object_ptr temp_obj;
  119.  
  120.     if (gRT_out_format == OUTPUT_RTRACE)
  121.     fprintf(gOutfile, "Objects\n");
  122.  
  123.     /* Step through all objects dumping them as we go. */
  124.     for (temp_obj = gLib_objects, gObject_count = 0;
  125.      temp_obj != NULL;
  126.      temp_obj = temp_obj->next_object, gObject_count++) {
  127.  
  128.     PLATFORM_MULTITASK();
  129.     lookup_surface_stats(temp_obj->surf_index, &gTexture_count,
  130.                  &gTexture_ior);
  131.     if (temp_obj->tx != NULL) {
  132.        /* Set the active transform to what it was at the time
  133.           the object was created */
  134.        lib_tx_push();
  135.        lib_set_current_tx(*temp_obj->tx);
  136.        }
  137.     switch (temp_obj->object_type) {
  138.         case BOX_OBJ:
  139.         lib_output_box(temp_obj->object_data.box.point1,
  140.                    temp_obj->object_data.box.point2);
  141.         break;
  142.         case CONE_OBJ:
  143.         lib_output_cylcone(temp_obj->object_data.cone.base_pt,
  144.                    temp_obj->object_data.cone.apex_pt,
  145.                    temp_obj->curve_format);
  146.         break;
  147.         case DISC_OBJ:
  148.         lib_output_disc(temp_obj->object_data.disc.center,
  149.                 temp_obj->object_data.disc.normal,
  150.                 temp_obj->object_data.disc.iradius,
  151.                 temp_obj->object_data.disc.oradius,
  152.                 temp_obj->curve_format);
  153.         break;
  154.         case HEIGHT_OBJ:
  155.         lib_output_height(temp_obj->object_data.height.filename,
  156.                   temp_obj->object_data.height.data,
  157.                   temp_obj->object_data.height.height,
  158.                   temp_obj->object_data.height.width,
  159.                   temp_obj->object_data.height.x0,
  160.                   temp_obj->object_data.height.x1,
  161.                   temp_obj->object_data.height.y0,
  162.                   temp_obj->object_data.height.y1,
  163.                   temp_obj->object_data.height.z0,
  164.                   temp_obj->object_data.height.z1);
  165.         break;
  166.         case POLYGON_OBJ:
  167.         lib_output_polygon(temp_obj->object_data.polygon.tot_vert,
  168.                    temp_obj->object_data.polygon.vert);
  169.         break;
  170.         case POLYPATCH_OBJ:
  171.         lib_output_polypatch(temp_obj->object_data.polypatch.tot_vert,
  172.                      temp_obj->object_data.polypatch.vert,
  173.                      temp_obj->object_data.polypatch.norm);
  174.         break;
  175.         case SPHERE_OBJ:
  176.         lib_output_sphere(temp_obj->object_data.sphere.center_pt,
  177.                   temp_obj->curve_format);
  178.         break;
  179.         case SUPERQ_OBJ:
  180.         lib_output_sq_sphere(temp_obj->object_data.superq.center_pt,
  181.                      temp_obj->object_data.superq.a1,
  182.                      temp_obj->object_data.superq.a2,
  183.                      temp_obj->object_data.superq.a3,
  184.                      temp_obj->object_data.superq.n,
  185.                      temp_obj->object_data.superq.e);
  186.         break;
  187.         case TORUS_OBJ:
  188.         lib_output_torus(temp_obj->object_data.torus.center,
  189.                  temp_obj->object_data.torus.normal,
  190.                  temp_obj->object_data.torus.iradius,
  191.                  temp_obj->object_data.torus.oradius,
  192.                  temp_obj->curve_format);
  193.         break;
  194.         default:
  195.         fprintf(gOutfile, "Bad object type: %d\n",
  196.               temp_obj->object_type);
  197.         exit(1);
  198.     }
  199.     if (temp_obj->tx != NULL) {
  200.        /* Reset the active transform */
  201.        lib_tx_pop();
  202.     }
  203.     }
  204.  
  205.     if (gRT_out_format == OUTPUT_RTRACE)
  206.     fprintf(gOutfile, "\n");
  207. }
  208.  
  209. /*-----------------------------------------------------------------*/
  210. void
  211. dump_reorder_surfaces PARAMS((void))
  212. {
  213.     surface_ptr temp_ptr, head_ptr = NULL;
  214.  
  215.     while (gLib_surfaces != NULL) {
  216.     temp_ptr = gLib_surfaces;
  217.     gLib_surfaces = gLib_surfaces->next;
  218.     temp_ptr->next = head_ptr;
  219.     head_ptr = temp_ptr;
  220.     }
  221.  
  222.     gLib_surfaces = head_ptr;
  223. }
  224.  
  225. /*-----------------------------------------------------------------*/
  226. void
  227. dump_all_lights PARAMS((void))
  228. {
  229.     light_ptr temp_ptr = gLib_lights;
  230.  
  231.     if (gRT_out_format == OUTPUT_RTRACE)
  232.     fprintf(gOutfile, "Lights\n");
  233.  
  234.     while (temp_ptr != NULL) {
  235.     lib_output_light(temp_ptr->center_pt);
  236.     temp_ptr = temp_ptr->next;
  237.     }
  238.  
  239.     if (gRT_out_format == OUTPUT_RTRACE)
  240.     fprintf(gOutfile, "\n");
  241. }
  242.  
  243. /*-----------------------------------------------------------------*/
  244. void
  245. dump_all_surfaces PARAMS((void))
  246. {
  247.     surface_ptr temp_ptr = gLib_surfaces;
  248.  
  249.     if (gRT_out_format == OUTPUT_RTRACE)
  250.     fprintf(gOutfile, "Surfaces\n");
  251.  
  252.     while (temp_ptr != NULL) {
  253.     lib_output_color(temp_ptr->surf_name, temp_ptr->color, temp_ptr->ka,
  254.                   temp_ptr->kd, temp_ptr->ks, temp_ptr->shine,
  255.                   temp_ptr->ang, temp_ptr->kt, temp_ptr->ior);
  256.     temp_ptr = temp_ptr->next;
  257.     }
  258.  
  259.     if (gRT_out_format == OUTPUT_RTRACE)
  260.     fprintf(gOutfile, "\n");
  261. }
  262.  
  263.