home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff397.lzh / DKBTrace / DKBSource.LZH / Sculpt2dkb.c < prev    next >
C/C++ Source or Header  |  1990-08-26  |  8KB  |  231 lines

  1. /*****************************************************************************
  2. *
  3. *                                    Sculpt2dat.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This program reads a Sculpt-Animate 3D format scene file and generates an
  8. *  output file that may be read in to DKBTrace.
  9. *
  10. * This software is freely distributable. The source and/or object code may be
  11. * copied or uploaded to communications services so long as this notice remains
  12. * at the top of each file.  If any changes are made to the program, you must
  13. * clearly indicate in the documentation and in the programs startup message
  14. * who it was who made the changes. The documentation should also describe what
  15. * those changes were. This software may not be included in whole or in
  16. * part into any commercial package without the express written consent of the
  17. * author.  It may, however, be included in other public domain or freely
  18. * distributed software so long as the proper credit for the software is given.
  19. *
  20. * This software is provided as is without any guarantees or warranty. Although
  21. * the author has attempted to find and correct any bugs in the software, he
  22. * is not responsible for any damage caused by the use of the software.  The
  23. * author is under no obligation to provide service, corrections, or upgrades
  24. * to this package.
  25. *
  26. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  27. * about them.  Also, if you have any comments or questions, you may contact me
  28. * at the following address:
  29. *
  30. *     David Buck
  31. *     22C Sonnet Cres.
  32. *     Nepean Ontario
  33. *     Canada, K2H 8W7
  34. *
  35. *  I can also be reached on the following bulleton boards:
  36. *
  37. *     ATX              (613) 526-4141
  38. *     OMX              (613) 731-3419
  39. *     Mystic           (613) 731-0088 or (613) 731-6698
  40. *
  41. *  Fidonet:   1:163/109.9
  42. *  Internet:  David_Buck@Carleton.CA
  43. *
  44. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  45. *
  46. *     Lattice BBS                      (708) 916-1200
  47. *     The Information Exchange BBS     (708) 945-5575
  48. *     Stillwaters BBS                  (708) 403-2826
  49. *
  50. *****************************************************************************/
  51.  
  52.  
  53. #include <stdio.h>
  54. #include "frame.h"
  55. #include "dkbproto.h"
  56.  
  57. void read_iff_file(void);
  58.  
  59. IMAGE_COLOUR *iff_colour_map;
  60. int colourmap_size;
  61. FILE *f, *out;
  62.  
  63. static CHUNK_HEADER Chunk_Header;
  64.  
  65. DBL *vertices_x, *vertices_y, *vertices_z;
  66. DBL max_x, max_y, max_z, min_x, min_y, min_z;
  67. #define FABS(x) ((x<0.0)?-x:x)
  68.  
  69. #define FORM 0x464f524dL
  70. #define SC3D 0x53433344L
  71. #define VERT 0x56455254L
  72. #define FACE 0x46414345L
  73.  
  74. #define CMPNONE 0
  75.  
  76. char *Textures[7] = {"Dull", "Shiny", "Mirror", "Luminous", "Glass", "Metal", "Glass2"};
  77.  
  78. void iff_error ()
  79.    {
  80.    printf ("Invalid iff file\n");
  81.    exit(1);
  82.    }
  83.  
  84. int read_byte(f)
  85.    FILE *f;
  86.    {
  87.    int c;
  88.    if ((c = getc(f)) == EOF)
  89.       iff_error();
  90.    return (c);
  91.    }
  92.  
  93. int read_word(f)
  94.    FILE *f;
  95.    {
  96.    int result;
  97.  
  98.    result = read_byte(f)*256;
  99.    result += read_byte(f);
  100.    return (result);
  101.    }
  102.  
  103. long read_long(f)
  104.    FILE *f;
  105.    {
  106.    int i;
  107.    long result;
  108.  
  109.    result = 0;
  110.    for (i = 0 ; i < 4 ; i++)
  111.       result = result * 256 + read_byte(f);
  112.  
  113.    return (result);
  114.    }
  115.  
  116. void Read_Chunk_Header (f, dest)
  117.    FILE *f;
  118.    CHUNK_HEADER *dest;
  119.    {
  120.    dest->name = read_long(f);
  121.    dest->size = (int) read_long(f);
  122.    }
  123.  
  124. void read_iff_file()
  125.    {
  126.    int i;
  127.    int vert1, vert2, vert3;
  128.    unsigned int texture;
  129.    DBL red, green, blue;
  130.  
  131.    max_x = max_y = max_z = -10000000.0;
  132.    min_x = min_y = min_z = 10000000.0;
  133.  
  134.    while (1) {
  135.       Read_Chunk_Header(f, &Chunk_Header);
  136.       switch ((int) Chunk_Header.name) {
  137.          case FORM: if (read_long(f) != SC3D)
  138.                        iff_error();
  139.                     break;
  140.  
  141.          case VERT: vertices_x = (DBL *)
  142.                         malloc (Chunk_Header.size * (sizeof(DBL)/sizeof(long)));
  143.                     vertices_y = (DBL *)
  144.                         malloc (Chunk_Header.size * (sizeof(DBL)/sizeof(long)));
  145.                     vertices_z = (DBL *)
  146.                         malloc (Chunk_Header.size * (sizeof(DBL)/sizeof(long)));
  147.                     for (i = 0 ; i < Chunk_Header.size/12 ; i++) {
  148.                        vertices_x[i] = read_long(f) / 10000.0;
  149.                        if (vertices_x[i] < min_x)
  150.                           min_x = vertices_x[i];
  151.                        if (vertices_x[i] > max_x)
  152.                           max_x = vertices_x[i];
  153.  
  154.                        vertices_y[i] = read_long(f) / 10000.0;
  155.                        if (vertices_y[i] < min_y)
  156.                           min_y = vertices_y[i];
  157.                        if (vertices_y[i] > max_y)
  158.                           max_y = vertices_y[i];
  159.  
  160.                        vertices_z[i] = read_long(f) / 10000.0;
  161.                        if (vertices_z[i] < min_z)
  162.                           min_z = vertices_z[i];
  163.                        if (vertices_z[i] > max_z)
  164.                           max_z = vertices_z[i];
  165.                        }
  166.                     break;
  167.  
  168.          case FACE: for (i = 0 ; i < Chunk_Header.size/16 ; i++) {
  169.                        vert1 = (int) read_long(f);
  170.                        vert2 = (int) read_long(f);
  171.                        vert3 = (int) read_long(f);
  172.                        red = read_byte(f) / 255.0;
  173.                        green = read_byte(f) / 255.0;
  174.                        blue = read_byte(f) / 255.0;
  175.  
  176.                texture = read_byte(f);
  177.                texture &= 0x07;        /* mask upper bits */
  178.  
  179.                fprintf (out, "OBJECT TRIANGLE <%f %f %f> <%f %f %f> <%f %f %f> END_TRIANGLE COLOUR RED %f GREEN %f BLUE %f TEXTURE %s END_TEXTURE END_OBJECT\n",
  180.                           vertices_x[vert1], vertices_y[vert1], vertices_z[vert1],
  181.                           vertices_x[vert2], vertices_y[vert2], vertices_z[vert2],
  182.                           vertices_x[vert3], vertices_y[vert3], vertices_z[vert3],
  183.                           red, green, blue, Textures[texture]);
  184.                        }
  185.                     return;
  186.  
  187.          default:
  188.             for (i = 0 ; i < Chunk_Header.size ; i++)
  189.                if (getc(f) == EOF)
  190.                   iff_error();
  191.             break;
  192.          }
  193.       }
  194.    }
  195.  
  196. void main (argc, argv)
  197.    int argc;
  198.    char **argv;
  199.    {
  200.    if (argc != 3) {
  201.       printf ("Usage: %s <scene-file> <output-file>\n", argv[0]);
  202.       exit (1);
  203.       }
  204.    
  205.    if ((f = fopen(argv[1], "rb")) == NULL) {
  206.       printf ("Cannot open IFF file %s\n", argv[1]);
  207.       exit(1);
  208.       }
  209.  
  210.    if ((out = fopen(argv[2], "w")) == NULL) {
  211.       printf ("Cannot open output file %s\n", argv[1]);
  212.       exit(1);
  213.       }
  214.    
  215.    fprintf(out, "COMPOSITE\n");
  216.    read_iff_file();
  217.    fprintf(out, "   BOUNDED_BY\n      INTERSECTION\n");
  218.    fprintf(out, "         PLANE <1.0  0.0  0.0> %1.02f END_PLANE\n", FABS(max_x) * 1.01);
  219.    fprintf(out, "         PLANE <-1.0 0.0  0.0> %1.02f END_PLANE\n", FABS(min_x) * 1.01);
  220.    fprintf(out, "         PLANE <0.0  1.0  0.0> %1.02f END_PLANE\n", FABS(max_y) * 1.01);
  221.    fprintf(out, "         PLANE <0.0 -1.0  0.0> %1.02f END_PLANE\n", FABS(min_y) * 1.01);
  222.    fprintf(out, "         PLANE <0.0  0.0  1.0> %1.02f END_PLANE\n", FABS(max_z) * 1.01);
  223.    fprintf(out, "         PLANE <0.0  0.0 -1.0> %1.02f END_PLANE\n", FABS(min_z) * 1.01);
  224.    fprintf(out, "      END_INTERSECTION\n   END_BOUND\n   \nEND_COMPOSITE\n");
  225.    printf ("X values range from %f to %f\n", min_x, max_x);
  226.    printf ("Y values range from %f to %f\n", min_y, max_y);
  227.    printf ("Z values range from %f to %f\n", min_z, max_z);
  228.    fclose(f);
  229.    fclose(out);
  230.    }
  231.