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

l3dslib.c File Reference

#include <stdio.h>
#include "cmdlib.h"
#include "mathlib.h"
#include "trilib.h"
#include "l3dslib.h"

Include dependency graph for l3dslib.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  tri

Defines

#define BLOCK_SIZE   4096
#define EDIT3DS   0x3D3D
#define EDIT_OBJECT   0x4000
#define MAIN3DS   0x4D4D
#define MAXVERTS   2000
#define OBJ_TRIMESH   0x4100
#define TRI_FACEL1   0x4120
#define TRI_VERTEXL   0x4110

Functions

void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
int ParseChunk (FILE *input)
int ParseFaceL1 (FILE *input)
int ParseVertexL (FILE *input)
void StoreAliasTriangles (void)

Variables

int bytesread
float fverts [MAXVERTS][3]
int level
int numtris
triangle_tptri
int totaltris
tri tris [MAXTRIANGLES]
int trisfound
int vertsfound


Define Documentation

#define BLOCK_SIZE   4096
 

Referenced by SelectSplitPlaneNum().

#define EDIT3DS   0x3D3D
 

Definition at line 33 of file l3dslib.c.

Referenced by ParseChunk().

#define EDIT_OBJECT   0x4000
 

Definition at line 34 of file l3dslib.c.

Referenced by ParseChunk().

#define MAIN3DS   0x4D4D
 

Definition at line 32 of file l3dslib.c.

Referenced by ParseChunk().

#define MAXVERTS   2000
 

Definition at line 39 of file l3dslib.c.

#define OBJ_TRIMESH   0x4100
 

Definition at line 35 of file l3dslib.c.

Referenced by ParseChunk().

#define TRI_FACEL1   0x4120
 

Definition at line 37 of file l3dslib.c.

Referenced by ParseChunk().

#define TRI_VERTEXL   0x4110
 

Definition at line 36 of file l3dslib.c.

Referenced by ParseChunk().


Function Documentation

void Load3DSTriangleList char *  filename,
triangle_t **  pptri,
int *  numtriangles
 

Definition at line 256 of file l3dslib.c.

References bytesread, Error(), exit(), fclose(), fopen(), fprintf(), fread(), fseek(), input, level, malloc(), numtris, ParseChunk(), ptri, SEEK_SET, stderr, totaltris, trisfound, and vertsfound.

00257 {
00258     FILE        *input;
00259     short int   tshort;
00260 
00261     bytesread = 0;
00262     level = 0;
00263     numtris = 0;
00264     totaltris = 0;
00265     vertsfound = 0;
00266     trisfound = 0;
00267 
00268     if ((input = fopen(filename, "rb")) == 0) {
00269         fprintf(stderr,"reader: could not open file '%s'\n", filename);
00270         exit(0);
00271     }
00272 
00273     fread(&tshort, sizeof(tshort), 1, input);
00274 
00275 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
00276 // no MAIN3DS
00277     if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {
00278         fprintf(stderr,"File is not a 3DS file.\n");
00279         exit(0);
00280     }
00281 
00282 // back to top of file so we can parse the first chunk descriptor
00283     fseek(input, 0, SEEK_SET);
00284 
00285     ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
00286 
00287     *pptri = ptri;
00288 
00289 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
00290 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
00291     ParseChunk (input);
00292 
00293     if (vertsfound || trisfound)
00294         Error ("Incomplete triangle set");
00295 
00296     *numtriangles = totaltris;
00297 
00298     fclose (input);
00299 }

Here is the call graph for this function:

int ParseChunk FILE input  ) 
 

Definition at line 164 of file l3dslib.c.

References bytesread, Done, EDIT3DS, EDIT_OBJECT, Error(), feof, fread(), i, input, length(), level, MAIN3DS, OBJ_TRIMESH, ParseFaceL1(), ParseVertexL(), t, TRI_FACEL1, TRI_VERTEXL, type, and w.

Referenced by Load3DSTriangleList().

00165 {
00166 #define BLOCK_SIZE  4096
00167     char            temp[BLOCK_SIZE];
00168     unsigned short  type;
00169     int             i, length, w, t, retval;
00170 
00171     level++;
00172     retval = 0;
00173 
00174 // chunk type
00175     if (feof(input))
00176         Error ("Error: unexpected end of file");
00177 
00178     fread(&type, sizeof(type), 1, input);
00179     bytesread += sizeof(type);
00180 
00181 // chunk length
00182     if (feof(input))
00183         Error ("Error: unexpected end of file");
00184 
00185     fread (&length, sizeof(length), 1, input);
00186     bytesread += sizeof(length);
00187     w = length - 6;
00188 
00189 // process chunk if we care about it, otherwise skip it
00190     switch (type)
00191     {
00192     case TRI_VERTEXL:
00193         w -= ParseVertexL (input);
00194         goto ParseSubchunk;
00195 
00196     case TRI_FACEL1:
00197         w -= ParseFaceL1 (input);
00198         goto ParseSubchunk;
00199 
00200     case EDIT_OBJECT:
00201     // read the name
00202         i = 0;
00203 
00204         do
00205         {
00206             if (feof(input))
00207                 Error ("Error: unexpected end of file");
00208 
00209             fread (&temp[i], 1, 1, input);
00210             i++;
00211             w--;
00212             bytesread++;
00213         } while (temp[i-1]);
00214 
00215     case MAIN3DS:
00216     case OBJ_TRIMESH:
00217     case EDIT3DS:
00218     // parse through subchunks
00219 ParseSubchunk:
00220         while (w > 0)
00221         {
00222             w -= ParseChunk (input);
00223         }
00224 
00225         retval = length;
00226         goto Done;
00227 
00228     default:
00229     // skip other chunks
00230         while (w > 0)
00231         {
00232             t = w;
00233 
00234             if (t > BLOCK_SIZE)
00235                 t = BLOCK_SIZE;
00236 
00237             if (feof(input))
00238                 Error ("Error: unexpected end of file");
00239 
00240             fread (&temp, t, 1, input);
00241             bytesread += t;
00242 
00243             w -= t;
00244         }
00245 
00246         retval = length;
00247         goto Done;
00248     }
00249 
00250 Done:
00251     level--;
00252     return retval;
00253 }

Here is the call graph for this function:

int ParseFaceL1 FILE input  ) 
 

Definition at line 122 of file l3dslib.c.

References bytesread, Error(), feof, fread(), i, input, j, numtris, StoreAliasTriangles(), tris, trisfound, tri::v, and vertsfound.

Referenced by ParseChunk().

00123 {
00124 
00125     int             i, j, startbytesread;
00126     unsigned short  tshort;
00127 
00128     if (trisfound)
00129         Error ("Error: Multiple face chunks");
00130 
00131     trisfound = 1;
00132     startbytesread = bytesread;
00133 
00134     if (feof(input))
00135         Error ("Error: unexpected end of file");
00136 
00137     fread(&tshort, sizeof(tshort), 1, input);
00138     bytesread += sizeof(tshort);
00139     numtris = (int)tshort;
00140 
00141     if (numtris > MAXTRIANGLES)
00142         Error ("Error: Too many triangles");
00143 
00144     for (i=0 ; i<numtris ; i++)
00145     {
00146         for (j=0 ; j<4 ; j++)
00147         {
00148             if (feof(input))
00149                 Error ("Error: unexpected end of file");
00150 
00151             fread(&tshort, sizeof(tshort), 1, input);
00152             bytesread += sizeof(tshort);
00153             tris[i].v[j] = (int)tshort;
00154         }
00155     }
00156 
00157     if (vertsfound && trisfound)
00158         StoreAliasTriangles ();
00159 
00160     return bytesread - startbytesread;
00161 }

Here is the call graph for this function:

int ParseVertexL FILE input  ) 
 

Definition at line 82 of file l3dslib.c.

References bytesread, Error(), feof, fread(), fverts, i, input, j, StoreAliasTriangles(), and vertsfound.

Referenced by ParseChunk().

00083 {
00084     int             i, j, startbytesread, numverts;
00085     unsigned short  tshort;
00086 
00087     if (vertsfound)
00088         Error ("Error: Multiple vertex chunks");
00089 
00090     vertsfound = 1;
00091     startbytesread = bytesread;
00092 
00093     if (feof(input))
00094         Error ("Error: unexpected end of file");
00095 
00096     fread(&tshort, sizeof(tshort), 1, input);
00097     bytesread += sizeof(tshort);
00098     numverts = (int)tshort;
00099 
00100     if (numverts > MAXVERTS)
00101         Error ("Error: Too many vertices");
00102 
00103     for (i=0 ; i<numverts ; i++)
00104     {
00105         for (j=0 ; j<3 ; j++)
00106         {
00107             if (feof(input))
00108                 Error ("Error: unexpected end of file");
00109 
00110             fread(&fverts[i][j], sizeof(float), 1, input);
00111             bytesread += sizeof(float);
00112         }
00113     }
00114 
00115     if (vertsfound && trisfound)
00116         StoreAliasTriangles ();
00117 
00118     return bytesread - startbytesread;
00119 }

Here is the call graph for this function:

void StoreAliasTriangles void   ) 
 

Definition at line 57 of file l3dslib.c.

References Error(), fverts, i, j, k, numtris, ptri, totaltris, tris, trisfound, tri::v, triangle_t::verts, and vertsfound.

Referenced by ParseFaceL1(), and ParseVertexL().

00058 {
00059     int     i, j, k;
00060 
00061     if ((totaltris + numtris) > MAXTRIANGLES)
00062         Error ("Error: Too many triangles");
00063 
00064     for (i=0; i<numtris ; i++)
00065     {
00066         for (j=0 ; j<3 ; j++)
00067         {
00068             for (k=0 ; k<3 ; k++)
00069             {
00070                 ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
00071             }
00072         }
00073     }
00074     
00075     totaltris += numtris;
00076     numtris = 0;
00077     vertsfound = 0;
00078     trisfound = 0;
00079 }

Here is the call graph for this function:


Variable Documentation

int bytesread
 

Definition at line 48 of file l3dslib.c.

Referenced by Load3DSTriangleList(), ParseChunk(), ParseFaceL1(), and ParseVertexL().

float fverts[MAXVERTS][3]
 

Definition at line 45 of file l3dslib.c.

Referenced by ParseVertexL(), and StoreAliasTriangles().

int level
 

Definition at line 48 of file l3dslib.c.

int numtris
 

Definition at line 48 of file l3dslib.c.

Referenced by Load3DSTriangleList(), ParseFaceL1(), and StoreAliasTriangles().

triangle_t* ptri
 

Definition at line 51 of file l3dslib.c.

Referenced by Load3DSTriangleList(), ReadPolysetGeometry(), StoreAliasTriangles(), and TRI_LoadPolysets().

int totaltris
 

Definition at line 48 of file l3dslib.c.

Referenced by Load3DSTriangleList(), and StoreAliasTriangles().

tri tris[MAXTRIANGLES]
 

Definition at line 46 of file l3dslib.c.

Referenced by ParseFaceL1(), and StoreAliasTriangles().

int trisfound
 

Definition at line 49 of file l3dslib.c.

Referenced by Load3DSTriangleList(), ParseFaceL1(), and StoreAliasTriangles().

int vertsfound
 

Definition at line 49 of file l3dslib.c.

Referenced by Load3DSTriangleList(), ParseFaceL1(), ParseVertexL(), and StoreAliasTriangles().


Generated on Thu Aug 25 15:46:19 2005 for Quake III Arena by  doxygen 1.3.9.1