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
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Nurail: Used pm_md3.c (Randy Reddig) as a template.
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* marker */
|
|
|
|
#define PM_FM_C
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "pm_fm.h"
|
|
|
|
|
|
|
|
//#define FM_VERBOSE_DBG 0
|
|
|
|
#undef FM_VERBOSE_DBG
|
|
|
|
#undef FM_DBG
|
|
|
|
|
|
|
|
typedef struct index_LUT_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
short Vert;
|
|
|
|
short ST;
|
|
|
|
struct index_LUT_s *next;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
} index_LUT_t;
|
|
|
|
|
|
|
|
typedef struct index_DUP_LUT_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
short ST;
|
|
|
|
short OldVert;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
} index_DUP_LUT_t;
|
|
|
|
|
|
|
|
|
|
|
|
// _fm_canload()
|
2012-03-17 20:01:54 +00:00
|
|
|
static int _fm_canload( PM_PARAMS_CANLOAD ){
|
|
|
|
fm_t fm;
|
|
|
|
unsigned char *bb;
|
|
|
|
int fm_file_pos;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
bb = (unsigned char *) buffer;
|
|
|
|
|
|
|
|
// Header
|
|
|
|
fm.fm_header_hdr = (fm_chunk_header_t *) bb;
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos = sizeof( fm_chunk_header_t ) + fm.fm_header_hdr->size;
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_VERBOSE_DBG
|
|
|
|
_pico_printf( PICO_VERBOSE, "IDENT: %s\n", (unsigned char *) fm.fm_header_hdr->ident );
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( strcmp( fm.fm_header_hdr->ident, FM_HEADERCHUNKNAME ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Header Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_IDENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check fm
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_header_hdr->version ) != FM_HEADERCHUNKVER ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Header Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skin
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_skin_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t ) + fm.fm_skin_hdr->size;
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_VERBOSE_DBG
|
|
|
|
_pico_printf( PICO_VERBOSE, "SKIN: %s\n", (unsigned char *) fm.fm_skin_hdr->ident );
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( strcmp( fm.fm_skin_hdr->ident, FM_SKINCHUNKNAME ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Skin Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_IDENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check fm
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_skin_hdr->version ) != FM_SKINCHUNKVER ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Skin Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// st
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_st_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t ) + fm.fm_st_hdr->size;
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_VERBOSE_DBG
|
|
|
|
_pico_printf( PICO_VERBOSE, "ST: %s\n", (unsigned char *) fm.fm_st_hdr->ident );
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( strcmp( fm.fm_st_hdr->ident, FM_STCOORDCHUNKNAME ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM ST Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_IDENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check fm
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_st_hdr->version ) != FM_STCOORDCHUNKVER ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM ST Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tri
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_tri_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t ) + fm.fm_tri_hdr->size;
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_VERBOSE_DBG
|
|
|
|
_pico_printf( PICO_VERBOSE, "TRI: %s\n", (unsigned char *) fm.fm_tri_hdr->ident );
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( strcmp( fm.fm_tri_hdr->ident, FM_TRISCHUNKNAME ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Tri Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_IDENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check fm
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_tri_hdr->version ) != FM_TRISCHUNKVER ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Tri Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// frame
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_frame_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t );
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_VERBOSE_DBG
|
|
|
|
_pico_printf( PICO_VERBOSE, "FRAME: %s\n", (unsigned char *) fm.fm_frame_hdr->ident );
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( strcmp( fm.fm_frame_hdr->ident, FM_FRAMESCHUNKNAME ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Frame Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_IDENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check fm
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_frame_hdr->version ) != FM_FRAMESCHUNKVER ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_WARNING, "FM Frame Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return PICO_PMV_ERROR_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// file seems to be a valid fm
|
|
|
|
return PICO_PMV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// _fm_load() loads a Heretic 2 model file.
|
2012-03-17 20:01:54 +00:00
|
|
|
static picoModel_t *_fm_load( PM_PARAMS_LOAD ){
|
|
|
|
int i, j, dups, dup_index;
|
|
|
|
int fm_file_pos;
|
|
|
|
short tot_numVerts;
|
|
|
|
index_LUT_t *p_index_LUT, *p_index_LUT2, *p_index_LUT3;
|
|
|
|
index_DUP_LUT_t *p_index_LUT_DUPS;
|
|
|
|
|
|
|
|
fm_vert_normal_t *vert;
|
|
|
|
|
|
|
|
char skinname[FM_SKINPATHSIZE];
|
|
|
|
fm_t fm;
|
|
|
|
fm_header_t *fm_head;
|
|
|
|
fm_st_t *texCoord;
|
|
|
|
fm_xyz_st_t *tri_verts;
|
|
|
|
fm_xyz_st_t *triangle;
|
|
|
|
fm_frame_t *frame;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
picoByte_t *bb;
|
2012-03-17 20:01:54 +00:00
|
|
|
picoModel_t *picoModel;
|
|
|
|
picoSurface_t *picoSurface;
|
|
|
|
picoShader_t *picoShader;
|
|
|
|
picoVec3_t xyz, normal;
|
|
|
|
picoVec2_t st;
|
|
|
|
picoColor_t color;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// fm loading
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, "Loading \"%s\"", fileName );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
bb = (picoByte_t*) buffer;
|
|
|
|
|
|
|
|
// Header Header
|
|
|
|
fm.fm_header_hdr = (fm_chunk_header_t *) bb;
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos = sizeof( fm_chunk_header_t ) + fm.fm_header_hdr->size;
|
|
|
|
if ( ( strcmp( fm.fm_header_hdr->ident, FM_HEADERCHUNKNAME ) ) ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Header Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_header_hdr->version ) != FM_HEADERCHUNKVER ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Header Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skin Header
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_skin_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t ) + fm.fm_skin_hdr->size;
|
|
|
|
if ( ( strcmp( fm.fm_skin_hdr->ident, FM_SKINCHUNKNAME ) ) ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Skin Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_skin_hdr->version ) != FM_SKINCHUNKVER ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Skin Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ST Header
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_st_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t ) + fm.fm_st_hdr->size;
|
|
|
|
if ( ( strcmp( fm.fm_st_hdr->ident, FM_STCOORDCHUNKNAME ) ) ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM ST Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_st_hdr->version ) != FM_STCOORDCHUNKVER ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM ST Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tris Header
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_tri_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t ) + fm.fm_tri_hdr->size;
|
|
|
|
if ( ( strcmp( fm.fm_tri_hdr->ident, FM_TRISCHUNKNAME ) ) ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Tri Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_tri_hdr->version ) != FM_TRISCHUNKVER ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Tri Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Frame Header
|
2012-03-17 20:01:54 +00:00
|
|
|
fm.fm_frame_hdr = (fm_chunk_header_t *) ( bb + fm_file_pos );
|
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t );
|
|
|
|
if ( ( strcmp( fm.fm_frame_hdr->ident, FM_FRAMESCHUNKNAME ) ) ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Frame Ident incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_little_long( fm.fm_frame_hdr->version ) != FM_FRAMESCHUNKVER ) {
|
|
|
|
_pico_printf( PICO_WARNING, "FM Frame Version incorrect\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos = sizeof( fm_chunk_header_t );
|
|
|
|
fm_head = fm.fm_header = (fm_header_t *) ( bb + fm_file_pos );
|
2007-11-04 03:34:51 +00:00
|
|
|
fm_file_pos += fm.fm_header_hdr->size;
|
|
|
|
|
|
|
|
// Skin
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t );
|
|
|
|
fm.fm_skin = (fm_skinpath_t *) ( bb + fm_file_pos );
|
2007-11-04 03:34:51 +00:00
|
|
|
fm_file_pos += fm.fm_skin_hdr->size;
|
|
|
|
|
|
|
|
// ST
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t );
|
|
|
|
texCoord = fm.fm_st = (fm_st_t *) ( bb + fm_file_pos );
|
2007-11-04 03:34:51 +00:00
|
|
|
fm_file_pos += fm.fm_st_hdr->size;
|
|
|
|
|
|
|
|
// Tri
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t );
|
|
|
|
tri_verts = fm.fm_tri = (fm_xyz_st_t *) ( bb + fm_file_pos );
|
2007-11-04 03:34:51 +00:00
|
|
|
fm_file_pos += fm.fm_tri_hdr->size;
|
|
|
|
|
|
|
|
// Frame
|
2012-03-17 20:01:54 +00:00
|
|
|
fm_file_pos += sizeof( fm_chunk_header_t );
|
|
|
|
frame = fm.fm_frame = (fm_frame_t *) ( bb + fm_file_pos );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// do frame check
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fm_head->numFrames < 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_printf( PICO_ERROR, "%s has 0 frames!", fileName );
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( frameNum < 0 || frameNum >= fm_head->numFrames ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_printf( PICO_ERROR, "Invalid or out-of-range FM frame specified" );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// swap fm
|
|
|
|
fm_head->skinWidth = _pico_little_long( fm_head->skinWidth );
|
|
|
|
fm_head->skinHeight = _pico_little_long( fm_head->skinHeight );
|
|
|
|
fm_head->frameSize = _pico_little_long( fm_head->frameSize );
|
|
|
|
|
|
|
|
fm_head->numSkins = _pico_little_long( fm_head->numSkins );
|
|
|
|
fm_head->numXYZ = _pico_little_long( fm_head->numXYZ );
|
|
|
|
fm_head->numST = _pico_little_long( fm_head->numST );
|
|
|
|
fm_head->numTris = _pico_little_long( fm_head->numTris );
|
|
|
|
fm_head->numGLCmds = _pico_little_long( fm_head->numGLCmds );
|
|
|
|
fm_head->numFrames = _pico_little_long( fm_head->numFrames );
|
|
|
|
|
|
|
|
// swap frame scale and translation
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
frame->header.scale[ i ] = _pico_little_float( frame->header.scale[ i ] );
|
|
|
|
frame->header.translate[ i ] = _pico_little_float( frame->header.translate[ i ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// swap triangles
|
|
|
|
triangle = tri_verts;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->numTris; i++, triangle++ )
|
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
|
|
|
{
|
|
|
|
triangle->index_xyz[ j ] = _pico_little_short( triangle->index_xyz[ j ] );
|
|
|
|
triangle->index_st[ j ] = _pico_little_short( triangle->index_st[ j ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// swap st coords
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->numST; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
texCoord->s = _pico_little_short( texCoord[i].s );
|
|
|
|
texCoord->t = _pico_little_short( texCoord[i].t );
|
|
|
|
}
|
|
|
|
// set Skin Name
|
2013-05-03 06:57:29 +00:00
|
|
|
strncpy( skinname, (char *) fm.fm_skin, FM_SKINPATHSIZE );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
|
|
|
// Print out md2 values
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_VERBOSE,"numSkins->%d numXYZ->%d numST->%d numTris->%d numFrames->%d\nSkin Name \"%s\"\n", fm_head->numSkins, fm_head->numXYZ, fm_head->numST, fm_head->numTris, fm_head->numFrames, &skinname );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// detox Skin name
|
|
|
|
_pico_setfext( skinname, "" );
|
|
|
|
_pico_unixify( skinname );
|
|
|
|
|
|
|
|
/* create new pico model */
|
|
|
|
picoModel = PicoNewModel();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( picoModel == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_printf( PICO_ERROR, "Unable to allocate a new model" );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do model setup */
|
|
|
|
PicoSetModelFrameNum( picoModel, frameNum );
|
|
|
|
PicoSetModelNumFrames( picoModel, fm_head->numFrames ); /* sea */
|
|
|
|
PicoSetModelName( picoModel, fileName );
|
|
|
|
PicoSetModelFileName( picoModel, fileName );
|
|
|
|
|
|
|
|
// allocate new pico surface
|
|
|
|
picoSurface = PicoNewSurface( picoModel );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( picoSurface == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
|
|
|
|
PicoFreeModel( picoModel );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PicoSetSurfaceType( picoSurface, PICO_TRIANGLES );
|
|
|
|
PicoSetSurfaceName( picoSurface, frame->header.name );
|
|
|
|
picoShader = PicoNewShader( picoModel );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( picoShader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
|
|
|
|
PicoFreeModel( picoModel );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PicoSetShaderName( picoShader, skinname );
|
|
|
|
|
|
|
|
// associate current surface with newly created shader
|
|
|
|
PicoSetSurfaceShader( picoSurface, picoShader );
|
|
|
|
|
|
|
|
// Init LUT for Verts
|
2012-03-17 20:01:54 +00:00
|
|
|
p_index_LUT = (index_LUT_t *)_pico_alloc( sizeof( index_LUT_t ) * fm_head->numXYZ );
|
|
|
|
for ( i = 0; i < fm_head->numXYZ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p_index_LUT[i].Vert = -1;
|
|
|
|
p_index_LUT[i].ST = -1;
|
|
|
|
p_index_LUT[i].next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in Look Up Table, and allocate/fill Linked List from vert array as needed for dup STs per Vert.
|
|
|
|
tot_numVerts = fm_head->numXYZ;
|
|
|
|
dups = 0;
|
|
|
|
triangle = tri_verts;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->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
|
|
|
if ( p_index_LUT[triangle->index_xyz[j]].ST == -1 ) { // No Main Entry
|
2007-11-04 03:34:51 +00:00
|
|
|
p_index_LUT[triangle->index_xyz[j]].ST = triangle->index_st[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( triangle->index_st[j] == p_index_LUT[triangle->index_xyz[j]].ST ) { // Equal to Main Entry
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, "-> Tri #%d, Vert %d:\t XYZ:%d ST:%d\n", i, j, triangle->index_xyz[j], triangle->index_st[j] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( ( p_index_LUT[triangle->index_xyz[j]].next == NULL ) ) { // Not equal to Main entry, and no LL entry
|
|
|
|
// Add first entry of LL from Main
|
|
|
|
p_index_LUT2 = (index_LUT_t *)_pico_alloc( sizeof( index_LUT_t ) );
|
|
|
|
if ( p_index_LUT2 == NULL ) {
|
|
|
|
_pico_printf( PICO_NORMAL, " Couldn't allocate memory!\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
p_index_LUT[triangle->index_xyz[j]].next = (index_LUT_t *)p_index_LUT2;
|
|
|
|
p_index_LUT2->Vert = dups;
|
|
|
|
p_index_LUT2->ST = triangle->index_st[j];
|
|
|
|
p_index_LUT2->next = NULL;
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, " ADDING first LL XYZ:%d DUP:%d ST:%d\n", triangle->index_xyz[j], dups, triangle->index_st[j] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
triangle->index_xyz[j] = dups + fm_head->numXYZ; // Make change in Tri hunk
|
|
|
|
dups++;
|
|
|
|
}
|
|
|
|
else // Try to find in LL from Main Entry
|
|
|
|
{
|
|
|
|
p_index_LUT3 = p_index_LUT2 = p_index_LUT[triangle->index_xyz[j]].next;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( ( p_index_LUT2 != NULL ) && ( triangle->index_xyz[j] != p_index_LUT2->Vert ) ) // Walk down LL
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p_index_LUT3 = p_index_LUT2;
|
|
|
|
p_index_LUT2 = p_index_LUT2->next;
|
|
|
|
}
|
|
|
|
p_index_LUT2 = p_index_LUT3;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( triangle->index_st[j] == p_index_LUT2->ST ) { // Found it
|
2007-11-04 03:34:51 +00:00
|
|
|
triangle->index_xyz[j] = p_index_LUT2->Vert + fm_head->numXYZ; // Make change in Tri hunk
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, "--> Tri #%d, Vert %d:\t XYZ:%d ST:%d\n", i, j, triangle->index_xyz[j], triangle->index_st[j] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p_index_LUT2->next == NULL ) { // Didn't find it. Add entry to LL.
|
2007-11-04 03:34:51 +00:00
|
|
|
// Add the Entry
|
2012-03-17 20:01:54 +00:00
|
|
|
p_index_LUT3 = (index_LUT_t *)_pico_alloc( sizeof( index_LUT_t ) );
|
|
|
|
if ( p_index_LUT3 == NULL ) {
|
|
|
|
_pico_printf( PICO_NORMAL, " Couldn't allocate memory!\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
p_index_LUT2->next = (index_LUT_t *)p_index_LUT3;
|
|
|
|
p_index_LUT3->Vert = dups;
|
|
|
|
p_index_LUT3->ST = triangle->index_st[j];
|
|
|
|
p_index_LUT3->next = NULL;
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, " ADDING additional LL XYZ:%d DUP:%d NewXYZ:%d ST:%d\n", triangle->index_xyz[j], dups, dups + ( fm_head->numXYZ ), triangle->index_st[j] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
triangle->index_xyz[j] = dups + fm_head->numXYZ; // Make change in Tri hunk
|
|
|
|
dups++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, "---> Tri #%d, Vert %d:\t XYZ:%d ST:%d\n", i, j, triangle->index_xyz[j], triangle->index_st[j] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
triangle++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// malloc and build array for Dup STs
|
2012-03-17 20:01:54 +00:00
|
|
|
p_index_LUT_DUPS = (index_DUP_LUT_t *)_pico_alloc( sizeof( index_DUP_LUT_t ) * dups );
|
|
|
|
if ( p_index_LUT_DUPS == NULL ) {
|
|
|
|
_pico_printf( PICO_NORMAL, " Couldn't allocate memory!\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
dup_index = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->numXYZ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p_index_LUT2 = p_index_LUT[i].next;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( p_index_LUT2 != NULL )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p_index_LUT_DUPS[p_index_LUT2->Vert].OldVert = i;
|
|
|
|
p_index_LUT_DUPS[p_index_LUT2->Vert].ST = p_index_LUT2->ST;
|
|
|
|
dup_index++;
|
|
|
|
p_index_LUT2 = p_index_LUT2->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, " Dups = %d\n", dups );
|
|
|
|
_pico_printf( PICO_NORMAL, " Dup Index = %d\n", dup_index );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->numXYZ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, "Vert: %4d\t%4d",i, p_index_LUT[i].ST );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p_index_LUT[i].next != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
p_index_LUT2 = p_index_LUT[i].next;
|
|
|
|
do {
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, " %4d %4d", p_index_LUT2->Vert, p_index_LUT2->ST );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
p_index_LUT2 = p_index_LUT2->next;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( p_index_LUT2 != NULL );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_NORMAL, "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FM_VERBOSE_DBG
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < dup_index; i++ )
|
|
|
|
_pico_printf( PICO_NORMAL, " Dup Index #%d OldVert: %d ST: %d\n", i, p_index_LUT_DUPS[i].OldVert, p_index_LUT_DUPS[i].ST );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
triangle = tri_verts;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->numTris; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < 3; j++ )
|
|
|
|
_pico_printf( PICO_NORMAL, "Tri #%d, Vert %d:\t XYZ:%d ST:%d\n", i, j, triangle->index_xyz[j], triangle->index_st[j] );
|
|
|
|
_pico_printf( PICO_NORMAL, "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
triangle++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Build Picomodel
|
|
|
|
triangle = tri_verts;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < fm_head->numTris; j++, triangle++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceIndex( picoSurface, j * 3, triangle->index_xyz[0] );
|
|
|
|
PicoSetSurfaceIndex( picoSurface, j * 3 + 1, triangle->index_xyz[1] );
|
|
|
|
PicoSetSurfaceIndex( picoSurface, j * 3 + 2, triangle->index_xyz[2] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vert = (fm_vert_normal_t*) ( (picoByte_t*) ( frame->verts ) );
|
|
|
|
for ( i = 0; i < fm_head->numXYZ; i++, vert++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* set vertex origin */
|
|
|
|
xyz[ 0 ] = vert->v[0] * frame->header.scale[0] + frame->header.translate[0];
|
|
|
|
xyz[ 1 ] = vert->v[1] * frame->header.scale[1] + frame->header.translate[1];
|
|
|
|
xyz[ 2 ] = vert->v[2] * frame->header.scale[2] + frame->header.translate[2];
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceXYZ( picoSurface, i, xyz );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set normal */
|
|
|
|
normal[ 0 ] = fm_normals[vert->lightnormalindex][0];
|
|
|
|
normal[ 1 ] = fm_normals[vert->lightnormalindex][1];
|
|
|
|
normal[ 2 ] = fm_normals[vert->lightnormalindex][2];
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceNormal( picoSurface, i, normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set st coords */
|
2012-03-17 20:01:54 +00:00
|
|
|
st[ 0 ] = ( ( texCoord[p_index_LUT[i].ST].s ) / ( (float)fm_head->skinWidth ) );
|
|
|
|
st[ 1 ] = ( texCoord[p_index_LUT[i].ST].t / ( (float)fm_head->skinHeight ) );
|
|
|
|
PicoSetSurfaceST( picoSurface, 0, i, st );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dups ) {
|
|
|
|
for ( i = 0; i < dups; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
j = p_index_LUT_DUPS[i].OldVert;
|
|
|
|
/* set vertex origin */
|
|
|
|
xyz[ 0 ] = frame->verts[j].v[0] * frame->header.scale[0] + frame->header.translate[0];
|
|
|
|
xyz[ 1 ] = frame->verts[j].v[1] * frame->header.scale[1] + frame->header.translate[1];
|
|
|
|
xyz[ 2 ] = frame->verts[j].v[2] * frame->header.scale[2] + frame->header.translate[2];
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceXYZ( picoSurface, i + fm_head->numXYZ, xyz );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set normal */
|
|
|
|
normal[ 0 ] = fm_normals[frame->verts[j].lightnormalindex][0];
|
|
|
|
normal[ 1 ] = fm_normals[frame->verts[j].lightnormalindex][1];
|
|
|
|
normal[ 2 ] = fm_normals[frame->verts[j].lightnormalindex][2];
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceNormal( picoSurface, i + fm_head->numXYZ, normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set st coords */
|
2012-03-17 20:01:54 +00:00
|
|
|
st[ 0 ] = ( ( texCoord[p_index_LUT_DUPS[i].ST].s ) / ( (float)fm_head->skinWidth ) );
|
|
|
|
st[ 1 ] = ( texCoord[p_index_LUT_DUPS[i].ST].t / ( (float)fm_head->skinHeight ) );
|
|
|
|
PicoSetSurfaceST( picoSurface, 0, i + fm_head->numXYZ, st );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set color */
|
|
|
|
PicoSetSurfaceColor( picoSurface, 0, 0, color );
|
|
|
|
|
|
|
|
// Free up malloc'ed LL entries
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < fm_head->numXYZ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p_index_LUT[i].next != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
p_index_LUT2 = p_index_LUT[i].next;
|
|
|
|
do {
|
|
|
|
p_index_LUT3 = p_index_LUT2->next;
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_free( p_index_LUT2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
p_index_LUT2 = p_index_LUT3;
|
|
|
|
dups--;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( p_index_LUT2 != NULL );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dups ) {
|
|
|
|
_pico_printf( PICO_WARNING, " Not all LL mallocs freed\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// Free malloc'ed LUTs
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_free( p_index_LUT );
|
|
|
|
_pico_free( p_index_LUT_DUPS );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* return the new pico model */
|
|
|
|
return picoModel;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* pico file format module definition */
|
|
|
|
const picoModule_t picoModuleFM =
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
"0.85", /* module version string */
|
|
|
|
"Heretic 2 FM", /* module display name */
|
|
|
|
"Nurail", /* author's name */
|
|
|
|
"2003 Nurail", /* module copyright */
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
"fm", NULL, NULL, NULL /* default extensions to use */
|
2007-11-04 03:34:51 +00:00
|
|
|
},
|
2012-03-17 20:01:54 +00:00
|
|
|
_fm_canload, /* validation routine */
|
|
|
|
_fm_load, /* load routine */
|
|
|
|
NULL, /* save validation routine */
|
|
|
|
NULL /* save routine */
|
2007-11-04 03:34:51 +00:00
|
|
|
};
|