1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4 
5    This file is part of GtkRadiant.
6 
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 //
23 // l3dslib.c: library for loading triangles from an Alias triangle file
24 //
25 
26 #include <stdio.h>
27 #include "cmdlib.h"
28 #include "mathlib.h"
29 #include "trilib.h"
30 #include "l3dslib.h"
31 
32 #define MAIN3DS       0x4D4D
33 #define EDIT3DS       0x3D3D  // this is the start of the editor config
34 #define EDIT_OBJECT   0x4000
35 #define OBJ_TRIMESH   0x4100
36 #define TRI_VERTEXL   0x4110
37 #define TRI_FACEL1    0x4120
38 
39 #define MAXVERTS    2000
40 #define MAXTRIANGLES    750
41 
42 typedef struct {
43 	int v[4];
44 } tri;
45 
46 float fverts[MAXVERTS][3];
47 tri tris[MAXTRIANGLES];
48 
49 int bytesread, level, numtris, totaltris;
50 int vertsfound, trisfound;
51 
52 triangle_t  *ptri;
53 
54 
55 // Alias stores triangles as 3 explicit vertices in .tri files, so even though we
56 // start out with a vertex pool and vertex indices for triangles, we have to convert
57 // to raw, explicit triangles
StoreAliasTriangles(void)58 void StoreAliasTriangles( void ){
59 	int i, j, k;
60 
61 	if ( ( totaltris + numtris ) > MAXTRIANGLES ) {
62 		Error( "Error: Too many triangles" );
63 	}
64 
65 	for ( i = 0; i < numtris ; i++ )
66 	{
67 		for ( j = 0 ; j < 3 ; j++ )
68 		{
69 			for ( k = 0 ; k < 3 ; k++ )
70 			{
71 				ptri[i + totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
72 			}
73 		}
74 	}
75 
76 	totaltris += numtris;
77 	numtris = 0;
78 	vertsfound = 0;
79 	trisfound = 0;
80 }
81 
82 
ParseVertexL(FILE * input)83 int ParseVertexL( FILE *input ){
84 	int i, j, startbytesread, numverts;
85 	unsigned short tshort;
86 
87 	if ( vertsfound ) {
88 		Error( "Error: Multiple vertex chunks" );
89 	}
90 
91 	vertsfound = 1;
92 	startbytesread = bytesread;
93 
94 	if ( feof( input ) ) {
95 		Error( "Error: unexpected end of file" );
96 	}
97 
98 	fread( &tshort, sizeof( tshort ), 1, input );
99 	bytesread += sizeof( tshort );
100 	numverts = (int)tshort;
101 
102 	if ( numverts > MAXVERTS ) {
103 		Error( "Error: Too many vertices" );
104 	}
105 
106 	for ( i = 0 ; i < numverts ; i++ )
107 	{
108 		for ( j = 0 ; j < 3 ; j++ )
109 		{
110 			if ( feof( input ) ) {
111 				Error( "Error: unexpected end of file" );
112 			}
113 
114 			fread( &fverts[i][j], sizeof( float ), 1, input );
115 			bytesread += sizeof( float );
116 		}
117 	}
118 
119 	if ( vertsfound && trisfound ) {
120 		StoreAliasTriangles();
121 	}
122 
123 	return bytesread - startbytesread;
124 }
125 
126 
ParseFaceL1(FILE * input)127 int ParseFaceL1( FILE *input ){
128 
129 	int i, j, startbytesread;
130 	unsigned short tshort;
131 
132 	if ( trisfound ) {
133 		Error( "Error: Multiple face chunks" );
134 	}
135 
136 	trisfound = 1;
137 	startbytesread = bytesread;
138 
139 	if ( feof( input ) ) {
140 		Error( "Error: unexpected end of file" );
141 	}
142 
143 	fread( &tshort, sizeof( tshort ), 1, input );
144 	bytesread += sizeof( tshort );
145 	numtris = (int)tshort;
146 
147 	if ( numtris > MAXTRIANGLES ) {
148 		Error( "Error: Too many triangles" );
149 	}
150 
151 	for ( i = 0 ; i < numtris ; i++ )
152 	{
153 		for ( j = 0 ; j < 4 ; j++ )
154 		{
155 			if ( feof( input ) ) {
156 				Error( "Error: unexpected end of file" );
157 			}
158 
159 			fread( &tshort, sizeof( tshort ), 1, input );
160 			bytesread += sizeof( tshort );
161 			tris[i].v[j] = (int)tshort;
162 		}
163 	}
164 
165 	if ( vertsfound && trisfound ) {
166 		StoreAliasTriangles();
167 	}
168 
169 	return bytesread - startbytesread;
170 }
171 
172 
ParseChunk(FILE * input)173 int ParseChunk( FILE *input ){
174 #define BLOCK_SIZE  4096
175 	char temp[BLOCK_SIZE];
176 	unsigned short type;
177 	int i, length, w, t, retval;
178 
179 	level++;
180 	retval = 0;
181 
182 // chunk type
183 	if ( feof( input ) ) {
184 		Error( "Error: unexpected end of file" );
185 	}
186 
187 	fread( &type, sizeof( type ), 1, input );
188 	bytesread += sizeof( type );
189 
190 // chunk length
191 	if ( feof( input ) ) {
192 		Error( "Error: unexpected end of file" );
193 	}
194 
195 	fread( &length, sizeof( length ), 1, input );
196 	bytesread += sizeof( length );
197 	w = length - 6;
198 
199 // process chunk if we care about it, otherwise skip it
200 	switch ( type )
201 	{
202 	case TRI_VERTEXL:
203 		w -= ParseVertexL( input );
204 		goto ParseSubchunk;
205 
206 	case TRI_FACEL1:
207 		w -= ParseFaceL1( input );
208 		goto ParseSubchunk;
209 
210 	case EDIT_OBJECT:
211 		// read the name
212 		i = 0;
213 
214 		do
215 		{
216 			if ( feof( input ) ) {
217 				Error( "Error: unexpected end of file" );
218 			}
219 
220 			fread( &temp[i], 1, 1, input );
221 			i++;
222 			w--;
223 			bytesread++;
224 		} while ( temp[i - 1] );
225 
226 	case MAIN3DS:
227 	case OBJ_TRIMESH:
228 	case EDIT3DS:
229 		// parse through subchunks
230 ParseSubchunk:
231 		while ( w > 0 )
232 		{
233 			w -= ParseChunk( input );
234 		}
235 
236 		retval = length;
237 		goto Done;
238 
239 	default:
240 		// skip other chunks
241 		while ( w > 0 )
242 		{
243 			t = w;
244 
245 			if ( t > BLOCK_SIZE ) {
246 				t = BLOCK_SIZE;
247 			}
248 
249 			if ( feof( input ) ) {
250 				Error( "Error: unexpected end of file" );
251 			}
252 
253 			fread( &temp, t, 1, input );
254 			bytesread += t;
255 
256 			w -= t;
257 		}
258 
259 		retval = length;
260 		goto Done;
261 	}
262 
263 Done:
264 	level--;
265 	return retval;
266 }
267 
268 
Load3DSTriangleList(char * filename,triangle_t ** pptri,int * numtriangles)269 void Load3DSTriangleList( char *filename, triangle_t **pptri, int *numtriangles ){
270 	FILE        *input;
271 	short int tshort;
272 
273 	bytesread = 0;
274 	level = 0;
275 	numtris = 0;
276 	totaltris = 0;
277 	vertsfound = 0;
278 	trisfound = 0;
279 
280 	if ( ( input = fopen( filename, "rb" ) ) == 0 ) {
281 		fprintf( stderr,"reader: could not open file '%s'\n", filename );
282 		exit( 0 );
283 	}
284 
285 	fread( &tshort, sizeof( tshort ), 1, input );
286 
287 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
288 // no MAIN3DS
289 	if ( ( tshort != MAIN3DS ) && ( tshort != EDIT3DS ) ) {
290 		fprintf( stderr,"File is not a 3DS file.\n" );
291 		exit( 0 );
292 	}
293 
294 // back to top of file so we can parse the first chunk descriptor
295 	fseek( input, 0, SEEK_SET );
296 
297 	ptri = safe_malloc( MAXTRIANGLES * sizeof( triangle_t ) );
298 
299 	*pptri = ptri;
300 
301 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
302 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
303 	ParseChunk( input );
304 
305 	if ( vertsfound || trisfound ) {
306 		Error( "Incomplete triangle set" );
307 	}
308 
309 	*numtriangles = totaltris;
310 
311 	fclose( input );
312 }
313