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
23 // l3dslib.c: library for loading triangles from an Alias triangle file
36 #define MAIN3DS 0x4D4D
37 #define EDIT3DS 0x3D3D // this is the start of the editor config
38 #define EDIT_OBJECT 0x4000
39 #define OBJ_TRIMESH 0x4100
40 #define TRI_VERTEXL 0x4110
41 #define TRI_FACEL1 0x4120
49 float fverts[MAXVERTS][3];
50 tri tris[MAXTRIANGLES];
52 int bytesread, level, numtris, totaltris;
53 int vertsfound, trisfound;
59 void DefaultNodesList( mesh_node_t **nodesList, int *num_mesh_nodes, int *numtriangles ){
64 memset( &( *nodesList )[0], 0, sizeof( mesh_node_t ) );
65 strcpy( ( *nodesList )[0].name, "default" );
67 // set all of the tris to be used for the top node
68 for ( i = 0; i < ( *numtriangles ); i++ )
71 bit = 1 << ( ( i ) & 7 );
73 ( *nodesList )[0].tris[pos] |= bit;
79 // Alias stores triangles as 3 explicit vertices in .tri files, so even though we
80 // start out with a vertex pool and vertex indices for triangles, we have to convert
81 // to raw, explicit triangles
82 void StoreAliasTriangles( void ){
85 if ( ( totaltris + numtris ) > MAXTRIANGLES ) {
86 Error( "Error: Too many triangles" );
89 for ( i = 0; i < numtris ; i++ )
91 for ( j = 0 ; j < 3 ; j++ )
93 for ( k = 0 ; k < 3 ; k++ )
95 ptri[i + totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
100 totaltris += numtris;
107 int ParseVertexL( FILE *input ){
108 int i, j, startbytesread, numverts;
109 unsigned short tshort;
112 Error( "Error: Multiple vertex chunks" );
116 startbytesread = bytesread;
118 if ( feof( input ) ) {
119 Error( "Error: unexpected end of file" );
122 fread( &tshort, sizeof( tshort ), 1, input );
123 bytesread += sizeof( tshort );
124 numverts = (int)tshort;
126 if ( numverts > MAXVERTS ) {
127 Error( "Error: Too many vertices" );
130 for ( i = 0 ; i < numverts ; i++ )
132 for ( j = 0 ; j < 3 ; j++ )
134 if ( feof( input ) ) {
135 Error( "Error: unexpected end of file" );
138 fread( &fverts[i][j], sizeof( float ), 1, input );
139 bytesread += sizeof( float );
143 if ( vertsfound && trisfound ) {
144 StoreAliasTriangles();
147 return bytesread - startbytesread;
151 int ParseFaceL1( FILE *input ){
153 int i, j, startbytesread;
154 unsigned short tshort;
157 Error( "Error: Multiple face chunks" );
161 startbytesread = bytesread;
163 if ( feof( input ) ) {
164 Error( "Error: unexpected end of file" );
167 fread( &tshort, sizeof( tshort ), 1, input );
168 bytesread += sizeof( tshort );
169 numtris = (int)tshort;
171 if ( numtris > MAXTRIANGLES ) {
172 Error( "Error: Too many triangles" );
175 for ( i = 0 ; i < numtris ; i++ )
177 for ( j = 0 ; j < 4 ; j++ )
179 if ( feof( input ) ) {
180 Error( "Error: unexpected end of file" );
183 fread( &tshort, sizeof( tshort ), 1, input );
184 bytesread += sizeof( tshort );
185 tris[i].v[j] = (int)tshort;
189 if ( vertsfound && trisfound ) {
190 StoreAliasTriangles();
193 return bytesread - startbytesread;
197 int ParseChunk( FILE *input ){
198 #define BLOCK_SIZE 4096
199 char temp[BLOCK_SIZE];
201 int i, length, w, t, retval;
207 if ( feof( input ) ) {
208 Error( "Error: unexpected end of file" );
211 fread( &type, sizeof( type ), 1, input );
212 bytesread += sizeof( type );
215 if ( feof( input ) ) {
216 Error( "Error: unexpected end of file" );
219 fread( &length, sizeof( length ), 1, input );
220 bytesread += sizeof( length );
223 // process chunk if we care about it, otherwise skip it
227 w -= ParseVertexL( input );
231 w -= ParseFaceL1( input );
240 if ( feof( input ) ) {
241 Error( "Error: unexpected end of file" );
244 fread( &temp[i], 1, 1, input );
248 } while ( temp[i - 1] );
253 // parse through subchunks
257 w -= ParseChunk( input );
269 if ( t > BLOCK_SIZE ) {
273 if ( feof( input ) ) {
274 Error( "Error: unexpected end of file" );
277 fread( &temp, t, 1, input );
293 void Load3DSTriangleList( char *filename, triangle_t **pptri, int *numtriangles, mesh_node_t **nodesList, int *num_mesh_nodes ){
299 *nodesList = (mesh_node_t *) SafeMalloc( MAX_FM_MESH_NODES * sizeof( mesh_node_t ), "Mesh Node List" );
309 if ( ( input = fopen( filename, "rb" ) ) == 0 ) {
310 fprintf( stderr,"reader: could not open file '%s'\n", filename );
314 fread( &tshort, sizeof( tshort ), 1, input );
316 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
318 if ( ( tshort != MAIN3DS ) && ( tshort != EDIT3DS ) ) {
319 fprintf( stderr,"File is not a 3DS file.\n" );
323 // back to top of file so we can parse the first chunk descriptor
324 fseek( input, 0, SEEK_SET );
326 ptri = malloc( MAXTRIANGLES * sizeof( triangle_t ) );
330 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
331 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
334 if ( vertsfound || trisfound ) {
335 Error( "Incomplete triangle set" );
338 *numtriangles = totaltris;
342 DefaultNodesList( nodesList,num_mesh_nodes,numtriangles );
345 //==========================================================================
349 //==========================================================================
351 void LoadASC( char *fileName, triangle_t **triList, int *triangleCount, mesh_node_t **nodesList, int *num_mesh_nodes ){
367 *nodesList = (mesh_node_t *) SafeMalloc( MAX_FM_MESH_NODES * sizeof( mesh_node_t ), "Mesh Node List" );
370 TK_OpenSource( fileName );
373 while ( goodObject == false )
375 TK_Beyond( TK_C_NAMED );
376 TK_Beyond( TK_OBJECT );
377 TK_Beyond( TK_C_TRI );
378 TK_Beyond( TK_MESH );
379 TK_BeyondRequire( TK_C_VERTICES, TK_COLON );
380 TK_FetchRequire( TK_INTNUMBER );
381 vertexCount = tk_IntNumber;
382 if ( vertexCount > 0 ) {
386 TK_BeyondRequire( TK_C_FACES, TK_COLON );
387 TK_FetchRequire( TK_INTNUMBER );
388 triCount = tk_IntNumber;
389 if ( triCount >= MAXTRIANGLES ) {
390 Error( "Too many triangles in file %s\n", fileName );
392 *triangleCount = triCount;
393 tList = (triangle_t *) SafeMalloc( MAXTRIANGLES * sizeof( triangle_t ), "Triangle list" );
396 memset( *triList,0,MAXTRIANGLES * sizeof( triangle_t ) );
397 TK_BeyondRequire( TK_C_VERTEX, TK_LIST );
399 /* rx = ((rotation[0]+90.0)/360.0)*2.0*M_PI;
400 //rx = (rotation[0]/360.0)*2.0*M_PI;
401 ry = (rotation[1]/360.0)*2.0*M_PI;
402 rz = (rotation[2]/360.0)*2.0*M_PI;
404 vList = (void *) SafeMalloc( vertexCount * sizeof vList[0], "Vertex list" );
405 for ( i = 0; i < vertexCount; i++ )
407 TK_BeyondRequire( TK_C_VERTEX, TK_INTNUMBER );
408 if ( tk_IntNumber != i ) {
409 Error( "File '%s', line %d:\nVertex index mismatch.\n",
410 tk_SourceName, tk_Line );
412 TK_FetchRequireFetch( TK_COLON );
414 TK_BeyondRequire( TK_COLON, TK_FLOATNUMBER );
416 TK_BeyondRequire( TK_COLON, TK_FLOATNUMBER );
418 TK_BeyondRequire( TK_COLON, TK_FLOATNUMBER );
421 /* x2 = x*cos(rz)+y*sin(rz);
422 y2 = -x*sin(rz)+y*cos(rz);
425 y2 = y*cos(rx)+z*sin(rx);
426 z2 = -y*sin(rx)+z*cos(rx);
429 x2 = x*cos(ry)-z*sin(ry);
430 z2 = x*sin(ry)+z*cos(ry);
438 TK_BeyondRequire( TK_C_FACE, TK_LIST );
439 for ( i = 0; i < triCount; i++ )
441 TK_BeyondRequire( TK_C_FACE, TK_INTNUMBER );
442 if ( tk_IntNumber != i ) {
443 Error( "File '%s', line %d:\nTriangle index mismatch.\n",
444 tk_SourceName, tk_Line );
446 for ( j = 0; j < 3; j++ )
448 TK_BeyondRequire( TK_IDENTIFIER, TK_COLON );
449 TK_FetchRequire( TK_INTNUMBER );
450 if ( tk_IntNumber >= vertexCount ) {
451 Error( "File '%s', line %d:\nVertex number"
452 " > vertexCount: %d\n", tk_SourceName, tk_Line,
455 tList[i].verts[2 - j][0] = vList[tk_IntNumber].v[0];
456 tList[i].verts[2 - j][1] = vList[tk_IntNumber].v[1];
457 tList[i].verts[2 - j][2] = vList[tk_IntNumber].v[2];
459 tList[i].indicies[2 - j] = tk_IntNumber;
463 /* printf("Face %i:\n v0: %f, %f, %f\n v1: %f, %f, %f\n"
464 " v2: %f, %f, %f\n", i,
465 tList[i].verts[0][0],
466 tList[i].verts[0][1],
467 tList[i].verts[0][2],
468 tList[i].verts[1][0],
469 tList[i].verts[1][1],
470 tList[i].verts[1][2],
471 tList[i].verts[2][0],
472 tList[i].verts[2][1],
473 tList[i].verts[2][2]);
477 DefaultNodesList( nodesList,num_mesh_nodes,triangleCount );