2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 // l3dslib.c: library for loading triangles from an Alias triangle file
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
45 float fverts[MAXVERTS][3];
46 tri tris[MAXTRIANGLES];
48 int bytesread, level, numtris, totaltris;
49 int vertsfound, trisfound;
54 // Alias stores triangles as 3 explicit vertices in .tri files, so even though we
55 // start out with a vertex pool and vertex indices for triangles, we have to convert
56 // to raw, explicit triangles
57 void StoreAliasTriangles( void ){
60 if ( ( totaltris + numtris ) > MAXTRIANGLES ) {
61 Error( "Error: Too many triangles" );
64 for ( i = 0; i < numtris ; i++ )
66 for ( j = 0 ; j < 3 ; j++ )
68 for ( k = 0 ; k < 3 ; k++ )
70 ptri[i + totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
82 int ParseVertexL( FILE *input ){
83 int i, j, startbytesread, numverts;
84 unsigned short tshort;
87 Error( "Error: Multiple vertex chunks" );
91 startbytesread = bytesread;
93 if ( feof( input ) ) {
94 Error( "Error: unexpected end of file" );
97 fread( &tshort, sizeof( tshort ), 1, input );
98 bytesread += sizeof( tshort );
99 numverts = (int)tshort;
101 if ( numverts > MAXVERTS ) {
102 Error( "Error: Too many vertices" );
105 for ( i = 0 ; i < numverts ; i++ )
107 for ( j = 0 ; j < 3 ; j++ )
109 if ( feof( input ) ) {
110 Error( "Error: unexpected end of file" );
113 fread( &fverts[i][j], sizeof( float ), 1, input );
114 bytesread += sizeof( float );
118 if ( vertsfound && trisfound ) {
119 StoreAliasTriangles();
122 return bytesread - startbytesread;
126 int ParseFaceL1( FILE *input ){
128 int i, j, startbytesread;
129 unsigned short tshort;
132 Error( "Error: Multiple face chunks" );
136 startbytesread = bytesread;
138 if ( feof( input ) ) {
139 Error( "Error: unexpected end of file" );
142 fread( &tshort, sizeof( tshort ), 1, input );
143 bytesread += sizeof( tshort );
144 numtris = (int)tshort;
146 if ( numtris > MAXTRIANGLES ) {
147 Error( "Error: Too many triangles" );
150 for ( i = 0 ; i < numtris ; i++ )
152 for ( j = 0 ; j < 4 ; j++ )
154 if ( feof( input ) ) {
155 Error( "Error: unexpected end of file" );
158 fread( &tshort, sizeof( tshort ), 1, input );
159 bytesread += sizeof( tshort );
160 tris[i].v[j] = (int)tshort;
164 if ( vertsfound && trisfound ) {
165 StoreAliasTriangles();
168 return bytesread - startbytesread;
172 int ParseChunk( FILE *input ){
173 #define BLOCK_SIZE 4096
174 char temp[BLOCK_SIZE];
176 int i, length, w, t, retval;
182 if ( feof( input ) ) {
183 Error( "Error: unexpected end of file" );
186 fread( &type, sizeof( type ), 1, input );
187 bytesread += sizeof( type );
190 if ( feof( input ) ) {
191 Error( "Error: unexpected end of file" );
194 fread( &length, sizeof( length ), 1, input );
195 bytesread += sizeof( length );
198 // process chunk if we care about it, otherwise skip it
202 w -= ParseVertexL( input );
206 w -= ParseFaceL1( input );
215 if ( feof( input ) ) {
216 Error( "Error: unexpected end of file" );
219 fread( &temp[i], 1, 1, input );
223 } while ( temp[i - 1] );
228 // parse through subchunks
232 w -= ParseChunk( input );
244 if ( t > BLOCK_SIZE ) {
248 if ( feof( input ) ) {
249 Error( "Error: unexpected end of file" );
252 fread( &temp, t, 1, input );
268 void Load3DSTriangleList( char *filename, triangle_t **pptri, int *numtriangles ){
279 if ( ( input = fopen( filename, "rb" ) ) == 0 ) {
280 fprintf( stderr,"reader: could not open file '%s'\n", filename );
284 fread( &tshort, sizeof( tshort ), 1, input );
286 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
288 if ( ( tshort != MAIN3DS ) && ( tshort != EDIT3DS ) ) {
289 fprintf( stderr,"File is not a 3DS file.\n" );
293 // back to top of file so we can parse the first chunk descriptor
294 fseek( input, 0, SEEK_SET );
296 ptri = malloc( MAXTRIANGLES * sizeof( triangle_t ) );
300 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
301 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
304 if ( vertsfound || trisfound ) {
305 Error( "Incomplete triangle set" );
308 *numtriangles = totaltris;