home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / plgmerge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-13  |  7.0 KB  |  342 lines

  1. /*  PLG file processing package
  2. /*  by Dave Stampe 13/5/93
  3. /*
  4. /*  PLGMERGE.EXE
  5. /*
  6. /*  merges all PLGs in a file-- run PLGX to clean up
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <dos.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. int nlines;             /* total lines in PLG file */
  17. int nameline;           /* original file line numbers of PLG */
  18. int vtxline;            /* sections: so comments will be in  */
  19. int polyline;           /* the right places */
  20.  
  21. char fin[80] = "";      /* in, out filennames */
  22. char fout[80] = "";
  23.  
  24. FILE *in, *out;
  25.  
  26. char tvtxn[] = "tmpvtx.";
  27. char tplyn[] = "tmpplg.";
  28.  
  29. FILE *tvtx, *tply;
  30. int vtxlne, plylne;    /* lines in temp files */
  31.  
  32. char plgname[100];      /* PLG object name */
  33.  
  34. int nverts, npolys;     /* old, new vertex counts to/from processing */
  35.  
  36. int verts_in_file = 0;
  37. int verts_base = 0;
  38. int total_polys = 0;
  39. int total_vertices = 0;
  40.  
  41. char line[300];        /* line from PLG file */
  42. char oline[300];
  43.  
  44.  
  45. read_name()   /* reads PLG till name found */
  46. {
  47.  int i,j,k;
  48.  char *c;
  49.  
  50.  while(!feof(in))        /* read till name */
  51.   {
  52.    fgets(line, 300, in);
  53.  
  54.    if(feof(in)) return;    /* EOF before new object */
  55.  
  56.    if(sscanf(line, "%s %d %d", plgname, &nverts, &npolys)==3)
  57.     if(strchr(plgname,'#')==NULL)    /* skip comments */
  58.     {
  59.      fputs("# Merged From: ", tvtx);
  60.      fputs("# Merged From: ", tply);
  61.      fputs(line, tvtx);
  62.      fputs(line, tply);
  63.      vtxlne++;
  64.      plylne++;
  65.      break;
  66.     }
  67.    fputs(line, tvtx);
  68.    fputs(line, tply);
  69.    vtxlne++;
  70.    plylne++;
  71.   }
  72. }
  73.  
  74. read_vertices()    /* reads vertex secton of PLG object */
  75. {
  76.  int i,j,k;
  77.  char *c;
  78.  float x, y, z;
  79.  
  80.  verts_in_file = 0;
  81.  
  82.  while(nverts)
  83.   {
  84.    if(feof(in))
  85.     {
  86.      fprintf(stderr, "Early EOF in input file\n");
  87.      fcloseall();
  88.      exit(-1);
  89.     }
  90.  
  91.    fgets(line, 300, in);    /* delete comments */
  92.    j=sscanf(line, "%f %f %f", &x, &y, &z);    /* get vertex */
  93.    if(j==3)
  94.     {
  95.      nverts--;           /* valid vertex count */
  96.      verts_in_file++;
  97.      total_vertices++;
  98.     }
  99.    fputs(line, tvtx);
  100.    vtxlne++;
  101.   }
  102. }
  103.  
  104.  
  105. read_polygons()     /* reads polygons, renumbers verticess */
  106. {
  107.  int i,j,k;
  108.  int nverts;
  109.  char *c;
  110.  char color[200];
  111.  char *trcomment;
  112.  char outline[200];
  113.  char tc[20];
  114.  
  115.  for(i=0;i<npolys;i++)        /* read polygons */
  116.   {
  117.    while(1)      /* skip till valid poly line */
  118.     {
  119.      if(feof(in))
  120.       {
  121.        fprintf(stderr, "Early EOF in input file\n");
  122.        fcloseall();
  123.        exit(-1);
  124.       }
  125.  
  126.      trcomment = NULL;
  127.  
  128.      fgets(line, 300, in);
  129.      strcpy(oline,line);
  130.  
  131.      c=strchr(line,'#');    /* skip comments */
  132.      if(c)
  133.       {
  134.        trcomment = c;
  135.        *c = 0;
  136.       }
  137.  
  138.      c = strtok(line," \t");
  139.      if(c==NULL || c[0]==0 || c[0]=='\n')
  140.       {
  141.        fputs(oline,tply);
  142.        plylne++;
  143.       }
  144.      else break;
  145.     }
  146.  
  147.    strcpy(outline,c);
  148.  
  149.    c = strtok(NULL," \t");
  150.    nverts = atoi(c);
  151.    sprintf(outline+strlen(outline)," %d", nverts);
  152.    if(nverts<1)
  153.     {
  154.      fprintf(stderr, "Syntax error on line %d of input file\n", nlines);
  155.      fcloseall();
  156.      exit(-1);
  157.     }
  158.    if(nverts>20)    /* max. 20 vertices for REND386 */
  159.     {
  160.      fprintf(stderr, "Too many vertices in poly on line %d of input file\n", nlines);
  161.      fcloseall();
  162.      exit(-1);
  163.     }
  164.    for (j = 0; j < nverts; ++j)    /* read vertex indices */
  165.     {
  166.      c = strtok(NULL," \t");
  167.      k = atoi(c);
  168.      if(k<0)
  169.       {
  170.        fprintf(stderr, "Bad vertex number on line %d of input file\n", nlines);
  171.        fcloseall();
  172.        exit(-1);
  173.       }
  174.      sprintf(outline+strlen(outline)," %d", k + verts_base);
  175.     }
  176.    if(trcomment) sprintf(outline+strlen(outline), " #%s", trcomment);
  177.    else sprintf(outline+strlen(outline),"\n");
  178.  
  179.    fputs(outline,tply);
  180.    plylne++;
  181.    total_polys++;
  182.   }
  183.  return 1;
  184. }
  185.  
  186.  
  187.  
  188. write_output()     /* writes PLG file, using temps */
  189. {
  190.  int i,j,k, ln;
  191.  char lbuff[300];
  192.  
  193.  fprintf(out,"%s %d %d\n",     /* name line with comments */
  194.        plgname, total_vertices, total_polys);
  195.  for(i=0;i<vtxlne;i++)
  196.   {
  197.    fgets(line, 300, tvtx);
  198.    fputs(line, out);
  199.   }
  200.  for(i=0;i<plylne;i++)
  201.   {
  202.    fgets(line, 300, tply);
  203.    fputs(line, out);
  204.   }
  205. }
  206.  
  207.  
  208. syntax()
  209. {
  210.  fprintf(stderr,"PLGMERGE.EXE PLG file processor (c) 1993 by Dave STampe\n");
  211.  fprintf(stderr,"USE: PLGMERGE <infile> <outfile>\n");
  212.  fprintf(stderr,"  Merges all PLG objects in <infile> into one object.\n");
  213.  fprintf(stderr,"  Preserves comments. Will write output to <infile> if\n");
  214.  fprintf(stderr,"  <outfile is not supplied.  Will replace $ in <outfile>\n");
  215.  fprintf(stderr,"  with <infile> for use with batch files.\n");
  216.  fprintf(stderr,"  Run PLGX /I /V /N to clean up duplicate vertices later.\n");
  217. }
  218.  
  219.  
  220. int add_extension(char *fname, char *ext)
  221. {
  222.  int i,j;
  223.  
  224.  if(fname[0] == 0) return(-1);         /* error: no string */
  225.  for(i=0;i<70;i++)
  226.   {
  227.    if(fname[i] == '.') return(1);    /* already has valid extension */
  228.    if(fname[i] == 0)
  229.     {
  230.      fname[i] = '.';                 /* add default extension */
  231.      fname[i+1] = ext[0];
  232.      fname[i+2] = ext[1];
  233.      fname[i+3] = ext[2];
  234.      fname[i+4] = 0;
  235.      return(0);
  236.     }
  237.   }
  238.  return(-1);            /* error: string too long */
  239. }
  240.  
  241.  
  242. int obj = 0;
  243.  
  244. void main(int argc, char *argv[])
  245. {
  246.  int i,c;
  247.  
  248.  if(argc<2)
  249.   {
  250.    syntax();
  251.    exit(-1);
  252.   }
  253.  
  254.  for (i = 1; i < argc; i++)
  255.   {
  256.    if(fin[0]=='\0') strcpy(fin,argv[i]);      /* copy in, out filename */
  257.    else        strcpy(fout,argv[i]);
  258.   }
  259.  
  260.  if(fin[0]==0 && fout[0]==0) syntax();
  261.  
  262.  if(strchr(fout, '$'))    /* '$' in out name replaced by in name */
  263.   {
  264.    char t[100];
  265.    char *p = strchr(fout, '$');
  266.    *p++ = 0;
  267.    strcpy(t, fout);
  268.    strcat(t, fin);
  269.    strcat(t, p);
  270.    strcpy(fout,t);
  271.   }
  272.  
  273.  add_extension(fin, "plg");
  274.  if(!fout[0]) strcpy(fout, fin);
  275.  
  276.  if((in=fopen(fin,"r"))==NULL)
  277.   {
  278.    fprintf(stderr, "Can't open %s for input.\n", fin);
  279.    exit(-1);
  280.   }
  281.  
  282.  if((tvtx=fopen(tvtxn,"w"))==NULL)
  283.   {
  284.    fprintf(stderr, "Can't open %s for output.\n", tvtxn);
  285.    exit(-1);
  286.   }
  287.  vtxlne = 0;
  288.  
  289.  if((tply=fopen(tplyn,"w"))==NULL)
  290.   {
  291.    fprintf(stderr, "Can't open %s for output.\n", tplyn);
  292.    exit(-1);
  293.   }
  294.  plylne = 0;
  295.  
  296.  verts_base = 0;
  297.  total_polys = 0;
  298.  total_vertices = 0;
  299.  
  300.  while(!feof(in))
  301.   {
  302.    read_name();
  303.    if(feof(in)) break;
  304.    read_vertices();
  305.    fprintf(tvtx,"\n");
  306.    vtxlne++;
  307.    read_polygons();
  308.    fprintf(tply,"\n");
  309.    plylne++;
  310.    verts_base += verts_in_file;
  311.   }
  312.  
  313.  fclose(in);
  314.  fclose(tvtx);
  315.  fclose(tply);
  316.  
  317.  if((tvtx=fopen(tvtxn,"r"))==NULL)
  318.   {
  319.    fprintf(stderr, "Can't open %s for input.\n", tvtxn);
  320.    exit(-1);
  321.   }
  322.  
  323.  if((tply=fopen(tplyn,"r"))==NULL)
  324.   {
  325.    fprintf(stderr, "Can't open %s for input.\n", tplyn);
  326.    exit(-1);
  327.   }
  328.  
  329.  if(fout[0])
  330.   if((out=fopen(fout,"w"))==NULL)
  331.     {
  332.      fprintf(stderr, "Can't open %s for output.\n", fout);
  333.      exit(-1);
  334.     }
  335.  
  336.  
  337.  write_output();
  338.  fcloseall();
  339.  fprintf(stderr, "Sucessful completion.\n");
  340.  exit(0);
  341. }
  342.