4 static void Load3DS( const char *filename, _3DS_t *p3DS, qboolean verbose );
6 static qboolean s_verbose;
8 #define MAX_MATERIALS 100
9 #define MAX_NAMED_OBJECTS 100
10 #define MAX_MESH_MATERIAL_GROUPS 100
11 #define MAX_TRI_OBJECTS 512
13 static char s_buffer[1000000];
15 static int ReadString( FILE *fp, char *buffer )
22 fread( &buffer[i], 1, sizeof( char ), fp );
24 } while ( buffer[i++] != 0 );
30 static int ReadChunkAndLength( FILE *fp, short *chunk, long *len )
32 if ( fread( chunk, sizeof( short ), 1, fp ) != 1 )
34 if ( fread( len, sizeof( long ), 1, fp ) != 1 )
35 Error( "Unexpected EOF found" );
39 static void LoadMapName( FILE *fp, char *buffer, int thisChunkLen )
41 unsigned short chunkID;
45 while ( ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
49 case _3DS_CHUNK_MAT_MAPNAME:
50 fread( buffer, chunkLen - 6, 1, fp );
53 fread( s_buffer, chunkLen - 6, 1, fp );
56 bytesRead += chunkLen;
57 if ( bytesRead >= thisChunkLen )
62 static void LoadMaterialList( FILE *fp, long thisChunkLen, _3DSMaterial_t *pMat )
65 unsigned short chunkID;
71 memset( &mat, 0, sizeof( mat ) );
74 printf( " >>> MATERIAL LIST\n" );
76 while ( ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
80 case _3DS_CHUNK_MAT_NAME:
81 fread( mat.name, chunkLen - 6, 1, fp );
83 printf( " found mat name '%s'\n", mat.name );
85 case _3DS_CHUNK_TEXMAP:
86 LoadMapName( fp, mat.texture, chunkLen - 6 );
88 printf( " found texture '%s'\n", mat.texture );
90 case _3DS_CHUNK_SPECMAP:
91 LoadMapName( fp, mat.specular, chunkLen - 6 );
93 printf( " found specular map '%s'\n", mat.specular );
95 case _3DS_CHUNK_OPACMAP:
96 LoadMapName( fp, mat.opacity, chunkLen - 6 );
98 printf( " found opacity map '%s'\n", mat.opacity );
100 case _3DS_CHUNK_REFLMAP:
101 LoadMapName( fp, mat.reflection, chunkLen - 6 );
103 printf( " found reflection map '%s'\n", mat.reflection );
105 case _3DS_CHUNK_BUMPMAP:
106 LoadMapName( fp, mat.bump, chunkLen - 6 );
108 printf( " found bump map '%s'\n", mat.bump );
111 fread( s_buffer, chunkLen - 6, 1, fp );
115 bytesRead += chunkLen;
117 if ( bytesRead >= thisChunkLen )
123 if ( mat.texture[0] )
125 sprintf( buffer, "%s%s", curdir, mat.texture );
126 if ( strstr( buffer, gamedir + 1 ) )
127 strcpy( mat.texture, strstr( buffer, gamedir + 1 ) + strlen( gamedir ) - 1 );
129 strcpy( mat.texture, buffer );
132 if ( mat.specular[0] )
134 sprintf( buffer, "%s%s", curdir, mat.specular );
135 if ( strstr( buffer, gamedir + 1 ) )
136 strcpy( mat.specular, strstr( buffer, gamedir + 1 ) + strlen( gamedir ) - 1 );
138 strcpy( mat.specular, buffer );
143 sprintf( buffer, "%s%s", curdir, mat.bump );
144 if ( strstr( buffer, gamedir + 1 ) )
145 strcpy( mat.bump, strstr( buffer, gamedir + 1 ) + strlen( gamedir ) - 1 );
147 strcpy( mat.bump, buffer );
150 if ( mat.reflection[0] )
152 sprintf( buffer, "%s%s", curdir, mat.reflection );
153 if ( strstr( buffer, gamedir + 1 ) )
154 strcpy( mat.reflection, strstr( buffer, gamedir + 1 ) + strlen( gamedir ) - 1 );
156 strcpy( mat.reflection, buffer );
159 if ( mat.opacity[0] )
161 sprintf( buffer, "%s%s", curdir, mat.opacity );
162 if ( strstr( buffer, gamedir + 1 ) )
163 strcpy( mat.opacity, strstr( buffer, gamedir + 1 ) + strlen( gamedir ) - 1 );
165 strcpy( mat.opacity, buffer );
171 static void LoadMeshMaterialGroup( FILE *fp, long thisChunkLen, _3DSMeshMaterialGroup_t *pMMG )
173 _3DSMeshMaterialGroup_t mmg;
175 memset( &mmg, 0, sizeof( mmg ) );
177 ReadString( fp, mmg.name );
179 fread( &mmg.numFaces, sizeof( mmg.numFaces ), 1, fp );
180 mmg.pFaces = malloc( sizeof( mmg.pFaces[0] ) * mmg.numFaces );
181 fread( mmg.pFaces, sizeof( mmg.pFaces[0] ), mmg.numFaces, fp );
185 printf( " >>> MESH MATERIAL GROUP '%s' (%d faces)\n", mmg.name, mmg.numFaces );
190 for ( i = 0; i < mmg.numFaces; i++ )
192 printf( " %d\n", mmg.pFaces[i] );
200 static void LoadNamedTriObject( FILE *fp, long thisChunkLen, _3DSTriObject_t *pTO )
203 unsigned short chunkID;
206 _3DSTriObject_t triObj;
207 _3DSMeshMaterialGroup_t meshMaterialGroups[MAX_MESH_MATERIAL_GROUPS];
208 int numMeshMaterialGroups = 0;
210 memset( &triObj, 0, sizeof( triObj ) );
213 printf( " >>> NAMED TRI OBJECT\n" );
215 while ( ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
219 case _3DS_CHUNK_MSH_MAT_GROUP:
220 LoadMeshMaterialGroup( fp, chunkLen - 6, &meshMaterialGroups[numMeshMaterialGroups] );
221 bytesRead += chunkLen;
222 numMeshMaterialGroups++;
224 case _3DS_CHUNK_FACE_ARRAY:
225 fread( &triObj.numFaces, sizeof( triObj.numFaces ), 1, fp );
226 assert( triObj.pFaces == 0 );
228 triObj.pFaces = malloc( sizeof( triObj.pFaces[0] ) * triObj.numFaces );
229 fread( triObj.pFaces, sizeof( triObj.pFaces[0] ), triObj.numFaces, fp );
230 bytesRead += sizeof( triObj.numFaces ) + triObj.numFaces * sizeof( triObj.pFaces[0] ) + 6;
234 printf( " found face array with %d faces\n", triObj.numFaces );
235 for ( i = 0; i < triObj.numFaces; i++ )
237 printf( " %d: %d,%d,%d\n", i, triObj.pFaces[i].a, triObj.pFaces[i].b, triObj.pFaces[i].c );
242 case _3DS_CHUNK_POINT_ARRAY:
243 fread( &triObj.numPoints, sizeof( triObj.numPoints ), 1, fp );
244 triObj.pPoints = malloc( sizeof( triObj.pPoints[0] ) * triObj.numPoints );
245 fread( triObj.pPoints, sizeof( triObj.pPoints[0] ), triObj.numPoints, fp );
246 bytesRead += sizeof( triObj.numPoints ) + triObj.numPoints * sizeof( triObj.pPoints[0] ) + 6;
248 // flip points around into our coordinate system
249 for ( i = 0; i < triObj.numPoints; i++ )
253 x = triObj.pPoints[i].x;
254 y = triObj.pPoints[i].y;
255 z = triObj.pPoints[i].z;
257 triObj.pPoints[i].x = -y;
258 triObj.pPoints[i].y = x;
259 triObj.pPoints[i].z = z;
264 printf( " found point array with %d points\n", triObj.numPoints );
265 for ( i = 0; i < triObj.numPoints; i++ )
267 printf( " %d: %f,%f,%f\n", i, triObj.pPoints[i].x, triObj.pPoints[i].y, triObj.pPoints[i].z );
271 case _3DS_CHUNK_TEX_VERTS:
272 fread( &triObj.numTexVerts, sizeof( triObj.numTexVerts ), 1, fp );
273 triObj.pTexVerts = malloc( sizeof( triObj.pTexVerts[0] ) * triObj.numTexVerts );
274 fread( triObj.pTexVerts, sizeof( triObj.pTexVerts[0] ), triObj.numTexVerts, fp );
275 bytesRead += sizeof( triObj.numTexVerts ) + sizeof( triObj.pTexVerts[0] ) * triObj.numTexVerts + 6;
279 printf( " found tex vert array with %d tex verts\n", triObj.numTexVerts );
280 for ( i = 0; i < triObj.numTexVerts; i++ )
282 printf( " %d: %f,%f\n", i, triObj.pTexVerts[i].s, triObj.pTexVerts[i].t );
287 fread( s_buffer, chunkLen - 6, 1, fp );
288 bytesRead += chunkLen;
292 if ( bytesRead >= thisChunkLen )
297 if ( numMeshMaterialGroups == 0 )
299 numMeshMaterialGroups = 1;
300 strcpy( meshMaterialGroups[0].name, "(null)" );
301 if ( pTO->numTexVerts ) {
302 printf( "Warning: assigning (null) skin to tri object\n" );
307 assert( pTO->numFaces == meshMaterialGroups[0].numFaces );
310 pTO->pMeshMaterialGroups = malloc( sizeof( _3DSMeshMaterialGroup_t ) * numMeshMaterialGroups );
311 memcpy( pTO->pMeshMaterialGroups, meshMaterialGroups, numMeshMaterialGroups * sizeof( meshMaterialGroups[0] ) );
312 pTO->numMeshMaterialGroups = numMeshMaterialGroups;
317 assert( numMeshMaterialGroups <= 1 );
320 static void LoadNamedObject( FILE *fp, long thisChunkLen, _3DSNamedObject_t *pNO )
323 unsigned short chunkID;
327 _3DSTriObject_t triObj[MAX_TRI_OBJECTS];
328 int numTriObjects = 0;
330 memset( triObj, 0, sizeof( triObj ) );
332 bytesRead += ReadString( fp, name );
335 printf( " >>> NAMED OBJECT '%s'\n", name );
337 while ( ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
341 case _3DS_CHUNK_NAMED_TRI_OBJECT:
342 LoadNamedTriObject( fp, chunkLen - 6, &triObj[numTriObjects] );
346 fread( s_buffer, chunkLen - 6, 1, fp );
350 bytesRead += chunkLen;
352 if ( bytesRead >= thisChunkLen )
356 strcpy( pNO->name, name );
357 pNO->pTriObjects = malloc( sizeof( _3DSTriObject_t ) * numTriObjects );
358 memcpy( pNO->pTriObjects, triObj, sizeof( triObj[0] ) * numTriObjects );
359 pNO->numTriObjects = numTriObjects;
361 assert( numTriObjects <= 1 );
364 static void LoadEditChunk( FILE *fp, long thisChunkLen, _3DSEditChunk_t *pEC )
366 unsigned short chunkID;
369 _3DSEditChunk_t editChunk;
371 _3DSMaterial_t mat[MAX_MATERIALS];
372 _3DSNamedObject_t namedObjects[MAX_NAMED_OBJECTS];
374 int numMaterials = 0, numNamedObjects = 0;
376 memset( &editChunk, 0, sizeof( editChunk ) );
379 printf( ">>> EDIT CHUNK\n" );
381 while ( ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
385 case _3DS_CHUNK_MAT_LIST:
386 LoadMaterialList( fp, chunkLen - 6, &mat[numMaterials] );
389 case _3DS_CHUNK_NAMED_OBJECT:
390 LoadNamedObject( fp, chunkLen - 6, &namedObjects[numNamedObjects] );
391 if ( namedObjects[numNamedObjects].numTriObjects != 0 )
394 case _3DS_CHUNK_MESH_VERSION:
396 fread( s_buffer, chunkLen - 6, 1, fp );
400 bytesRead += chunkLen;
402 if ( bytesRead >= thisChunkLen )
406 if ( numMaterials == 0 )
409 strcpy( mat[0].name, "(null)" );
410 printf( "Warning: no material definitions found\n" );
413 pEC->numNamedObjects = numNamedObjects;
415 pEC->pMaterials = malloc( sizeof( _3DSMaterial_t ) * numMaterials );
416 pEC->pNamedObjects = malloc( sizeof( _3DSNamedObject_t ) * numNamedObjects );
418 memcpy( pEC->pMaterials, mat, numMaterials * sizeof( mat[0] ) );
419 memcpy( pEC->pNamedObjects, namedObjects, numNamedObjects * sizeof( namedObjects[0] ) );
422 static void Load3DS( const char *filename, _3DS_t *p3DS, qboolean verbose )
425 unsigned short chunkID;
427 _3DSEditChunk_t editChunk;
431 if ( ( fp = fopen( filename, "rb" ) ) == 0 )
432 Error( "Unable to open '%s'", filename );
435 if ( ( fread( &chunkID, sizeof( short ), 1, fp ) != 1 ) ||
436 ( LittleShort( chunkID ) != _3DS_CHUNK_MAGIC ) )
438 Error( "Missing or incorrect magic number in '%s'", filename );
440 if ( fread( &chunkLen, sizeof( chunkLen ), 1, fp ) != 1 )
441 Error( "Unexpected EOF encountered in '%s'", filename );
443 if ( !ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
444 Error( "Missing version number in '%s'", filename );
445 if ( fread( s_buffer, chunkLen - 6, 1, fp ) != 1 )
446 Error( "Unexpected EOF encountered in '%s'", filename );
448 while ( ReadChunkAndLength( fp, &chunkID, &chunkLen ) )
452 case _3DS_CHUNK_EDIT:
453 LoadEditChunk( fp, chunkLen - 6, &editChunk );
455 case _3DS_CHUNK_KEYFRAME_DATA:
456 fread( s_buffer, chunkLen - 6, 1, fp );
459 fread( s_buffer, chunkLen - 6, 1, fp );
466 p3DS->editChunk = editChunk;
469 static void ComputeNormals( _3DSTriObject_t *pTO, triangle_t *pTris )
471 vec3_t faceNormals[POLYSET_MAXTRIANGLES];
472 vec3_t vertexNormals[POLYSET_MAXTRIANGLES*3];
473 vec3_t side0, side1, facenormal;
476 memset( faceNormals, 0, sizeof( faceNormals ) );
477 memset( vertexNormals, 0, sizeof( vertexNormals ) );
480 // compute face normals
482 for ( f = 0; f < pTO->numFaces; f++ )
484 VectorSubtract( pTris[f].verts[0], pTris[f].verts[1], side0 );
485 VectorSubtract( pTris[f].verts[2], pTris[f].verts[1], side1 );
487 CrossProduct( side0, side1, facenormal );
488 VectorNormalize( facenormal, faceNormals[f] );
492 // sum vertex normals
494 for ( v = 0; v < pTO->numPoints; v++ )
496 for ( f = 0; f < pTO->numFaces; f++ )
498 if ( ( pTO->pFaces[f].a == v ) ||
499 ( pTO->pFaces[f].b == v ) ||
500 ( pTO->pFaces[f].c == v ) )
502 vertexNormals[v][0] += faceNormals[f][0];
503 vertexNormals[v][1] += faceNormals[f][1];
504 vertexNormals[v][2] += faceNormals[f][2];
508 VectorNormalize( vertexNormals[v], vertexNormals[v] );
512 // copy vertex normals into triangles
514 for ( f = 0; f < pTO->numFaces; f++ )
516 int i0 = pTO->pFaces[f].c;
517 int i1 = pTO->pFaces[f].b;
518 int i2 = pTO->pFaces[f].a;
520 VectorCopy( vertexNormals[i0], pTris[f].normals[0] );
521 VectorCopy( vertexNormals[i1], pTris[f].normals[1] );
522 VectorCopy( vertexNormals[i2], pTris[f].normals[2] );
527 ** void _3DS_LoadPolysets
529 void _3DS_LoadPolysets( const char *filename, polyset_t **ppPSET, int *numpsets, qboolean verbose )
534 triangle_t *ptri, *triangles;
538 memset( &_3ds, 0, sizeof( _3ds ) );
539 Load3DS( filename, &_3ds, verbose );
541 // compute information
542 numPolysets = _3ds.editChunk.numNamedObjects;
545 pPSET = calloc( 1, numPolysets * sizeof( polyset_t ) );
546 triangles = ptri = calloc( 1, POLYSET_MAXTRIANGLES * sizeof( triangle_t ) );
548 // copy the data over
549 for ( i = 0; i < numPolysets; i++ )
551 char matnamebuf[1024];
554 _3DSTriObject_t *pTO = &_3ds.editChunk.pNamedObjects[i].pTriObjects[0];
556 pPSET[i].triangles = ptri;
557 pPSET[i].numtriangles = pTO->numFaces;
558 strcpy( pPSET[i].name, _3ds.editChunk.pNamedObjects[i].name );
560 strcpy( matnamebuf, filename );
561 if ( strrchr( matnamebuf, '/' ) )
562 *( strrchr( matnamebuf, '/' ) + 1 )= 0;
563 strcat( matnamebuf, pTO->pMeshMaterialGroups[0].name );
565 if ( strstr( matnamebuf, gamedir ) )
566 strcpy( pPSET[i].materialname, strstr( matnamebuf, gamedir ) + strlen( gamedir ) );
568 strcpy( pPSET[i].materialname, pTO->pMeshMaterialGroups[0].name );
570 assert( pPSET[i].numtriangles < POLYSET_MAXTRIANGLES );
572 for ( tri = ptri, j = 0; j < pPSET[i].numtriangles; j++ )
574 int i0 = pTO->pFaces[j].c;
575 int i1 = pTO->pFaces[j].b;
576 int i2 = pTO->pFaces[j].a;
578 tri->verts[0][0] = pTO->pPoints[i0].x;
579 tri->verts[0][1] = pTO->pPoints[i0].y;
580 tri->verts[0][2] = pTO->pPoints[i0].z;
582 tri->verts[1][0] = pTO->pPoints[i1].x;
583 tri->verts[1][1] = pTO->pPoints[i1].y;
584 tri->verts[1][2] = pTO->pPoints[i1].z;
586 tri->verts[2][0] = pTO->pPoints[i2].x;
587 tri->verts[2][1] = pTO->pPoints[i2].y;
588 tri->verts[2][2] = pTO->pPoints[i2].z;
590 for ( k = 0; k < 3; k++ )
592 tri->colors[0][k] = 1;
593 tri->colors[1][k] = 1;
594 tri->colors[2][k] = 1;
598 if ( pTO->pTexVerts )
600 tri->texcoords[0][0] = pTO->pTexVerts[i0].s;
601 tri->texcoords[0][1] = 1.0f - pTO->pTexVerts[i0].t;
602 tri->texcoords[1][0] = pTO->pTexVerts[i1].s;
603 tri->texcoords[1][1] = 1.0f - pTO->pTexVerts[i1].t;
604 tri->texcoords[2][0] = pTO->pTexVerts[i2].s;
605 tri->texcoords[2][1] = 1.0f - pTO->pTexVerts[i2].t;
611 ptri += pPSET[i].numtriangles;
612 assert( ptri - triangles < POLYSET_MAXTRIANGLES );
615 // compute normal data
617 for ( i = 0; i < numPolysets; i++ )
619 // unique vertices based solely on vertex position
620 ComputeNormals( &_3ds.editChunk.pNamedObjects[i].pTriObjects[0],
621 pPSET[i].triangles );
625 free( _3ds.editChunk.pMaterials );
626 free( _3ds.editChunk.pNamedObjects );
629 *numpsets = numPolysets;