Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

trilib.h File Reference

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Functions

void TRI_LoadPolysets (const char *filename, polyset_t **ppPSET, int *numpsets)


Function Documentation

void TRI_LoadPolysets const char *  filename,
polyset_t **  ppPSET,
int *  numpsets
 

Definition at line 100 of file trilib.c.

References BigLong(), calloc(), count, Error(), fclose(), feof, fopen(), fread(), i, input, name, polyset_t::numtriangles, POLYSET_MAXPOLYSETS, POLYSET_MAXTRIANGLES, ptri, ReadPolysetGeometry(), strcpy(), strlwr(), strncpy(), t, and polyset_t::triangles.

00101 {
00102     FILE        *input;
00103     float       start;
00104     char        name[256], tex[256];
00105     int         i, count, magic, pset = 0;
00106     triangle_t  *ptri;
00107     polyset_t   *pPSET;
00108     int         iLevel;
00109     int         exitpattern;
00110     float       t;
00111 
00112     t = -FLOAT_START;
00113     *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
00114     *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
00115     *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
00116     *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
00117 
00118     if ((input = fopen(filename, "rb")) == 0)
00119         Error ("reader: could not open file '%s'", filename);
00120 
00121     iLevel = 0;
00122 
00123     fread(&magic, sizeof(int), 1, input);
00124     if (BigLong(magic) != MAGIC)
00125         Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
00126 
00127     pPSET = calloc( 1, POLYSET_MAXPOLYSETS * sizeof( polyset_t ) );
00128     ptri = calloc( 1, POLYSET_MAXTRIANGLES * sizeof( triangle_t ) );
00129 
00130     *ppPSET = pPSET;
00131 
00132     while (feof(input) == 0) {
00133         if (fread(&start,  sizeof(float), 1, input) < 1)
00134             break;
00135         *(int *)&start = BigLong(*(int *)&start);
00136         if (*(int *)&start != exitpattern)
00137         {
00138             if (start == FLOAT_START) {
00139                 /* Start of an object or group of objects. */
00140                 i = -1;
00141                 do {
00142                     /* There are probably better ways to read a string from */
00143                     /* a file, but this does allow you to do error checking */
00144                     /* (which I'm not doing) on a per character basis.      */
00145                     ++i;
00146                     fread( &(name[i]), sizeof( char ), 1, input);
00147                 } while( name[i] != '\0' );
00148     
00149                 if ( i != 0 )
00150                     strncpy( pPSET[pset].name, name, sizeof( pPSET[pset].name ) - 1 );
00151                 else
00152                     strcpy( pPSET[pset].name , "(unnamed)" );
00153                 strlwr( pPSET[pset].name );
00154 
00155 //              indent();
00156 //              fprintf(stdout,"OBJECT START: %s\n",name);
00157                 fread( &count, sizeof(int), 1, input);
00158                 count = BigLong(count);
00159                 ++iLevel;
00160                 if (count != 0) {
00161 //                  indent();
00162 //                  fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
00163     
00164                     i = -1;
00165                     do {
00166                         ++i;
00167                         fread( &(tex[i]), sizeof( char ), 1, input);
00168                     } while( tex[i] != '\0' );
00169 
00170 /*
00171                     if ( i != 0 )
00172                         strncpy( pPSET[pset].texname, tex, sizeof( pPSET[pset].texname ) - 1 );
00173                     else
00174                         strcpy( pPSET[pset].texname, "(unnamed)" );
00175                     strlwr( pPSET[pset].texname );
00176 */
00177 
00178 //                  indent();
00179 //                  fprintf(stdout,"  Object texture name: '%s'\n",tex);
00180                 }
00181     
00182                 /* Else (count == 0) this is the start of a group, and */
00183                 /* no texture name is present. */
00184             }
00185             else if (start == FLOAT_END) {
00186                 /* End of an object or group. Yes, the name should be */
00187                 /* obvious from context, but it is in here just to be */
00188                 /* safe and to provide a little extra information for */
00189                 /* those who do not wish to write a recursive reader. */
00190                 /* Mea culpa. */
00191                 --iLevel;
00192                 i = -1;
00193                 do {
00194                     ++i;
00195                     fread( &(name[i]), sizeof( char ), 1, input);
00196                 } while( name[i] != '\0' );
00197 
00198                 if ( i != 0 )
00199                     strncpy( pPSET[pset].name, name, sizeof( pPSET[pset].name ) - 1 );
00200                 else
00201                     strcpy( pPSET[pset].name , "(unnamed)" );
00202 
00203                 strlwr( pPSET[pset].name );
00204     
00205 //              indent();
00206 //              fprintf(stdout,"OBJECT END: %s\n",name);
00207                 continue;
00208             }
00209         }
00210 
00211 //
00212 // read the triangles
00213 //      
00214         if ( count > 0 )
00215         {
00216             pPSET[pset].triangles = ptri;
00217             ReadPolysetGeometry( pPSET[0].triangles, input, count, ptri );
00218             ptri += count;
00219             pPSET[pset].numtriangles = count;
00220             if ( ++pset >= POLYSET_MAXPOLYSETS )
00221             {
00222                 Error ("Error: too many polysets; increase POLYSET_MAXPOLYSETS\n");
00223             }
00224         }
00225     }
00226 
00227     *numpsets = pset;
00228 
00229     fclose (input);
00230 }

Here is the call graph for this function:


Generated on Thu Aug 25 15:47:04 2005 for Quake III Arena by  doxygen 1.3.9.1