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
22 // 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
45 float fverts[MAXVERTS][3];
\r
46 tri tris[MAXTRIANGLES];
\r
48 int bytesread, level, numtris, totaltris;
\r
49 int vertsfound, trisfound;
\r
54 // Alias stores triangles as 3 explicit vertices in .tri files, so even though we
\r
55 // start out with a vertex pool and vertex indices for triangles, we have to convert
\r
56 // to raw, explicit triangles
\r
57 void StoreAliasTriangles (void)
\r
61 if ((totaltris + numtris) > MAXTRIANGLES)
\r
62 Error ("Error: Too many triangles");
\r
64 for (i=0; i<numtris ; i++)
\r
66 for (j=0 ; j<3 ; j++)
\r
68 for (k=0 ; k<3 ; k++)
\r
70 ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
\r
75 totaltris += numtris;
\r
82 int ParseVertexL (FILE *input)
\r
84 int i, j, startbytesread, numverts;
\r
85 unsigned short tshort;
\r
88 Error ("Error: Multiple vertex chunks");
\r
91 startbytesread = bytesread;
\r
94 Error ("Error: unexpected end of file");
\r
96 fread(&tshort, sizeof(tshort), 1, input);
\r
97 bytesread += sizeof(tshort);
\r
98 numverts = (int)tshort;
\r
100 if (numverts > MAXVERTS)
\r
101 Error ("Error: Too many vertices");
\r
103 for (i=0 ; i<numverts ; i++)
\r
105 for (j=0 ; j<3 ; j++)
\r
108 Error ("Error: unexpected end of file");
\r
110 fread(&fverts[i][j], sizeof(float), 1, input);
\r
111 bytesread += sizeof(float);
\r
115 if (vertsfound && trisfound)
\r
116 StoreAliasTriangles ();
\r
118 return bytesread - startbytesread;
\r
122 int ParseFaceL1 (FILE *input)
\r
125 int i, j, startbytesread;
\r
126 unsigned short tshort;
\r
129 Error ("Error: Multiple face chunks");
\r
132 startbytesread = bytesread;
\r
135 Error ("Error: unexpected end of file");
\r
137 fread(&tshort, sizeof(tshort), 1, input);
\r
138 bytesread += sizeof(tshort);
\r
139 numtris = (int)tshort;
\r
141 if (numtris > MAXTRIANGLES)
\r
142 Error ("Error: Too many triangles");
\r
144 for (i=0 ; i<numtris ; i++)
\r
146 for (j=0 ; j<4 ; j++)
\r
149 Error ("Error: unexpected end of file");
\r
151 fread(&tshort, sizeof(tshort), 1, input);
\r
152 bytesread += sizeof(tshort);
\r
153 tris[i].v[j] = (int)tshort;
\r
157 if (vertsfound && trisfound)
\r
158 StoreAliasTriangles ();
\r
160 return bytesread - startbytesread;
\r
164 int ParseChunk (FILE *input)
\r
166 #define BLOCK_SIZE 4096
\r
167 char temp[BLOCK_SIZE];
\r
168 unsigned short type;
\r
169 int i, length, w, t, retval;
\r
176 Error ("Error: unexpected end of file");
\r
178 fread(&type, sizeof(type), 1, input);
\r
179 bytesread += sizeof(type);
\r
183 Error ("Error: unexpected end of file");
\r
185 fread (&length, sizeof(length), 1, input);
\r
186 bytesread += sizeof(length);
\r
189 // process chunk if we care about it, otherwise skip it
\r
193 w -= ParseVertexL (input);
\r
194 goto ParseSubchunk;
\r
197 w -= ParseFaceL1 (input);
\r
198 goto ParseSubchunk;
\r
207 Error ("Error: unexpected end of file");
\r
209 fread (&temp[i], 1, 1, input);
\r
213 } while (temp[i-1]);
\r
218 // parse through subchunks
\r
222 w -= ParseChunk (input);
\r
229 // skip other chunks
\r
234 if (t > BLOCK_SIZE)
\r
238 Error ("Error: unexpected end of file");
\r
240 fread (&temp, t, 1, input);
\r
256 void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
\r
268 if ((input = fopen(filename, "rb")) == 0) {
\r
269 fprintf(stderr,"reader: could not open file '%s'\n", filename);
\r
273 fread(&tshort, sizeof(tshort), 1, input);
\r
275 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
\r
277 if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {
\r
278 fprintf(stderr,"File is not a 3DS file.\n");
\r
282 // back to top of file so we can parse the first chunk descriptor
\r
283 fseek(input, 0, SEEK_SET);
\r
285 ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
\r
289 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
\r
290 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
\r
291 ParseChunk (input);
\r
293 if (vertsfound || trisfound)
\r
294 Error ("Incomplete triangle set");
\r
296 *numtriangles = totaltris;
\r