2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
23 // l3dslib.c: library for loading triangles from an Alias triangle file
\r
28 #include "mathlib.h"
\r
30 #include "l3dslib.h"
\r
32 #define MAIN3DS 0x4D4D
\r
33 #define EDIT3DS 0x3D3D // this is the start of the editor config
\r
34 #define EDIT_OBJECT 0x4000
\r
35 #define OBJ_TRIMESH 0x4100
\r
36 #define TRI_VERTEXL 0x4110
\r
37 #define TRI_FACEL1 0x4120
\r
39 #define MAXVERTS 2000
\r
40 #define MAXTRIANGLES 750
\r
46 float fverts[MAXVERTS][3];
\r
47 tri tris[MAXTRIANGLES];
\r
49 int bytesread, level, numtris, totaltris;
\r
50 int vertsfound, trisfound;
\r
55 // Alias stores triangles as 3 explicit vertices in .tri files, so even though we
\r
56 // start out with a vertex pool and vertex indices for triangles, we have to convert
\r
57 // to raw, explicit triangles
\r
58 void StoreAliasTriangles (void)
\r
62 if ((totaltris + numtris) > MAXTRIANGLES)
\r
63 Error ("Error: Too many triangles");
\r
65 for (i=0; i<numtris ; i++)
\r
67 for (j=0 ; j<3 ; j++)
\r
69 for (k=0 ; k<3 ; k++)
\r
71 ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
\r
76 totaltris += numtris;
\r
83 int ParseVertexL (FILE *input)
\r
85 int i, j, startbytesread, numverts;
\r
86 unsigned short tshort;
\r
89 Error ("Error: Multiple vertex chunks");
\r
92 startbytesread = bytesread;
\r
95 Error ("Error: unexpected end of file");
\r
97 fread(&tshort, sizeof(tshort), 1, input);
\r
98 bytesread += sizeof(tshort);
\r
99 numverts = (int)tshort;
\r
101 if (numverts > MAXVERTS)
\r
102 Error ("Error: Too many vertices");
\r
104 for (i=0 ; i<numverts ; i++)
\r
106 for (j=0 ; j<3 ; j++)
\r
109 Error ("Error: unexpected end of file");
\r
111 fread(&fverts[i][j], sizeof(float), 1, input);
\r
112 bytesread += sizeof(float);
\r
116 if (vertsfound && trisfound)
\r
117 StoreAliasTriangles ();
\r
119 return bytesread - startbytesread;
\r
123 int ParseFaceL1 (FILE *input)
\r
126 int i, j, startbytesread;
\r
127 unsigned short tshort;
\r
130 Error ("Error: Multiple face chunks");
\r
133 startbytesread = bytesread;
\r
136 Error ("Error: unexpected end of file");
\r
138 fread(&tshort, sizeof(tshort), 1, input);
\r
139 bytesread += sizeof(tshort);
\r
140 numtris = (int)tshort;
\r
142 if (numtris > MAXTRIANGLES)
\r
143 Error ("Error: Too many triangles");
\r
145 for (i=0 ; i<numtris ; i++)
\r
147 for (j=0 ; j<4 ; j++)
\r
150 Error ("Error: unexpected end of file");
\r
152 fread(&tshort, sizeof(tshort), 1, input);
\r
153 bytesread += sizeof(tshort);
\r
154 tris[i].v[j] = (int)tshort;
\r
158 if (vertsfound && trisfound)
\r
159 StoreAliasTriangles ();
\r
161 return bytesread - startbytesread;
\r
165 int ParseChunk (FILE *input)
\r
167 #define BLOCK_SIZE 4096
\r
168 char temp[BLOCK_SIZE];
\r
169 unsigned short type;
\r
170 int i, length, w, t, retval;
\r
177 Error ("Error: unexpected end of file");
\r
179 fread(&type, sizeof(type), 1, input);
\r
180 bytesread += sizeof(type);
\r
184 Error ("Error: unexpected end of file");
\r
186 fread (&length, sizeof(length), 1, input);
\r
187 bytesread += sizeof(length);
\r
190 // process chunk if we care about it, otherwise skip it
\r
194 w -= ParseVertexL (input);
\r
195 goto ParseSubchunk;
\r
198 w -= ParseFaceL1 (input);
\r
199 goto ParseSubchunk;
\r
208 Error ("Error: unexpected end of file");
\r
210 fread (&temp[i], 1, 1, input);
\r
214 } while (temp[i-1]);
\r
219 // parse through subchunks
\r
223 w -= ParseChunk (input);
\r
230 // skip other chunks
\r
235 if (t > BLOCK_SIZE)
\r
239 Error ("Error: unexpected end of file");
\r
241 fread (&temp, t, 1, input);
\r
257 void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
\r
269 if ((input = fopen(filename, "rb")) == 0) {
\r
270 fprintf(stderr,"reader: could not open file '%s'\n", filename);
\r
274 fread(&tshort, sizeof(tshort), 1, input);
\r
276 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
\r
278 if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {
\r
279 fprintf(stderr,"File is not a 3DS file.\n");
\r
283 // back to top of file so we can parse the first chunk descriptor
\r
284 fseek(input, 0, SEEK_SET);
\r
286 ptri = safe_malloc (MAXTRIANGLES * sizeof(triangle_t));
\r
290 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
\r
291 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
\r
292 ParseChunk (input);
\r
294 if (vertsfound || trisfound)
\r
295 Error ("Incomplete triangle set");
\r
297 *numtriangles = totaltris;
\r