2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma hdrstop
|
2012-12-22 15:18:19 +00:00
|
|
|
#include "precompiled.h"
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
#include "Model_ma.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
======================================================================
|
|
|
|
|
|
|
|
Parses Maya ASCII files.
|
|
|
|
|
|
|
|
======================================================================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
#define MA_VERBOSE( x ) { if ( maGlobal.verbose ) { common->Printf x ; } }
|
|
|
|
|
|
|
|
// working variables used during parsing
|
2012-11-28 15:47:07 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
bool verbose;
|
2012-11-28 15:47:07 +00:00
|
|
|
maModel_t* model;
|
|
|
|
maObject_t* currentObject;
|
2012-11-26 18:58:24 +00:00
|
|
|
} ma_t;
|
|
|
|
|
|
|
|
static ma_t maGlobal;
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_ParseNodeHeader( idParser& parser, maNodeHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
memset( header, 0, sizeof( maNodeHeader_t ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( parser.ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( !token.Icmp( "-" ) )
|
|
|
|
{
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "n" ) )
|
|
|
|
{
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
strcpy( header->name, token.c_str() );
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "p" ) )
|
|
|
|
{
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
strcpy( header->parent, token.c_str() );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( !token.Icmp( ";" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseHeaderIndex( maAttribHeader_t* header, int& minIndex, int& maxIndex, const char* headerType, const char* skipString )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idParser miniParse;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
miniParse.LoadMemory( header->name, strlen( header->name ), headerType );
|
|
|
|
if( skipString )
|
|
|
|
{
|
|
|
|
miniParse.SkipUntilString( skipString );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !miniParse.SkipUntilString( "[" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
minIndex = miniParse.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
miniParse.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "]" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maxIndex = minIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maxIndex = miniParse.ParseInt();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseAttribHeader( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
idToken token;
|
|
|
|
|
|
|
|
memset( header, 0, sizeof( maAttribHeader_t ) );
|
|
|
|
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "-" ) )
|
|
|
|
{
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "s" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
header->size = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.ReadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
strcpy( header->name, token.c_str() );
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ReadVec3( idParser& parser, idVec3& vec )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !parser.SkipUntilString( "double3" ) )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Invalid Vec3", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//We need to flip y and z because of the maya coordinate system
|
|
|
|
vec.x = parser.ParseFloat();
|
|
|
|
vec.z = parser.ParseFloat();
|
|
|
|
vec.y = parser.ParseFloat();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool IsNodeComplete( idToken& token )
|
|
|
|
{
|
|
|
|
if( !token.Icmp( "createNode" ) || !token.Icmp( "connectAttr" ) || !token.Icmp( "select" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseTransform( idParser& parser )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maNodeHeader_t header;
|
|
|
|
maTransform_t* transform;
|
2012-11-28 15:47:07 +00:00
|
|
|
memset( &header, 0, sizeof( header ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate room for the transform
|
2012-11-28 15:47:07 +00:00
|
|
|
transform = ( maTransform_t* )Mem_Alloc( sizeof( maTransform_t ), TAG_MODEL );
|
|
|
|
memset( transform, 0, sizeof( maTransform_t ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
transform->scale.x = transform->scale.y = transform->scale.z = 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the header info from the transform
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ParseNodeHeader( parser, &header );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read the transform attributes
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( parser.ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( IsNodeComplete( token ) )
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !token.Icmp( "setAttr" ) )
|
|
|
|
{
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( ".t" ) )
|
|
|
|
{
|
|
|
|
if( !MA_ReadVec3( parser, transform->translate ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
transform->translate.y *= -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( !token.Icmp( ".r" ) )
|
|
|
|
{
|
|
|
|
if( !MA_ReadVec3( parser, transform->rotate ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( !token.Icmp( ".s" ) )
|
|
|
|
{
|
|
|
|
if( !MA_ReadVec3( parser, transform->scale ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
parser.SkipRestOfLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( header.parent[0] != 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//Find the parent
|
|
|
|
maTransform_t** parent;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->transforms.Get( header.parent, &parent );
|
|
|
|
if( parent )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
transform->parent = *parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add this transform to the list
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->transforms.Set( header.name, transform );
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseVertex( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the verts if this is the first attribute for verticies
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->vertexes )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numVertexes = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->vertexes = ( idVec3* )Mem_Alloc( sizeof( idVec3 ) * pMesh->numVertexes, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "VertexHeader", NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read each vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = minIndex; i <= maxIndex; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->vertexes[i].x = parser.ParseFloat();
|
|
|
|
pMesh->vertexes[i].z = parser.ParseFloat();
|
|
|
|
pMesh->vertexes[i].y = -parser.ParseFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseVertexTransforms( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the verts if this is the first attribute for verticies
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->vertTransforms )
|
|
|
|
{
|
|
|
|
if( header->size == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
header->size = 1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numVertTransforms = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->vertTransforms = ( idVec4* )Mem_Alloc( sizeof( idVec4 ) * pMesh->numVertTransforms, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->nextVertTransformIndex = 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "VertexTransformHeader", NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "-" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tk2;
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.ReadToken( &tk2 );
|
|
|
|
if( !tk2.Icmp( "type" ) )
|
|
|
|
{
|
|
|
|
parser.SkipUntilString( "float3" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &tk2 );
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read each vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = minIndex; i <= maxIndex; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->vertTransforms[pMesh->nextVertTransformIndex].x = parser.ParseFloat();
|
|
|
|
pMesh->vertTransforms[pMesh->nextVertTransformIndex].z = parser.ParseFloat();
|
|
|
|
pMesh->vertTransforms[pMesh->nextVertTransformIndex].y = -parser.ParseFloat();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//w hold the vert index
|
|
|
|
pMesh->vertTransforms[pMesh->nextVertTransformIndex].w = i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->nextVertTransformIndex++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseEdge( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the verts if this is the first attribute for verticies
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->edges )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numEdges = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->edges = ( idVec3* )Mem_Alloc( sizeof( idVec3 ) * pMesh->numEdges, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "EdgeHeader", NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read each vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = minIndex; i <= maxIndex; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->edges[i].x = parser.ParseFloat();
|
|
|
|
pMesh->edges[i].y = parser.ParseFloat();
|
|
|
|
pMesh->edges[i].z = parser.ParseFloat();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseNormal( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the verts if this is the first attribute for verticies
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->normals )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numNormals = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->normals = ( idVec3* )Mem_Alloc( sizeof( idVec3 ) * pMesh->numNormals, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "NormalHeader", NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "-" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tk2;
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.ReadToken( &tk2 );
|
|
|
|
if( !tk2.Icmp( "type" ) )
|
|
|
|
{
|
|
|
|
parser.SkipUntilString( "float3" );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &tk2 );
|
|
|
|
parser.UnreadToken( &token );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read each vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = minIndex; i <= maxIndex; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->normals[i].x = parser.ParseFloat();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Adjust the normals for the change in coordinate systems
|
|
|
|
pMesh->normals[i].z = parser.ParseFloat();
|
|
|
|
pMesh->normals[i].y = -parser.ParseFloat();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->normals[i].Normalize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->normalsParsed = true;
|
|
|
|
pMesh->nextNormal = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseFace( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the verts if this is the first attribute for verticies
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->faces )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numFaces = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->faces = ( maFace_t* )Mem_Alloc( sizeof( maFace_t ) * pMesh->numFaces, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "FaceHeader", NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read the face data
|
2012-11-28 15:47:07 +00:00
|
|
|
int currentFace = minIndex - 1;
|
|
|
|
while( parser.ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( IsNodeComplete( token ) )
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !token.Icmp( "f" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int count = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( count != 3 )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Face is not a triangle.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
//Increment the face number because a new face always starts with an "f" token
|
|
|
|
currentFace++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//We cannot reorder edges until later because the normal processing
|
|
|
|
//assumes the edges are in the original order
|
|
|
|
pMesh->faces[currentFace].edge[0] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].edge[1] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].edge[2] = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Some more init stuff
|
|
|
|
pMesh->faces[currentFace].vertexColors[0] = pMesh->faces[currentFace].vertexColors[1] = pMesh->faces[currentFace].vertexColors[2] = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "mu" ) )
|
|
|
|
{
|
|
|
|
int uvstIndex = parser.ParseInt();
|
|
|
|
uvstIndex;
|
2012-11-26 18:58:24 +00:00
|
|
|
int count = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( count != 3 )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Invalid texture coordinates.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
pMesh->faces[currentFace].tVertexNum[0] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].tVertexNum[1] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].tVertexNum[2] = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "mf" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int count = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( count != 3 )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Invalid texture coordinates.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
pMesh->faces[currentFace].tVertexNum[0] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].tVertexNum[1] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].tVertexNum[2] = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "fc" ) )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int count = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( count != 3 )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Invalid vertex color.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
pMesh->faces[currentFace].vertexColors[0] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].vertexColors[1] = parser.ParseInt();
|
|
|
|
pMesh->faces[currentFace].vertexColors[2] = parser.ParseInt();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseColor( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the verts if this is the first attribute for verticies
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->colors )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numColors = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->colors = ( byte* )Mem_Alloc( sizeof( byte ) * pMesh->numColors * 4, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "ColorHeader", NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read each vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = minIndex; i <= maxIndex; i++ )
|
|
|
|
{
|
|
|
|
pMesh->colors[i * 4] = parser.ParseFloat() * 255;
|
|
|
|
pMesh->colors[i * 4 + 1] = parser.ParseFloat() * 255;
|
|
|
|
pMesh->colors[i * 4 + 2] = parser.ParseFloat() * 255;
|
|
|
|
pMesh->colors[i * 4 + 3] = parser.ParseFloat() * 255;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseTVert( idParser& parser, maAttribHeader_t* header )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//This is not the texture coordinates. It is just the name so ignore it
|
2012-11-28 15:47:07 +00:00
|
|
|
if( strstr( header->name, "uvsn" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Allocate enough space for all the data
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !pMesh->tvertexes )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->numTVertexes = header->size;
|
2012-11-28 15:47:07 +00:00
|
|
|
pMesh->tvertexes = ( idVec2* )Mem_Alloc( sizeof( idVec2 ) * pMesh->numTVertexes, TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the start and end index for this attribute
|
|
|
|
int minIndex, maxIndex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !MA_ParseHeaderIndex( header, minIndex, maxIndex, "TextureCoordHeader", "uvsp" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//This was just a header
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "-" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tk2;
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.ReadToken( &tk2 );
|
|
|
|
if( !tk2.Icmp( "type" ) )
|
|
|
|
{
|
|
|
|
parser.SkipUntilString( "float2" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &tk2 );
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read each tvert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = minIndex; i <= maxIndex; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->tvertexes[i].x = parser.ParseFloat();
|
|
|
|
pMesh->tvertexes[i].y = 1.0f - parser.ParseFloat();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Quick check to see if the vert participates in a shared normal
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_QuickIsVertShared( int faceIndex, int vertIndex )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
int vertNum = pMesh->faces[faceIndex].vertexNum[vertIndex];
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < 3; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int edge = pMesh->faces[faceIndex].edge[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( edge < 0 )
|
|
|
|
{
|
|
|
|
edge = idMath::Fabs( edge ) - 1;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pMesh->edges[edge].z == 1 && ( pMesh->edges[edge].x == vertNum || pMesh->edges[edge].y == vertNum ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_GetSharedFace( int faceIndex, int vertIndex, int& sharedFace, int& sharedVert )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
|
|
|
int vertNum = pMesh->faces[faceIndex].vertexNum[vertIndex];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sharedFace = -1;
|
|
|
|
sharedVert = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Find a shared edge on this face that contains the specified vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int edgeIndex = 0; edgeIndex < 3; edgeIndex++ )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int edge = pMesh->faces[faceIndex].edge[edgeIndex];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( edge < 0 )
|
|
|
|
{
|
|
|
|
edge = idMath::Fabs( edge ) - 1;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( pMesh->edges[edge].z == 1 && ( pMesh->edges[edge].x == vertNum || pMesh->edges[edge].y == vertNum ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( int i = 0; i < faceIndex; i++ )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( int j = 0; j < 3; j++ )
|
|
|
|
{
|
|
|
|
if( pMesh->faces[i].vertexNum[j] == vertNum )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
sharedFace = i;
|
|
|
|
sharedVert = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( sharedFace != -1 )
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_ParseMesh( idParser& parser )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
maObject_t* object;
|
|
|
|
object = ( maObject_t* )Mem_Alloc( sizeof( maObject_t ), TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
memset( object, 0, sizeof( maObject_t ) );
|
|
|
|
maGlobal.model->objects.Append( object );
|
|
|
|
maGlobal.currentObject = object;
|
|
|
|
object->materialRef = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the header info from the mesh
|
|
|
|
maNodeHeader_t header;
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ParseNodeHeader( parser, &header );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Find my parent
|
2012-11-28 15:47:07 +00:00
|
|
|
if( header.parent[0] != 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//Find the parent
|
|
|
|
maTransform_t** parent;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->transforms.Get( header.parent, &parent );
|
|
|
|
if( parent )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maGlobal.currentObject->mesh.transform = *parent;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
strcpy( object->name, header.name );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read the transform attributes
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( parser.ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( IsNodeComplete( token ) )
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !token.Icmp( "setAttr" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maAttribHeader_t header;
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ParseAttribHeader( parser, &header );
|
|
|
|
|
|
|
|
if( strstr( header.name, ".vt" ) )
|
|
|
|
{
|
|
|
|
MA_ParseVertex( parser, &header );
|
|
|
|
}
|
|
|
|
else if( strstr( header.name, ".ed" ) )
|
|
|
|
{
|
|
|
|
MA_ParseEdge( parser, &header );
|
|
|
|
}
|
|
|
|
else if( strstr( header.name, ".pt" ) )
|
|
|
|
{
|
|
|
|
MA_ParseVertexTransforms( parser, &header );
|
|
|
|
}
|
|
|
|
else if( strstr( header.name, ".n" ) )
|
|
|
|
{
|
|
|
|
MA_ParseNormal( parser, &header );
|
|
|
|
}
|
|
|
|
else if( strstr( header.name, ".fc" ) )
|
|
|
|
{
|
|
|
|
MA_ParseFace( parser, &header );
|
|
|
|
}
|
|
|
|
else if( strstr( header.name, ".clr" ) )
|
|
|
|
{
|
|
|
|
MA_ParseColor( parser, &header );
|
|
|
|
}
|
|
|
|
else if( strstr( header.name, ".uvst" ) )
|
|
|
|
{
|
|
|
|
MA_ParseTVert( parser, &header );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
parser.SkipRestOfLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maMesh_t* pMesh = &maGlobal.currentObject->mesh;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the verts from the edge
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < pMesh->numFaces; i++ )
|
|
|
|
{
|
|
|
|
for( int j = 0; j < 3; j++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int edge = pMesh->faces[i].edge[j];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( edge < 0 )
|
|
|
|
{
|
|
|
|
edge = idMath::Fabs( edge ) - 1;
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->faces[i].vertexNum[j] = pMesh->edges[edge].y;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pMesh->faces[i].vertexNum[j] = pMesh->edges[edge].x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the normals
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pMesh->normalsParsed )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < pMesh->numFaces; i++ )
|
|
|
|
{
|
|
|
|
for( int j = 0; j < 3; j++ )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Is this vertex shared
|
|
|
|
int sharedFace = -1;
|
|
|
|
int sharedVert = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( MA_QuickIsVertShared( i, j ) )
|
|
|
|
{
|
|
|
|
MA_GetSharedFace( i, j, sharedFace, sharedVert );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( sharedFace != -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the normal from the share
|
|
|
|
pMesh->faces[i].vertexNormals[j] = pMesh->faces[sharedFace].vertexNormals[sharedVert];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//The vertex is not shared so get the next normal
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pMesh->nextNormal >= pMesh->numNormals )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//We are using more normals than exist
|
2012-11-28 15:47:07 +00:00
|
|
|
throw idException( va( "Maya Loader '%s': Invalid Normals Index.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
pMesh->faces[i].vertexNormals[j] = pMesh->normals[pMesh->nextNormal];
|
|
|
|
pMesh->nextNormal++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Now that the normals are good...lets reorder the verts to make the tris face the right way
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < pMesh->numFaces; i++ )
|
|
|
|
{
|
|
|
|
int tmp = pMesh->faces[i].vertexNum[1];
|
|
|
|
pMesh->faces[i].vertexNum[1] = pMesh->faces[i].vertexNum[2];
|
|
|
|
pMesh->faces[i].vertexNum[2] = tmp;
|
|
|
|
|
|
|
|
idVec3 tmpVec = pMesh->faces[i].vertexNormals[1];
|
|
|
|
pMesh->faces[i].vertexNormals[1] = pMesh->faces[i].vertexNormals[2];
|
|
|
|
pMesh->faces[i].vertexNormals[2] = tmpVec;
|
|
|
|
|
|
|
|
tmp = pMesh->faces[i].tVertexNum[1];
|
|
|
|
pMesh->faces[i].tVertexNum[1] = pMesh->faces[i].tVertexNum[2];
|
|
|
|
pMesh->faces[i].tVertexNum[2] = tmp;
|
|
|
|
|
|
|
|
tmp = pMesh->faces[i].vertexColors[1];
|
|
|
|
pMesh->faces[i].vertexColors[1] = pMesh->faces[i].vertexColors[2];
|
|
|
|
pMesh->faces[i].vertexColors[2] = tmp;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Now apply the pt transformations
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < pMesh->numVertTransforms; i++ )
|
|
|
|
{
|
|
|
|
pMesh->vertexes[( int )pMesh->vertTransforms[i].w] += pMesh->vertTransforms[i].ToVec3();
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_VERBOSE( ( va( "MESH %s - parent %s\n", header.name, header.parent ) ) );
|
|
|
|
MA_VERBOSE( ( va( "\tverts:%d\n", maGlobal.currentObject->mesh.numVertexes ) ) );
|
|
|
|
MA_VERBOSE( ( va( "\tfaces:%d\n", maGlobal.currentObject->mesh.numFaces ) ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_ParseFileNode( idParser& parser )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Get the header info from the node
|
|
|
|
maNodeHeader_t header;
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ParseNodeHeader( parser, &header );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Read the transform attributes
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( parser.ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( IsNodeComplete( token ) )
|
|
|
|
{
|
|
|
|
parser.UnreadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !token.Icmp( "setAttr" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maAttribHeader_t attribHeader;
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ParseAttribHeader( parser, &attribHeader );
|
|
|
|
|
|
|
|
if( strstr( attribHeader.name, ".ftn" ) )
|
|
|
|
{
|
|
|
|
parser.SkipUntilString( "string" );
|
|
|
|
parser.ReadToken( &token );
|
|
|
|
if( !token.Icmp( "(" ) )
|
|
|
|
{
|
|
|
|
parser.ReadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maFileNode_t* fileNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
fileNode = ( maFileNode_t* )Mem_Alloc( sizeof( maFileNode_t ), TAG_MODEL );
|
|
|
|
strcpy( fileNode->name, header.name );
|
|
|
|
strcpy( fileNode->path, token.c_str() );
|
|
|
|
|
|
|
|
maGlobal.model->fileNodes.Set( fileNode->name, fileNode );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
parser.SkipRestOfLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_ParseMaterialNode( idParser& parser )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
//Get the header info from the node
|
|
|
|
maNodeHeader_t header;
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ParseNodeHeader( parser, &header );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maMaterialNode_t* matNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
matNode = ( maMaterialNode_t* )Mem_Alloc( sizeof( maMaterialNode_t ), TAG_MODEL );
|
|
|
|
memset( matNode, 0, sizeof( maMaterialNode_t ) );
|
|
|
|
|
|
|
|
strcpy( matNode->name, header.name );
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->materialNodes.Set( matNode->name, matNode );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_ParseCreateNode( idParser& parser )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.ReadToken( &token );
|
|
|
|
|
|
|
|
if( !token.Icmp( "transform" ) )
|
|
|
|
{
|
|
|
|
MA_ParseTransform( parser );
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "mesh" ) )
|
|
|
|
{
|
|
|
|
MA_ParseMesh( parser );
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "file" ) )
|
|
|
|
{
|
|
|
|
MA_ParseFileNode( parser );
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "shadingEngine" ) || !token.Icmp( "lambert" ) || !token.Icmp( "phong" ) || !token.Icmp( "blinn" ) )
|
|
|
|
{
|
|
|
|
MA_ParseMaterialNode( parser );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
int MA_AddMaterial( const char* materialName )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
maMaterialNode_t** destNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->materialNodes.Get( materialName, &destNode );
|
|
|
|
if( destNode )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maMaterialNode_t* matNode = *destNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Iterate down the tree until we get a file
|
2012-11-28 15:47:07 +00:00
|
|
|
while( matNode && !matNode->file )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
matNode = matNode->child;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( matNode && matNode->file )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Got the file
|
2012-11-28 15:47:07 +00:00
|
|
|
maMaterial_t* material;
|
|
|
|
material = ( maMaterial_t* )Mem_Alloc( sizeof( maMaterial_t ), TAG_MODEL );
|
2012-11-26 18:58:24 +00:00
|
|
|
memset( material, 0, sizeof( maMaterial_t ) );
|
|
|
|
|
|
|
|
//Remove the OS stuff
|
|
|
|
idStr qPath;
|
|
|
|
qPath = fileSystem->OSPathToRelativePath( matNode->file->path );
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
strcpy( material->name, qPath.c_str() );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maGlobal.model->materials.Append( material );
|
2012-11-28 15:47:07 +00:00
|
|
|
return maGlobal.model->materials.Num() - 1;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool MA_ParseConnectAttr( idParser& parser )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
idStr temp;
|
|
|
|
idStr srcName;
|
|
|
|
idStr srcType;
|
|
|
|
idStr destName;
|
|
|
|
idStr destType;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.ReadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
temp = token;
|
2012-11-28 15:47:07 +00:00
|
|
|
int dot = temp.Find( "." );
|
|
|
|
if( dot == -1 )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Invalid Connect Attribute.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
srcName = temp.Left( dot );
|
|
|
|
srcType = temp.Right( temp.Length() - dot - 1 );
|
|
|
|
|
|
|
|
parser.ReadToken( &token );
|
2012-11-26 18:58:24 +00:00
|
|
|
temp = token;
|
2012-11-28 15:47:07 +00:00
|
|
|
dot = temp.Find( "." );
|
|
|
|
if( dot == -1 )
|
|
|
|
{
|
|
|
|
throw idException( va( "Maya Loader '%s': Invalid Connect Attribute.", parser.GetFileName() ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
destName = temp.Left( dot );
|
|
|
|
destType = temp.Right( temp.Length() - dot - 1 );
|
|
|
|
|
|
|
|
if( srcType.Find( "oc" ) != -1 )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Is this attribute a material node attribute
|
|
|
|
maMaterialNode_t** matNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->materialNodes.Get( srcName, &matNode );
|
|
|
|
if( matNode )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maMaterialNode_t** destNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->materialNodes.Get( destName, &destNode );
|
|
|
|
if( destNode )
|
|
|
|
{
|
|
|
|
( *destNode )->child = *matNode;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Is this attribute a file node
|
|
|
|
maFileNode_t** fileNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->fileNodes.Get( srcName, &fileNode );
|
|
|
|
if( fileNode )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maMaterialNode_t** destNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model->materialNodes.Get( destName, &destNode );
|
|
|
|
if( destNode )
|
|
|
|
{
|
|
|
|
( *destNode )->file = *fileNode;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( srcType.Find( "iog" ) != -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//Is this an attribute for one of our meshes
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < maGlobal.model->objects.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( !strcmp( maGlobal.model->objects[i]->name, srcName ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//maGlobal.model->objects[i]->materialRef = MA_AddMaterial(destName);
|
2012-11-28 15:47:07 +00:00
|
|
|
strcpy( maGlobal.model->objects[i]->materialName, destName );
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_BuildScale( idMat4& mat, float x, float y, float z )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
mat.Identity();
|
|
|
|
mat[0][0] = x;
|
|
|
|
mat[1][1] = y;
|
|
|
|
mat[2][2] = z;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_BuildAxisRotation( idMat4& mat, float ang, int axis )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
float sinAng = idMath::Sin( ang );
|
|
|
|
float cosAng = idMath::Cos( ang );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
mat.Identity();
|
2012-11-28 15:47:07 +00:00
|
|
|
switch( axis )
|
|
|
|
{
|
|
|
|
case 0: //x
|
|
|
|
mat[1][1] = cosAng;
|
|
|
|
mat[1][2] = sinAng;
|
|
|
|
mat[2][1] = -sinAng;
|
|
|
|
mat[2][2] = cosAng;
|
|
|
|
break;
|
|
|
|
case 1: //y
|
|
|
|
mat[0][0] = cosAng;
|
|
|
|
mat[0][2] = -sinAng;
|
|
|
|
mat[2][0] = sinAng;
|
|
|
|
mat[2][2] = cosAng;
|
|
|
|
break;
|
|
|
|
case 2://z
|
|
|
|
mat[0][0] = cosAng;
|
|
|
|
mat[0][1] = sinAng;
|
|
|
|
mat[1][0] = -sinAng;
|
|
|
|
mat[1][1] = cosAng;
|
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_ApplyTransformation( maModel_t* model )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( int i = 0; i < model->objects.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maMesh_t* mesh = &model->objects[i]->mesh;
|
|
|
|
maTransform_t* transform = mesh->transform;
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
while( transform )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idMat4 rotx, roty, rotz;
|
|
|
|
idMat4 scale;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
rotx.Identity();
|
|
|
|
roty.Identity();
|
|
|
|
rotz.Identity();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( fabs( transform->rotate.x ) > 0.0f )
|
|
|
|
{
|
|
|
|
MA_BuildAxisRotation( rotx, DEG2RAD( -transform->rotate.x ), 0 );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fabs( transform->rotate.y ) > 0.0f )
|
|
|
|
{
|
|
|
|
MA_BuildAxisRotation( roty, DEG2RAD( transform->rotate.y ), 1 );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fabs( transform->rotate.z ) > 0.0f )
|
|
|
|
{
|
|
|
|
MA_BuildAxisRotation( rotz, DEG2RAD( -transform->rotate.z ), 2 );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
MA_BuildScale( scale, transform->scale.x, transform->scale.y, transform->scale.z );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Apply the transformation to each vert
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int j = 0; j < mesh->numVertexes; j++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
mesh->vertexes[j] = scale * mesh->vertexes[j];
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
mesh->vertexes[j] = rotx * mesh->vertexes[j];
|
|
|
|
mesh->vertexes[j] = rotz * mesh->vertexes[j];
|
|
|
|
mesh->vertexes[j] = roty * mesh->vertexes[j];
|
|
|
|
|
|
|
|
mesh->vertexes[j] = mesh->vertexes[j] + transform->translate;
|
|
|
|
}
|
|
|
|
|
|
|
|
transform = transform->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
MA_Parse
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
maModel_t* MA_Parse( const char* buffer, const char* filename, bool verbose )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
memset( &maGlobal, 0, sizeof( maGlobal ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maGlobal.verbose = verbose;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maGlobal.currentObject = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// NOTE: using new operator because aseModel_t contains idList class objects
|
2012-11-28 15:47:07 +00:00
|
|
|
maGlobal.model = new( TAG_MODEL ) maModel_t;
|
2012-11-26 18:58:24 +00:00
|
|
|
maGlobal.model->objects.Resize( 32, 32 );
|
|
|
|
maGlobal.model->materials.Resize( 32, 32 );
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idParser parser;
|
2012-11-28 15:47:07 +00:00
|
|
|
parser.SetFlags( LEXFL_NOSTRINGCONCAT );
|
|
|
|
parser.LoadMemory( buffer, strlen( buffer ), filename );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( parser.ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
if( !token.Icmp( "createNode" ) )
|
|
|
|
{
|
|
|
|
MA_ParseCreateNode( parser );
|
|
|
|
}
|
|
|
|
else if( !token.Icmp( "connectAttr" ) )
|
|
|
|
{
|
|
|
|
MA_ParseConnectAttr( parser );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Resolve The Materials
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < maGlobal.model->objects.Num(); i++ )
|
|
|
|
{
|
|
|
|
maGlobal.model->objects[i]->materialRef = MA_AddMaterial( maGlobal.model->objects[i]->materialName );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//Apply Transformation
|
2012-11-28 15:47:07 +00:00
|
|
|
MA_ApplyTransformation( maGlobal.model );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return maGlobal.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
MA_Load
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
maModel_t* MA_Load( const char* fileName )
|
|
|
|
{
|
|
|
|
char* buf;
|
2012-11-26 18:58:24 +00:00
|
|
|
ID_TIME_T timeStamp;
|
2012-11-28 15:47:07 +00:00
|
|
|
maModel_t* ma;
|
|
|
|
|
|
|
|
fileSystem->ReadFile( fileName, ( void** )&buf, &timeStamp );
|
|
|
|
if( !buf )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ma = MA_Parse( buf, fileName, false );
|
|
|
|
ma->timeStamp = timeStamp;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
catch( idException& e )
|
|
|
|
{
|
|
|
|
common->Warning( "%s", e.GetError() );
|
|
|
|
if( maGlobal.model )
|
|
|
|
{
|
|
|
|
MA_Free( maGlobal.model );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
ma = NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
fileSystem->FreeFile( buf );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return ma;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
MA_Free
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void MA_Free( maModel_t* ma )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
maObject_t* obj;
|
|
|
|
maMesh_t* mesh;
|
|
|
|
maMaterial_t* material;
|
|
|
|
|
|
|
|
if( !ma )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < ma->objects.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
obj = ma->objects[i];
|
|
|
|
|
|
|
|
// free the base nesh
|
|
|
|
mesh = &obj->mesh;
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->vertexes )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->vertexes );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->vertTransforms )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->vertTransforms );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->normals )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->normals );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->tvertexes )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->tvertexes );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->edges )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->edges );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->colors )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->colors );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mesh->faces )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( mesh->faces );
|
|
|
|
}
|
|
|
|
Mem_Free( obj );
|
|
|
|
}
|
|
|
|
ma->objects.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < ma->materials.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
material = ma->materials[i];
|
|
|
|
Mem_Free( material );
|
|
|
|
}
|
|
|
|
ma->materials.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maTransform_t** trans;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < ma->transforms.Num(); i++ )
|
|
|
|
{
|
|
|
|
trans = ma->transforms.GetIndex( i );
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( *trans );
|
|
|
|
}
|
|
|
|
ma->transforms.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maFileNode_t** fileNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < ma->fileNodes.Num(); i++ )
|
|
|
|
{
|
|
|
|
fileNode = ma->fileNodes.GetIndex( i );
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( *fileNode );
|
|
|
|
}
|
|
|
|
ma->fileNodes.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
maMaterialNode_t** matNode;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < ma->materialNodes.Num(); i++ )
|
|
|
|
{
|
|
|
|
matNode = ma->materialNodes.GetIndex( i );
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( *matNode );
|
|
|
|
}
|
|
|
|
ma->materialNodes.Clear();
|
|
|
|
delete ma;
|
|
|
|
}
|