home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / JIREND / PLG.C < prev    next >
C/C++ Source or Header  |  1993-04-11  |  6KB  |  267 lines

  1. /* PLG file i/o */
  2.   
  3. /* Written by Bernie Roehl, March 1992 */
  4.   
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <alloc.h>
  9. #include "rend386.h"
  10. #include "plg.h"
  11. #include "eprint.h"
  12.   
  13. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  14.    May be freely used to write software for release into the public domain;
  15.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  16.    for permission to incorporate any part of this software into their
  17.    products!
  18.  *
  19.  * modified to include eprint stuff 11/20/92: Jerry Isdale
  20.  */
  21.   
  22. int load_err = 0; /* set if an error was encountered during loading */
  23.   
  24. static long xshift = 0, yshift = 0, zshift = 0;
  25. static float xrescale = 1, yrescale = 1, zrescale = 1;
  26. static int depthsort = 0;
  27.   
  28. static unsigned *cmap;
  29. static int mapsize = 0;
  30.   
  31. long lx, ly, lz; /* DEBUG */
  32.   
  33. static unsigned mapcolor(unsigned orig)
  34. {
  35.    if ((orig & 0x8000) == 0) return orig; /* only map if top bit set */
  36.    orig &= 0x7FFF; /* turn the top bit off */
  37.    if (cmap == NULL) return orig; /* no map */
  38.    if (orig > mapsize) return orig; /* color not in map */
  39.    return cmap[orig]; /* map it */
  40. }
  41.   
  42. void set_loadplg_colormap(unsigned *map, int msize)
  43. {
  44.    cmap = map;
  45.    mapsize = msize;
  46. }
  47.   
  48. void set_loadplg_offset(long x, long y, long z)
  49. {
  50.    xshift = x;
  51.    yshift = y;
  52.    zshift = z;
  53. }
  54.   
  55. void set_loadplg_scale(float x, float y, float z)
  56. {
  57.    xrescale = x;
  58.    yrescale = y;
  59.    zrescale = z;
  60. }
  61.   
  62. void set_loadplg_depthsort(int type)
  63. {
  64.    depthsort = type;
  65. }
  66.   
  67. void strip_comment(char *buff) /* also trim newline character(s) */
  68. {
  69.    char *p;
  70.    if ((p = strchr(buff, '\n')) != NULL) *p = '\0'; /* trim newline */
  71.    if ((p = strchr(buff, '#')) != NULL) *p = '\0'; /* trim comment */
  72.    if ((p = strchr(buff, '*')) != NULL) *p = '\0'; /* future expansion */
  73. }
  74.   
  75. static int load_in_plg_file(FILE *in, OBJECT **obj)
  76. {
  77.    char tbuff[1000], objname[100];
  78.    int nv, np, i;
  79.    char *p;
  80.    long size = 0;
  81.    int multi = (*obj) ? 1 : 0; /* multi-reps if not null object */
  82.   
  83.    /* skip blank lines */
  84.    do {
  85.       if (fgets(tbuff, sizeof(tbuff), in) == NULL) return -1;
  86.       if (strstr(tbuff,"#MULTI")) multi++;
  87.       strip_comment(tbuff);
  88.    }
  89.    while (tbuff[0] == '\0');
  90.   
  91.    if (sscanf(tbuff, "%s %d %d", objname, &nv, &np) != 3)
  92.    {
  93.       load_err = -1;
  94.       return -1;
  95.    }
  96.   
  97.    if (multi && (p = strchr(objname, '_')) != NULL)
  98.    {
  99.       *p++ = 0;
  100.       size = atol(p);
  101.    }
  102.    else size = 0; /* default: use largest detail always */
  103.   
  104.    if (*obj == NULL)
  105.    {
  106.       if ((*obj = new_obj(0, nv, np)) == NULL)
  107.       {
  108.          load_err = -2;
  109.          return -1;
  110.       }
  111.       set_rep_size(*obj, size);
  112.    }
  113.    else
  114.    {
  115.       if (add_representation(*obj, size, nv, np) == NULL)
  116.       {
  117.          load_err = -2;
  118.          return -1;
  119.       }
  120.    }
  121.   
  122.    for (i = 0; i < nv; ++i) /* load in vertices */
  123.    {
  124.       float x, y, z;
  125.       /* skip blank lines */
  126.       do
  127.       {
  128.          if (fgets(tbuff, sizeof(tbuff), in) == NULL)
  129.          {
  130.             load_err = -4;
  131.             return -1;
  132.          }
  133.          strip_comment(tbuff);
  134.       }
  135.       while (tbuff[0] == '\0');
  136.   
  137.       if (sscanf(tbuff, "%f %f %f", &x, &y, &z) != 3)
  138.       {
  139.          load_err = -5;
  140.          return -1;
  141.       }
  142.   
  143.       add_vertex(*obj, (lx = (long) (x*xrescale))+xshift,
  144.       (ly = (long) (y*yrescale))+yshift,
  145.       (lz = (long) (z*zrescale))+zshift);
  146.       lx++;
  147.    }
  148.   
  149.    for (i = 0; i < np; ++i) /* load polygons */
  150.    {
  151.       int j, npoints;
  152.       unsigned color;
  153.       POLY *poly;
  154.       char *p;
  155.       /* skip blank lines */
  156.       do
  157.       {
  158.          if (fgets(tbuff, sizeof(tbuff), in) == NULL)
  159.          {
  160.             load_err = -6;
  161.             return -1;
  162.          }
  163.          strip_comment(tbuff);
  164.       }
  165.       while (tbuff[0] == '\0');
  166.   
  167.       color = (unsigned) strtoul(tbuff,&p,0) ; /* req so hex colors usable */
  168.       color = mapcolor(color);
  169.       if ((p = strtok(p, " \t")) == NULL)
  170.       {
  171.          load_err = -8;
  172.          return -1;
  173.       }
  174.       npoints = atoi(p);
  175.       if ((poly = add_poly(*obj, color, npoints)) == NULL)
  176.       {
  177.          load_err = -9;
  178.          return -1;
  179.       }
  180.       for (j = 0; j < npoints; ++j)
  181.       {
  182.          if ((p = strtok(NULL, " \t")) == NULL)
  183.          {
  184.             load_err = -9;
  185.             return -1;
  186.          }
  187.          add_point(*obj, poly, atoi(p));
  188.       }
  189.    }
  190.   
  191. // Dump_Object(*obj);
  192.   
  193.    compute_obj(*obj); /* will do current rep only */
  194.    load_err = 0;
  195.    return 0;
  196. }
  197.   
  198. OBJECT *load_plg(FILE *in)
  199. {
  200.    OBJECT *obj = NULL;
  201.    load_err = 0;
  202.    if (load_in_plg_file(in, &obj)) return NULL;
  203.    set_object_sorting(obj, depthsort);
  204.    return obj;
  205. }
  206.   
  207. OBJECT *load_multi_plg(FILE *in)
  208. {
  209.    OBJECT *obj = NULL;
  210.    load_err = 0;
  211.    while (load_in_plg_file(in, &obj) == 0)
  212.       ;
  213.    set_object_sorting(obj, depthsort);
  214.    return obj;
  215. }
  216.   
  217.   
  218. save_plg(OBJECT *obj, FILE *out)
  219. {
  220.    int nv, np, i;
  221.    char buff[100];
  222.   
  223.    get_obj_info(obj, &nv, &np);
  224.    fprintf(out, "SAVED %d %d\n", nv, np);
  225.    for (i = 0; i < nv; ++i)
  226.    {
  227.       long x, y, z;
  228.       get_vertex_world_info(obj, i, &x, &y, &z);
  229.       fprintf(out, "%ld %ld %ld\n", x, y, z);
  230.    }
  231.    for (i = 0; i < np; ++i)
  232.    {
  233.       int j, n, verts[1000];
  234.       unsigned c;
  235.       get_poly_info(obj, i, &c, &n, verts, sizeof(verts)/sizeof(int));
  236.       fprintf(out, "0x%04.4X %d", c & 0x7FFF, n);
  237.       for (j = 0; j < n; ++j) fprintf(out, " %d", verts[j]);
  238.       fprintf(out, "\n");
  239.    }
  240.    return 0;
  241. }
  242.   
  243. void Dump_Object(OBJECT *obj)
  244. {
  245.    int nv, np, i;
  246.    char buff[100];
  247.   
  248.    get_obj_info(obj, &nv, &np);
  249.    eprintf("SAVED %d %d\n", nv, np);
  250.    for (i = 0; i < nv; ++i)
  251.    {
  252.       long x, y, z;
  253.       get_vertex_world_info(obj, i, &x, &y, &z);
  254.       eprintf("%ld %ld %ld\n", x, y, z);
  255.    }
  256.    for (i = 0; i < np; ++i)
  257.    {
  258.       int j, n, verts[1000];
  259.       unsigned c;
  260.       get_poly_info(obj, i, &c, &n, verts, sizeof(verts)/sizeof(int));
  261.       eprintf("0x%04.4X %d", c & 0x7FFF, n);
  262.       for (j = 0; j < n; ++j) eprintf(" %d", verts[j]);
  263.       eprintf("\n");
  264.    }
  265. }
  266.   
  267.