2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
// l3dslib.c: library for loading triangles from an Alias triangle file
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "inout.h"
|
|
|
|
#include "mathlib.h"
|
|
|
|
#include "trilib.h"
|
|
|
|
#include "l3dslib.h"
|
|
|
|
|
|
|
|
#define MAIN3DS 0x4D4D
|
|
|
|
#define EDIT3DS 0x3D3D // this is the start of the editor config
|
|
|
|
#define EDIT_OBJECT 0x4000
|
|
|
|
#define OBJ_TRIMESH 0x4100
|
|
|
|
#define TRI_VERTEXL 0x4110
|
|
|
|
#define TRI_FACEL1 0x4120
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAXVERTS 2000
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2012-03-17 20:01:54 +00:00
|
|
|
int v[4];
|
2007-11-04 03:34:51 +00:00
|
|
|
} tri;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
float fverts[MAXVERTS][3];
|
|
|
|
tri tris[MAXTRIANGLES];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int bytesread, level, numtris, totaltris;
|
|
|
|
int vertsfound, trisfound;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
triangle_t *ptri;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Alias stores triangles as 3 explicit vertices in .tri files, so even though we
|
|
|
|
// start out with a vertex pool and vertex indices for triangles, we have to convert
|
|
|
|
// to raw, explicit triangles
|
2012-03-17 20:01:54 +00:00
|
|
|
void StoreAliasTriangles( void ){
|
|
|
|
int i, j, k;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( totaltris + numtris ) > MAXTRIANGLES ) {
|
|
|
|
Error( "Error: Too many triangles" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numtris ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 3 ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0 ; k < 3 ; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ptri[i + totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
totaltris += numtris;
|
|
|
|
numtris = 0;
|
|
|
|
vertsfound = 0;
|
|
|
|
trisfound = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int ParseVertexL( FILE *input ){
|
|
|
|
int i, j, startbytesread, numverts;
|
|
|
|
unsigned short tshort;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertsfound ) {
|
|
|
|
Error( "Error: Multiple vertex chunks" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
vertsfound = 1;
|
|
|
|
startbytesread = bytesread;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &tshort, sizeof( tshort ), 1, input );
|
|
|
|
bytesread += sizeof( tshort );
|
2007-11-04 03:34:51 +00:00
|
|
|
numverts = (int)tshort;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numverts > MAXVERTS ) {
|
|
|
|
Error( "Error: Too many vertices" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numverts ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 3 ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &fverts[i][j], sizeof( float ), 1, input );
|
|
|
|
bytesread += sizeof( float );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertsfound && trisfound ) {
|
|
|
|
StoreAliasTriangles();
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return bytesread - startbytesread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int ParseFaceL1( FILE *input ){
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int i, j, startbytesread;
|
|
|
|
unsigned short tshort;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( trisfound ) {
|
|
|
|
Error( "Error: Multiple face chunks" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
trisfound = 1;
|
|
|
|
startbytesread = bytesread;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &tshort, sizeof( tshort ), 1, input );
|
|
|
|
bytesread += sizeof( tshort );
|
2007-11-04 03:34:51 +00:00
|
|
|
numtris = (int)tshort;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numtris > MAXTRIANGLES ) {
|
|
|
|
Error( "Error: Too many triangles" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numtris ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 4 ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &tshort, sizeof( tshort ), 1, input );
|
|
|
|
bytesread += sizeof( tshort );
|
2007-11-04 03:34:51 +00:00
|
|
|
tris[i].v[j] = (int)tshort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertsfound && trisfound ) {
|
|
|
|
StoreAliasTriangles();
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return bytesread - startbytesread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int ParseChunk( FILE *input ){
|
|
|
|
#define BLOCK_SIZE 4096
|
|
|
|
char temp[BLOCK_SIZE];
|
|
|
|
unsigned short type;
|
|
|
|
int i, length, w, t, retval;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
level++;
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
// chunk type
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &type, sizeof( type ), 1, input );
|
|
|
|
bytesread += sizeof( type );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// chunk length
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &length, sizeof( length ), 1, input );
|
|
|
|
bytesread += sizeof( length );
|
2007-11-04 03:34:51 +00:00
|
|
|
w = length - 6;
|
|
|
|
|
|
|
|
// process chunk if we care about it, otherwise skip it
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( type )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
case TRI_VERTEXL:
|
2012-03-17 20:01:54 +00:00
|
|
|
w -= ParseVertexL( input );
|
2007-11-04 03:34:51 +00:00
|
|
|
goto ParseSubchunk;
|
|
|
|
|
|
|
|
case TRI_FACEL1:
|
2012-03-17 20:01:54 +00:00
|
|
|
w -= ParseFaceL1( input );
|
2007-11-04 03:34:51 +00:00
|
|
|
goto ParseSubchunk;
|
|
|
|
|
|
|
|
case EDIT_OBJECT:
|
2012-03-17 20:01:54 +00:00
|
|
|
// read the name
|
2007-11-04 03:34:51 +00:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &temp[i], 1, 1, input );
|
2007-11-04 03:34:51 +00:00
|
|
|
i++;
|
|
|
|
w--;
|
|
|
|
bytesread++;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( temp[i - 1] );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
case MAIN3DS:
|
|
|
|
case OBJ_TRIMESH:
|
|
|
|
case EDIT3DS:
|
2012-03-17 20:01:54 +00:00
|
|
|
// parse through subchunks
|
2007-11-04 03:34:51 +00:00
|
|
|
ParseSubchunk:
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( w > 0 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
w -= ParseChunk( input );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = length;
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
default:
|
2012-03-17 20:01:54 +00:00
|
|
|
// skip other chunks
|
|
|
|
while ( w > 0 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
t = w;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( t > BLOCK_SIZE ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
t = BLOCK_SIZE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( feof( input ) ) {
|
|
|
|
Error( "Error: unexpected end of file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &temp, t, 1, input );
|
2007-11-04 03:34:51 +00:00
|
|
|
bytesread += t;
|
|
|
|
|
|
|
|
w -= t;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = length;
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Done:
|
|
|
|
level--;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Load3DSTriangleList( char *filename, triangle_t **pptri, int *numtriangles ){
|
2007-11-04 03:34:51 +00:00
|
|
|
FILE *input;
|
2012-03-17 20:01:54 +00:00
|
|
|
short int tshort;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
bytesread = 0;
|
|
|
|
level = 0;
|
|
|
|
numtris = 0;
|
|
|
|
totaltris = 0;
|
|
|
|
vertsfound = 0;
|
|
|
|
trisfound = 0;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( input = fopen( filename, "rb" ) ) == 0 ) {
|
|
|
|
fprintf( stderr,"reader: could not open file '%s'\n", filename );
|
|
|
|
exit( 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( &tshort, sizeof( tshort ), 1, input );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// should only be MAIN3DS, but some files seem to start with EDIT3DS, with
|
|
|
|
// no MAIN3DS
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( tshort != MAIN3DS ) && ( tshort != EDIT3DS ) ) {
|
|
|
|
fprintf( stderr,"File is not a 3DS file.\n" );
|
|
|
|
exit( 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// back to top of file so we can parse the first chunk descriptor
|
2012-03-17 20:01:54 +00:00
|
|
|
fseek( input, 0, SEEK_SET );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ptri = malloc( MAXTRIANGLES * sizeof( triangle_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
*pptri = ptri;
|
|
|
|
|
|
|
|
// parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
|
|
|
|
// OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
|
2012-03-17 20:01:54 +00:00
|
|
|
ParseChunk( input );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertsfound || trisfound ) {
|
|
|
|
Error( "Incomplete triangle set" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
*numtriangles = totaltris;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( input );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|