home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / common / trilib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-24  |  3.6 KB  |  166 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.     t = -FLOAT_START;
  60.     *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
  61.     *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
  62.     *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
  63.     *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
  64.  
  65.     if ((input = fopen(filename, "rb")) == 0)
  66.         Error ("reader: could not open file '%s'", filename);
  67.  
  68.     iLevel = 0;
  69.  
  70.     fread(&magic, sizeof(int), 1, input);
  71.     if (BigLong(magic) != MAGIC)
  72.         Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
  73.  
  74.     ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
  75.  
  76.     *pptri = ptri;
  77.  
  78.     while (feof(input) == 0) {
  79.         if (fread(&start,  sizeof(float), 1, input) < 1)
  80.             break;
  81.         *(int *)&start = BigLong(*(int *)&start);
  82.         if (*(int *)&start != exitpattern)
  83.         {
  84.             if (start == FLOAT_START) {
  85.                 /* Start of an object or group of objects. */
  86.                 i = -1;
  87.                 do {
  88.                     /* There are probably better ways to read a string from */
  89.                     /* a file, but this does allow you to do error checking */
  90.                     /* (which I'm not doing) on a per character basis.      */
  91.                     ++i;
  92.                     fread( &(name[i]), sizeof( char ), 1, input);
  93.                 } while( name[i] != '\0' );
  94.     
  95. //                indent();
  96. //                fprintf(stdout,"OBJECT START: %s\n",name);
  97.                 fread( &count, sizeof(int), 1, input);
  98.                 count = BigLong(count);
  99.                 ++iLevel;
  100.                 if (count != 0) {
  101. //                    indent();
  102. //                    fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
  103.     
  104.                     i = -1;
  105.                     do {
  106.                         ++i;
  107.                         fread( &(tex[i]), sizeof( char ), 1, input);
  108.                     } while( tex[i] != '\0' );
  109.     
  110. //                    indent();
  111. //                    fprintf(stdout,"  Object texture name: '%s'\n",tex);
  112.                 }
  113.     
  114.                 /* Else (count == 0) this is the start of a group, and */
  115.                 /* no texture name is present. */
  116.             }
  117.             else if (start == FLOAT_END) {
  118.                 /* End of an object or group. Yes, the name should be */
  119.                 /* obvious from context, but it is in here just to be */
  120.                 /* safe and to provide a little extra information for */
  121.                 /* those who do not wish to write a recursive reader. */
  122.                 /* Mia culpa. */
  123.                 --iLevel;
  124.                 i = -1;
  125.                 do {
  126.                     ++i;
  127.                     fread( &(name[i]), sizeof( char ), 1, input);
  128.                 } while( name[i] != '\0' );
  129.     
  130. //                indent();
  131. //                fprintf(stdout,"OBJECT END: %s\n",name);
  132.                 continue;
  133.             }
  134.         }
  135.  
  136. //
  137. // read the triangles
  138. //        
  139.         for (i = 0; i < count; ++i) {
  140.             int        j;
  141.  
  142.             fread( &tri, sizeof(tf_triangle), 1, input );
  143.             ByteSwapTri (&tri);
  144.             for (j=0 ; j<3 ; j++)
  145.             {
  146.                 int        k;
  147.  
  148.                 for (k=0 ; k<3 ; k++)
  149.                 {
  150.                     ptri->verts[j][k] = tri.pt[j].p.v[k];
  151.                 }
  152.             }
  153.  
  154.             ptri++;
  155.  
  156.             if ((ptri - *pptri) >= MAXTRIANGLES)
  157.                 Error ("Error: too many triangles; increase MAXTRIANGLES\n");
  158.         }
  159.     }
  160.  
  161.     *numtriangles = ptri - *pptri;
  162.  
  163.     fclose (input);
  164. }
  165.  
  166.