2007-11-04 03:34:51 +00:00
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoModel Library
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (c) 2002, Randy Reddig & seaw0lf
|
|
|
|
All rights reserved.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Neither the names of the copyright holders nor the names of its contributors may
|
|
|
|
be used to endorse or promote products derived from this software without
|
|
|
|
specific prior written permission.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
----------------------------------------------------------------------------- */
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* marker */
|
|
|
|
#define PM_MS3D_C
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "picointernal.h"
|
|
|
|
|
|
|
|
/* disable warnings */
|
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma warning( disable:4100 ) /* unref param */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* remarks:
|
|
|
|
* - loader seems stable
|
|
|
|
* todo:
|
|
|
|
* - fix uv coordinate problem
|
|
|
|
* - check for buffer overflows ('bufptr' accesses)
|
|
|
|
*/
|
|
|
|
/* uncomment when debugging this module */
|
|
|
|
#define DEBUG_PM_MS3D
|
|
|
|
#define DEBUG_PM_MS3D_EX
|
|
|
|
|
|
|
|
/* plain white */
|
|
|
|
static picoColor_t white = { 255,255,255,255 };
|
|
|
|
|
|
|
|
/* ms3d limits */
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MS3D_MAX_VERTS 8192
|
|
|
|
#define MS3D_MAX_TRIS 16384
|
|
|
|
#define MS3D_MAX_GROUPS 128
|
|
|
|
#define MS3D_MAX_MATERIALS 128
|
|
|
|
#define MS3D_MAX_JOINTS 128
|
|
|
|
#define MS3D_MAX_KEYFRAMES 216
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* ms3d flags */
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MS3D_SELECTED 1
|
|
|
|
#define MS3D_HIDDEN 2
|
|
|
|
#define MS3D_SELECTED2 4
|
|
|
|
#define MS3D_DIRTY 8
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* this freaky loader needs byte alignment */
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
/* ms3d header */
|
|
|
|
typedef struct SMsHeader
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
char magic[10];
|
|
|
|
int version;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsHeader;
|
|
|
|
|
|
|
|
/* ms3d vertex */
|
|
|
|
typedef struct SMsVertex
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned char flags; /* sel, sel2, or hidden */
|
|
|
|
float xyz[3];
|
|
|
|
char boneID; /* -1 means 'no bone' */
|
|
|
|
unsigned char refCount;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsVertex;
|
|
|
|
|
|
|
|
/* ms3d triangle */
|
|
|
|
typedef struct SMsTriangle
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned short flags; /* sel, sel2, or hidden */
|
|
|
|
unsigned short vertexIndices[3];
|
|
|
|
float vertexNormals[3][3];
|
|
|
|
float s[3];
|
|
|
|
float t[3];
|
|
|
|
unsigned char smoothingGroup; /* 1 - 32 */
|
|
|
|
unsigned char groupIndex;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsTriangle;
|
|
|
|
|
|
|
|
/* ms3d material */
|
|
|
|
typedef struct SMsMaterial
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
char name[32];
|
|
|
|
float ambient[4];
|
|
|
|
float diffuse[4];
|
|
|
|
float specular[4];
|
|
|
|
float emissive[4];
|
|
|
|
float shininess; /* range 0..128 */
|
|
|
|
float transparency; /* range 0..1 */
|
|
|
|
unsigned char mode;
|
|
|
|
char texture [128]; /* texture.bmp */
|
|
|
|
char alphamap[128]; /* alpha.bmp */
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsMaterial;
|
|
|
|
|
|
|
|
// ms3d group (static part)
|
|
|
|
// followed by a variable size block (see below)
|
|
|
|
typedef struct SMsGroup
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned char flags; // sel, hidden
|
|
|
|
char name[32];
|
|
|
|
unsigned short numTriangles;
|
2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned short triangleIndices[ numTriangles ];
|
|
|
|
char materialIndex; // -1 means 'no material'
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsGroup;
|
|
|
|
|
|
|
|
// ms3d joint
|
|
|
|
typedef struct SMsJoint
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned char flags;
|
|
|
|
char name[32];
|
|
|
|
char parentName[32];
|
|
|
|
float rotation[3];
|
|
|
|
float translation[3];
|
|
|
|
unsigned short numRotationKeyframes;
|
|
|
|
unsigned short numTranslationKeyframes;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsJoint;
|
|
|
|
|
|
|
|
// ms3d keyframe
|
|
|
|
typedef struct SMsKeyframe
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
float time;
|
|
|
|
float parameter[3];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TMsKeyframe;
|
|
|
|
|
|
|
|
/* restore previous data alignment */
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
/* _ms3d_canload:
|
|
|
|
* validates a milkshape3d model file.
|
|
|
|
*/
|
2012-03-17 20:01:54 +00:00
|
|
|
static int _ms3d_canload( PM_PARAMS_CANLOAD ){
|
2010-10-05 08:57:07 +00:00
|
|
|
const TMsHeader *hdr;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* sanity check */
|
2010-10-05 08:57:07 +00:00
|
|
|
if ( (size_t) bufSize < sizeof( TMsHeader ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return PICO_PMV_ERROR_SIZE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get ms3d header */
|
2010-10-05 08:57:07 +00:00
|
|
|
hdr = (const TMsHeader *)buffer;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* check ms3d magic */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( strncmp( hdr->magic,"MS3D000000",10 ) != 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return PICO_PMV_ERROR_IDENT;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* check ms3d version */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( hdr->version ) < 3 ||
|
|
|
|
_pico_little_long( hdr->version ) > 4 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_printf( PICO_ERROR,"MS3D file ignored. Only MS3D 1.3 and 1.4 is supported." );
|
|
|
|
return PICO_PMV_ERROR_VERSION;
|
|
|
|
}
|
|
|
|
/* file seems to be a valid ms3d */
|
|
|
|
return PICO_PMV_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static unsigned char *GetWord( unsigned char *bufptr, int *out ){
|
|
|
|
if ( bufptr == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
*out = _pico_little_short( *(unsigned short *)bufptr );
|
|
|
|
return( bufptr + 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* _ms3d_load:
|
|
|
|
* loads a milkshape3d model file.
|
2012-03-17 20:01:54 +00:00
|
|
|
*/
|
|
|
|
static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
|
|
|
|
picoModel_t *model;
|
2010-10-05 08:57:07 +00:00
|
|
|
unsigned char *bufptr, *bufptr0;
|
2012-03-17 20:01:54 +00:00
|
|
|
int shaderRefs[ MS3D_MAX_GROUPS ];
|
|
|
|
int numGroups;
|
|
|
|
int numMaterials;
|
2007-11-04 03:34:51 +00:00
|
|
|
// unsigned char *ptrToGroups;
|
2012-03-17 20:01:54 +00:00
|
|
|
int numVerts;
|
2007-11-04 03:34:51 +00:00
|
|
|
unsigned char *ptrToVerts;
|
2012-03-17 20:01:54 +00:00
|
|
|
int numTris;
|
2007-11-04 03:34:51 +00:00
|
|
|
unsigned char *ptrToTris;
|
2012-03-17 20:01:54 +00:00
|
|
|
int i,k,m;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* create new pico model */
|
|
|
|
model = PicoNewModel();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( model == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* do model setup */
|
|
|
|
PicoSetModelFrameNum( model, frameNum );
|
|
|
|
PicoSetModelName( model, fileName );
|
|
|
|
PicoSetModelFileName( model, fileName );
|
|
|
|
|
2010-10-05 08:57:07 +00:00
|
|
|
bufptr0 = bufptr = (picoByte_t*) _pico_alloc( bufSize );
|
|
|
|
memcpy( bufptr, buffer, bufSize );
|
2007-11-04 03:34:51 +00:00
|
|
|
/* skip header */
|
2010-10-05 08:57:07 +00:00
|
|
|
bufptr += sizeof( TMsHeader );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get number of vertices */
|
|
|
|
bufptr = GetWord( bufptr,&numVerts );
|
|
|
|
ptrToVerts = bufptr;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "NumVertices: %d\n",numVerts );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* swap verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
TMsVertex *vertex;
|
|
|
|
vertex = (TMsVertex *)bufptr;
|
|
|
|
bufptr += sizeof( TMsVertex );
|
|
|
|
|
|
|
|
vertex->xyz[ 0 ] = _pico_little_float( vertex->xyz[ 0 ] );
|
|
|
|
vertex->xyz[ 1 ] = _pico_little_float( vertex->xyz[ 1 ] );
|
|
|
|
vertex->xyz[ 2 ] = _pico_little_float( vertex->xyz[ 2 ] );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D_EX_
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Vertex: x: %f y: %f z: %f\n",
|
|
|
|
msvd[i]->vertex[0],
|
|
|
|
msvd[i]->vertex[1],
|
|
|
|
msvd[i]->vertex[2] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* get number of triangles */
|
|
|
|
bufptr = GetWord( bufptr,&numTris );
|
|
|
|
ptrToTris = bufptr;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "NumTriangles: %d\n",numTris );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* swap tris */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numTris; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
TMsTriangle *triangle;
|
|
|
|
triangle = (TMsTriangle *)bufptr;
|
|
|
|
bufptr += sizeof( TMsTriangle );
|
|
|
|
|
|
|
|
triangle->flags = _pico_little_short( triangle->flags );
|
|
|
|
|
|
|
|
/* run through all tri verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < 3; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* swap tex coords */
|
|
|
|
triangle->s[ k ] = _pico_little_float( triangle->s[ k ] );
|
|
|
|
triangle->t[ k ] = _pico_little_float( triangle->t[ k ] );
|
|
|
|
|
|
|
|
/* swap fields */
|
|
|
|
triangle->vertexIndices[ k ] = _pico_little_short( triangle->vertexIndices[ k ] );
|
|
|
|
triangle->vertexNormals[ 0 ][ k ] = _pico_little_float( triangle->vertexNormals[ 0 ][ k ] );
|
|
|
|
triangle->vertexNormals[ 1 ][ k ] = _pico_little_float( triangle->vertexNormals[ 1 ][ k ] );
|
|
|
|
triangle->vertexNormals[ 2 ][ k ] = _pico_little_float( triangle->vertexNormals[ 2 ][ k ] );
|
|
|
|
|
|
|
|
/* check for out of range indices */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( triangle->vertexIndices[ k ] >= numVerts ) {
|
|
|
|
_pico_printf( PICO_ERROR,"Vertex %d index %d out of range (%d, max %d)",i,k,triangle->vertexIndices[k],numVerts - 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
PicoFreeModel( model );
|
2010-10-05 08:57:07 +00:00
|
|
|
_pico_free( bufptr0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL; /* yuck */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* get number of groups */
|
|
|
|
bufptr = GetWord( bufptr,&numGroups );
|
|
|
|
// ptrToGroups = bufptr;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "NumGroups: %d\n",numGroups );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* run through all groups in model */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numGroups && i < MS3D_MAX_GROUPS; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
picoSurface_t *surface;
|
2012-03-17 20:01:54 +00:00
|
|
|
TMsGroup *group;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
group = (TMsGroup *)bufptr;
|
|
|
|
bufptr += sizeof( TMsGroup );
|
|
|
|
|
|
|
|
/* we ignore hidden groups */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( group->flags & MS3D_HIDDEN ) {
|
|
|
|
bufptr += ( group->numTriangles * 2 ) + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* forced null term of group name */
|
|
|
|
group->name[ 31 ] = '\0';
|
|
|
|
|
|
|
|
/* create new pico surface */
|
|
|
|
surface = PicoNewSurface( model );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( surface == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
PicoFreeModel( model );
|
2010-10-05 08:57:07 +00:00
|
|
|
_pico_free( bufptr0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* do surface setup */
|
|
|
|
PicoSetSurfaceType( surface,PICO_TRIANGLES );
|
|
|
|
PicoSetSurfaceName( surface,group->name );
|
|
|
|
|
|
|
|
/* process triangle indices */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < group->numTriangles; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
TMsTriangle *triangle;
|
|
|
|
unsigned int triangleIndex;
|
|
|
|
|
|
|
|
/* get triangle index */
|
|
|
|
bufptr = GetWord( bufptr,(int *)&triangleIndex );
|
|
|
|
|
|
|
|
/* get ptr to triangle data */
|
2012-03-17 20:01:54 +00:00
|
|
|
triangle = (TMsTriangle *)( ptrToTris + ( sizeof( TMsTriangle ) * triangleIndex ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* run through triangle vertices */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( m = 0; m < 3; m++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
TMsVertex *vertex;
|
|
|
|
unsigned int vertexIndex;
|
2012-03-17 20:01:54 +00:00
|
|
|
picoVec2_t texCoord;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get ptr to vertex data */
|
|
|
|
vertexIndex = triangle->vertexIndices[ m ];
|
2012-03-17 20:01:54 +00:00
|
|
|
vertex = (TMsVertex *)( ptrToVerts + ( sizeof( TMsVertex ) * vertexIndex ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* store vertex origin */
|
|
|
|
PicoSetSurfaceXYZ( surface,vertexIndex,vertex->xyz );
|
|
|
|
|
|
|
|
/* store vertex color */
|
|
|
|
PicoSetSurfaceColor( surface,0,vertexIndex,white );
|
|
|
|
|
|
|
|
/* store vertex normal */
|
|
|
|
PicoSetSurfaceNormal( surface,vertexIndex,triangle->vertexNormals[ m ] );
|
|
|
|
|
|
|
|
/* store current face vertex index */
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceIndex( surface,( k * 3 + ( 2 - m ) ),(picoIndex_t)vertexIndex );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get texture vertex coord */
|
|
|
|
texCoord[ 0 ] = triangle->s[ m ];
|
2012-03-17 20:01:54 +00:00
|
|
|
texCoord[ 1 ] = -triangle->t[ m ]; /* flip t */
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* store texture vertex coord */
|
|
|
|
PicoSetSurfaceST( surface,0,vertexIndex,texCoord );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* store material */
|
|
|
|
shaderRefs[ i ] = *bufptr++;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Group %d: '%s' (%d tris)\n",i,group->name,group->numTriangles );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* get number of materials */
|
|
|
|
bufptr = GetWord( bufptr,&numMaterials );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "NumMaterials: %d\n",numMaterials );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* run through all materials in model */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numMaterials; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
picoShader_t *shader;
|
2012-03-17 20:01:54 +00:00
|
|
|
picoColor_t ambient,diffuse,specular;
|
2007-11-04 03:34:51 +00:00
|
|
|
TMsMaterial *material;
|
2012-03-17 20:01:54 +00:00
|
|
|
int k;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
material = (TMsMaterial *)bufptr;
|
|
|
|
bufptr += sizeof( TMsMaterial );
|
|
|
|
|
|
|
|
/* null term strings */
|
2012-03-17 20:01:54 +00:00
|
|
|
material->name [ 31 ] = '\0';
|
2007-11-04 03:34:51 +00:00
|
|
|
material->texture [ 127 ] = '\0';
|
|
|
|
material->alphamap[ 127 ] = '\0';
|
|
|
|
|
|
|
|
/* ltrim strings */
|
|
|
|
_pico_strltrim( material->name );
|
|
|
|
_pico_strltrim( material->texture );
|
|
|
|
_pico_strltrim( material->alphamap );
|
|
|
|
|
|
|
|
/* rtrim strings */
|
|
|
|
_pico_strrtrim( material->name );
|
|
|
|
_pico_strrtrim( material->texture );
|
|
|
|
_pico_strrtrim( material->alphamap );
|
|
|
|
|
|
|
|
/* create new pico shader */
|
|
|
|
shader = PicoNewShader( model );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( shader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
PicoFreeModel( model );
|
2010-10-05 08:57:07 +00:00
|
|
|
_pico_free( bufptr0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* scale shader colors */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < 4; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ambient [ k ] = (picoByte_t) ( material->ambient[ k ] * 255 );
|
|
|
|
diffuse [ k ] = (picoByte_t) ( material->diffuse[ k ] * 255 );
|
|
|
|
specular[ k ] = (picoByte_t) ( material->specular[ k ] * 255 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/* set shader colors */
|
|
|
|
PicoSetShaderAmbientColor( shader,ambient );
|
|
|
|
PicoSetShaderDiffuseColor( shader,diffuse );
|
|
|
|
PicoSetShaderSpecularColor( shader,specular );
|
|
|
|
|
|
|
|
/* set shader transparency */
|
|
|
|
PicoSetShaderTransparency( shader,material->transparency );
|
|
|
|
|
|
|
|
/* set shader shininess (0..127) */
|
|
|
|
PicoSetShaderShininess( shader,material->shininess );
|
|
|
|
|
|
|
|
/* set shader name */
|
|
|
|
PicoSetShaderName( shader,material->name );
|
|
|
|
|
|
|
|
/* set shader texture map name */
|
|
|
|
PicoSetShaderMapName( shader,material->texture );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Material %d: '%s' ('%s','%s')\n",i,material->name,material->texture,material->alphamap );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* assign shaders to surfaces */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numGroups && i < MS3D_MAX_GROUPS; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
picoSurface_t *surface;
|
|
|
|
picoShader_t *shader;
|
|
|
|
|
|
|
|
/* sanity check */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( shaderRefs[ i ] >= MS3D_MAX_MATERIALS ||
|
|
|
|
shaderRefs[ i ] < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get surface */
|
|
|
|
surface = PicoGetModelSurface( model,i );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( surface == NULL ) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get shader */
|
|
|
|
shader = PicoGetModelShader( model,shaderRefs[ i ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( shader == NULL ) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* assign shader */
|
|
|
|
PicoSetSurfaceShader( surface,shader );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_MS3D
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Mapped: %d ('%s') to %d (%s)\n",
|
|
|
|
shaderRefs[i],shader->name,i,surface->name );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* return allocated pico model */
|
2010-10-05 08:57:07 +00:00
|
|
|
_pico_free( bufptr0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
return model;
|
|
|
|
// return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pico file format module definition */
|
|
|
|
const picoModule_t picoModuleMS3D =
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
"0.4-a", /* module version string */
|
|
|
|
"Milkshape 3D", /* module display name */
|
|
|
|
"seaw0lf", /* author's name */
|
|
|
|
"2002 seaw0lf", /* module copyright */
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
"ms3d",NULL,NULL,NULL /* default extensions to use */
|
2007-11-04 03:34:51 +00:00
|
|
|
},
|
2012-03-17 20:01:54 +00:00
|
|
|
_ms3d_canload, /* validation routine */
|
|
|
|
_ms3d_load, /* load routine */
|
|
|
|
NULL, /* save validation routine */
|
|
|
|
NULL /* save routine */
|
2007-11-04 03:34:51 +00:00
|
|
|
};
|