/** * @file l3dslib.c * @brief library for loading triangles from an Alias triangle file */ /* Copyright (C) 1997-2001 Id Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../../ufo2map/common/cmdlib.h" #include "../../ufo2map/common/mathlib.h" #include "trilib.h" #include "l3dslib.h" #define MAIN3DS 0x4D4D #define EDIT3DS 0x3D3D /* this is the start of the editor config */ #define EDIT_OBJECT 0x4000 #define OBJ_TRIMESH 0x4100 #define TRI_VERTEXL 0x4110 #define TRI_FACEL1 0x4120 #define MAXVERTS 2000 typedef struct { int v[4]; } tri; static float fverts[MAXVERTS][3]; static tri tris[MAXTRIANGLES]; static int bytesread, level, numtris, totaltris; static int vertsfound, trisfound; static triangle_t *ptri; /** * @brief Alias stores triangles as 3 explicit vertices in .tri files, so even though we * start out with a vertex pool and vertex indices for triangles, we have to convert * to raw, explicit triangles */ static void StoreAliasTriangles (void) { int i, j, k; if ((totaltris + numtris) > MAXTRIANGLES) Error ("Error: Too many triangles"); for (i=0; i MAXVERTS) Error ("Error: Too many vertices"); for (i=0 ; i MAXTRIANGLES) Error ("Error: Too many triangles"); for (i=0 ; i 0) { w -= ParseChunk (input); } retval = length; goto Done; default: /* skip other chunks */ while (w > 0) { t = w; if (t > BLOCK_SIZE) t = BLOCK_SIZE; if (feof(input)) Error ("Error: unexpected end of file"); if (fread (&temp, t, 1, input) != 1) printf("ParseChunk: read error\n"); bytesread += t; w -= t; } retval = length; goto Done; } Done: level--; return retval; } /** * @brief */ extern void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles) { FILE *input; short int tshort; bytesread = 0; level = 0; numtris = 0; totaltris = 0; vertsfound = 0; trisfound = 0; if ((input = fopen(filename, "rb")) == 0) { fprintf(stderr,"reader: could not open file '%s'\n", filename); exit(0); } if (fread(&tshort, sizeof(tshort), 1, input) != 1) printf("Load3DSTriangleList: read error\n"); /* should only be MAIN3DS, but some files seem to start with EDIT3DS, with */ /* no MAIN3DS */ if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) { fprintf(stderr,"File is not a 3DS file.\n"); exit(0); } /* back to top of file so we can parse the first chunk descriptor */ fseek(input, 0, SEEK_SET); ptri = malloc (MAXTRIANGLES * sizeof(triangle_t)); *pptri = ptri; /* parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT | */ /* OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks */ ParseChunk (input); if (vertsfound || trisfound) Error ("Incomplete triangle set"); *numtriangles = totaltris; fclose (input); }