home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / common / trilib.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  4KB  |  171 lines

  1. //
  2. // trilib.c: library for loading triangles from an Alias triangle file
  3. //
  4.  
  5. #include <stdio.h>
  6. #include "cmdlib.h"
  7. #include "mathlib.h"
  8. #include "trilib.h"
  9.  
  10. // on disk representation of a face
  11.  
  12.  
  13. #define    FLOAT_START    99999.0
  14. #define    FLOAT_END    -FLOAT_START
  15. #define MAGIC       123322
  16.  
  17. //#define NOISY 1
  18.  
  19. typedef struct {
  20.     float v[3];
  21. } vector;
  22.  
  23. typedef struct
  24. {
  25.     vector n;    /* normal */
  26.     vector p;    /* point */
  27.     vector c;    /* color */
  28.     float  u;    /* u */
  29.     float  v;    /* v */
  30. } aliaspoint_t;
  31.  
  32. typedef struct {
  33.     aliaspoint_t    pt[3];
  34. } tf_triangle;
  35.  
  36.  
  37. void ByteSwapTri (tf_triangle *tri)
  38. {
  39.     int        i;
  40.     
  41.     for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
  42.     {
  43.         ((int *)tri)[i] = BigLong (((int *)tri)[i]);
  44.     }
  45. }
  46.  
  47. void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
  48. {
  49.     FILE        *input;
  50.     float       start;
  51.     char        name[256], tex[256];
  52.     int         i, count, magic;
  53.     tf_triangle    tri;
  54.     triangle_t    *ptri;
  55.     int            iLevel;
  56.     int            exitpattern;
  57.     float        t;
  58.  
  59.  
  60.     t = -FLOAT_START;
  61.     *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
  62.     *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
  63.     *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
  64.     *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
  65.  
  66.     if ((input = fopen(filename, "rb")) == 0) {
  67.         fprintf(stderr,"reader: could not open file '%s'\n", filename);
  68.         exit(0);
  69.     }
  70.  
  71.     iLevel = 0;
  72.  
  73.     fread(&magic, sizeof(int), 1, input);
  74.     if (BigLong(magic) != MAGIC) {
  75.         fprintf(stderr,"File is not a Alias object separated triangle file, magic number is wrong.\n");
  76.         exit(0);
  77.     }
  78.  
  79.     ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
  80.  
  81.     *pptri = ptri;
  82.  
  83.     while (feof(input) == 0) {
  84.         fread(&start,  sizeof(float), 1, input);
  85.         *(int *)&start = BigLong(*(int *)&start);
  86.         if (*(int *)&start != exitpattern)
  87.         {
  88.             if (start == FLOAT_START) {
  89.                 /* Start of an object or group of objects. */
  90.                 i = -1;
  91.                 do {
  92.                     /* There are probably better ways to read a string from */
  93.                     /* a file, but this does allow you to do error checking */
  94.                     /* (which I'm not doing) on a per character basis.      */
  95.                     ++i;
  96.                     fread( &(name[i]), sizeof( char ), 1, input);
  97.                 } while( name[i] != '\0' );
  98.     
  99.     //            indent();
  100.     //            fprintf(stdout,"OBJECT START: %s\n",name);
  101.                 fread( &count, sizeof(int), 1, input);
  102.                 count = BigLong(count);
  103.                 ++iLevel;
  104.                 if (count != 0) {
  105.     //                indent();
  106.     
  107.     //                fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
  108.     
  109.                     i = -1;
  110.                     do {
  111.                         ++i;
  112.                         fread( &(tex[i]), sizeof( char ), 1, input);
  113.                     } while( tex[i] != '\0' );
  114.     
  115.     //                indent();
  116.     //                fprintf(stdout,"  Object texture name: '%s'\n",tex);
  117.                 }
  118.     
  119.                 /* Else (count == 0) this is the start of a group, and */
  120.                 /* no texture name is present. */
  121.             }
  122.             else if (start == FLOAT_END) {
  123.                 /* End of an object or group. Yes, the name should be */
  124.                 /* obvious from context, but it is in here just to be */
  125.                 /* safe and to provide a little extra information for */
  126.                 /* those who do not wish to write a recursive reader. */
  127.                 /* Mia culpa. */
  128.                 --iLevel;
  129.                 i = -1;
  130.                 do {
  131.                     ++i;
  132.                     fread( &(name[i]), sizeof( char ), 1, input);
  133.                 } while( name[i] != '\0' );
  134.     
  135.     //            indent();
  136.     //            fprintf(stdout,"OBJECT END: %s\n",name);
  137.                 continue;
  138.             }
  139.         }
  140.  
  141. //
  142. // read the triangles
  143. //        
  144.         for (i = 0; i < count; ++i) {
  145.             int        j;
  146.  
  147.             fread( &tri, sizeof(tf_triangle), 1, input );
  148.             ByteSwapTri (&tri);
  149.             for (j=0 ; j<3 ; j++)
  150.             {
  151.                 int        k;
  152.  
  153.                 for (k=0 ; k<3 ; k++)
  154.                 {
  155.                     ptri->verts[j][k] = tri.pt[j].p.v[k];
  156.                 }
  157.             }
  158.  
  159.             ptri++;
  160.  
  161.             if ((ptri - *pptri) >= MAXTRIANGLES)
  162.                 Error ("Error: too many triangles; increase MAXTRIANGLES\n");
  163.         }
  164.     }
  165.  
  166.     *numtriangles = ptri - *pptri;
  167.  
  168.     fclose (input);
  169. }
  170.  
  171.