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_LWO_C
/* dependencies */
# include "picointernal.h"
# include "lwo/lwo2.h"
/* uncomment when debugging this module */
/*#define DEBUG_PM_LWO*/
# ifdef DEBUG_PM_LWO
# include "time.h"
# endif
/* helper functions */
2012-03-17 20:01:54 +00:00
static const char * lwo_lwIDToStr ( unsigned int lwID ) {
2007-11-04 03:34:51 +00:00
static char lwIDStr [ 5 ] ;
2012-03-17 20:01:54 +00:00
if ( ! lwID ) {
2007-11-04 03:34:51 +00:00
return " n/a " ;
}
2012-03-17 20:01:54 +00:00
lwIDStr [ 0 ] = ( char ) ( ( lwID ) > > 24 ) ;
lwIDStr [ 1 ] = ( char ) ( ( lwID ) > > 16 ) ;
lwIDStr [ 2 ] = ( char ) ( ( lwID ) > > 8 ) ;
lwIDStr [ 3 ] = ( char ) ( ( lwID ) ) ;
2007-11-04 03:34:51 +00:00
lwIDStr [ 4 ] = ' \0 ' ;
return lwIDStr ;
}
/*
2012-03-17 20:01:54 +00:00
_lwo_canload ( )
validates a LightWave Object model file . btw , i use the
preceding underscore cause it ' s a static func referenced
by one structure only .
*/
static int _lwo_canload ( PM_PARAMS_CANLOAD ) {
2007-11-04 03:34:51 +00:00
picoMemStream_t * s ;
unsigned int failID = 0 ;
int failpos = - 1 ;
int ret ;
/* create a new pico memorystream */
2010-10-05 08:57:07 +00:00
s = _pico_new_memstream ( ( const picoByte_t * ) buffer , bufSize ) ;
2012-03-17 20:01:54 +00:00
if ( s = = NULL ) {
2007-11-04 03:34:51 +00:00
return PICO_PMV_ERROR_MEMORY ;
}
ret = lwValidateObject ( fileName , s , & failID , & failpos ) ;
_pico_free_memstream ( s ) ;
return ret ;
}
/*
2012-03-17 20:01:54 +00:00
_lwo_load ( )
loads a LightWave Object model file .
*/
static picoModel_t * _lwo_load ( PM_PARAMS_LOAD ) {
picoMemStream_t * s ;
unsigned int failID = 0 ;
int failpos = - 1 ;
lwObject * obj ;
lwSurface * surface ;
lwLayer * layer ;
lwPoint * pt ;
lwPolygon * pol ;
lwPolVert * v ;
lwVMapPt * vm ;
2016-12-09 01:57:39 +00:00
char name [ 256 ] ;
2012-03-17 20:01:54 +00:00
int i , j , k , numverts ;
picoModel_t * picoModel ;
picoSurface_t * picoSurface ;
picoShader_t * picoShader ;
picoVec3_t xyz , normal ;
picoVec2_t st ;
picoColor_t color ;
int defaultSTAxis [ 2 ] ;
picoVec2_t defaultXYZtoSTScale ;
2007-11-04 03:34:51 +00:00
picoVertexCombinationHash_t * * hashTable ;
2012-03-17 20:01:54 +00:00
picoVertexCombinationHash_t * vertexCombinationHash ;
2007-11-04 03:34:51 +00:00
# ifdef DEBUG_PM_LWO
clock_t load_start , load_finish , convert_start , convert_finish ;
double load_elapsed , convert_elapsed ;
load_start = clock ( ) ;
# endif
/* do frame check */
2012-03-17 20:01:54 +00:00
if ( frameNum < 0 | | frameNum > = 1 ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_ERROR , " Invalid or out-of-range LWO frame specified " ) ;
return NULL ;
}
/* create a new pico memorystream */
2010-10-05 08:57:07 +00:00
s = _pico_new_memstream ( ( const picoByte_t * ) buffer , bufSize ) ;
2012-03-17 20:01:54 +00:00
if ( s = = NULL ) {
2007-11-04 03:34:51 +00:00
return NULL ;
}
obj = lwGetObject ( fileName , s , & failID , & failpos ) ;
_pico_free_memstream ( s ) ;
2012-03-17 20:01:54 +00:00
if ( ! obj ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_ERROR , " Couldn't load LWO file, failed on ID '%s', position %d " , lwo_lwIDToStr ( failID ) , failpos ) ;
return NULL ;
}
# ifdef DEBUG_PM_LWO
convert_start = load_finish = clock ( ) ;
2012-03-17 20:01:54 +00:00
load_elapsed = ( double ) ( load_finish - load_start ) / CLOCKS_PER_SEC ;
2007-11-04 03:34:51 +00:00
# endif
/* -------------------------------------------------
2012-03-17 20:01:54 +00:00
pico model creation
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2007-11-04 03:34:51 +00:00
/* create a 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 , 1 ) ;
PicoSetModelName ( picoModel , fileName ) ;
PicoSetModelFileName ( picoModel , fileName ) ;
/* create all polygons from layer[ 0 ] that belong to this surface */
layer = & obj - > layer [ 0 ] ;
/* warn the user that other layers are discarded */
2012-03-17 20:01:54 +00:00
if ( obj - > nlayers > 1 ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_WARNING , " LWO loader discards any geometry data not in Layer 1 (%d layers found) " , obj - > nlayers ) ;
}
/* initialize dummy normal */
normal [ 0 ] = normal [ 1 ] = normal [ 2 ] = 0.f ;
/* setup default st map */
2012-03-17 20:01:54 +00:00
st [ 0 ] = st [ 1 ] = 0.f ; /* st[0] holds max, st[1] holds max par one */
2007-11-04 03:34:51 +00:00
defaultSTAxis [ 0 ] = 0 ;
defaultSTAxis [ 1 ] = 1 ;
2012-03-17 20:01:54 +00:00
for ( i = 0 ; i < 3 ; i + + )
2007-11-04 03:34:51 +00:00
{
float min = layer - > bbox [ i ] ;
float max = layer - > bbox [ i + 3 ] ;
float size = max - min ;
2012-03-17 20:01:54 +00:00
if ( size > st [ 0 ] ) {
2007-11-04 03:34:51 +00:00
defaultSTAxis [ 1 ] = defaultSTAxis [ 0 ] ;
defaultSTAxis [ 0 ] = i ;
st [ 1 ] = st [ 0 ] ;
st [ 0 ] = size ;
}
2012-03-17 20:01:54 +00:00
else if ( size > st [ 1 ] ) {
2007-11-04 03:34:51 +00:00
defaultSTAxis [ 1 ] = i ;
st [ 1 ] = size ;
}
}
defaultXYZtoSTScale [ 0 ] = 4.f / st [ 0 ] ;
defaultXYZtoSTScale [ 1 ] = 4.f / st [ 1 ] ;
/* LWO surfaces become pico surfaces */
surface = obj - > surf ;
2012-03-17 20:01:54 +00:00
while ( surface )
2007-11-04 03:34:51 +00:00
{
/* 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 ) ;
lwFreeObject ( obj ) ;
return NULL ;
}
/* LWO model surfaces are all triangle meshes */
PicoSetSurfaceType ( picoSurface , PICO_TRIANGLES ) ;
/* set surface name */
PicoSetSurfaceName ( picoSurface , surface - > name ) ;
/* create new pico shader */
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 ) ;
lwFreeObject ( obj ) ;
return NULL ;
}
/* detox and set shader name */
2012-03-17 20:01:54 +00:00
strncpy ( name , surface - > name , sizeof ( name ) ) ;
2016-12-09 01:57:39 +00:00
_pico_first_token ( name ) ;
2007-11-04 03:34:51 +00:00
_pico_setfext ( name , " " ) ;
_pico_unixify ( name ) ;
PicoSetShaderName ( picoShader , name ) ;
/* associate current surface with newly created shader */
PicoSetSurfaceShader ( picoSurface , picoShader ) ;
/* copy indices and vertex data */
numverts = 0 ;
hashTable = PicoNewVertexCombinationHashTable ( ) ;
2012-03-17 20:01:54 +00:00
if ( hashTable = = NULL ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_ERROR , " Unable to allocate hash table " ) ;
PicoFreeModel ( picoModel ) ;
lwFreeObject ( obj ) ;
return NULL ;
}
2012-03-17 20:01:54 +00:00
for ( i = 0 , pol = layer - > polygon . pol ; i < layer - > polygon . count ; i + + , pol + + )
2007-11-04 03:34:51 +00:00
{
/* does this polygon belong to this surface? */
2012-03-17 20:01:54 +00:00
if ( pol - > surf ! = surface ) {
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
/* we only support polygons of the FACE type */
2012-03-17 20:01:54 +00:00
if ( pol - > type ! = ID_FACE ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_WARNING , " LWO loader discarded a polygon because it's type != FACE (%s) " , lwo_lwIDToStr ( pol - > type ) ) ;
continue ;
}
/* NOTE: LWO has support for non-convex polygons, do we want to store them as well? */
2012-03-17 20:01:54 +00:00
if ( pol - > nverts ! = 3 ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_WARNING , " LWO loader discarded a polygon because it has != 3 verts (%d) " , pol - > nverts ) ;
continue ;
}
2012-03-17 20:01:54 +00:00
for ( j = 0 , v = pol - > v ; j < 3 ; j + + , v + + )
2007-11-04 03:34:51 +00:00
{
pt = & layer - > point . pt [ v - > index ] ;
/* setup data */
xyz [ 0 ] = pt - > pos [ 0 ] ;
xyz [ 1 ] = pt - > pos [ 2 ] ;
xyz [ 2 ] = pt - > pos [ 1 ] ;
2016-12-09 01:57:39 +00:00
///* doom3 lwo data doesn't seem to have smoothing-angle information */
//#if 0
2016-12-19 22:45:54 +00:00
if ( surface - > smooth < = 0 ) {
/* use face normals */
2007-11-04 03:34:51 +00:00
normal [ 0 ] = v - > norm [ 0 ] ;
normal [ 1 ] = v - > norm [ 2 ] ;
normal [ 2 ] = v - > norm [ 1 ] ;
2016-12-09 01:57:39 +00:00
}
else
//#endif
{
/* smooth normals later */
normal [ 0 ] = 0 ;
normal [ 1 ] = 0 ;
normal [ 2 ] = 0 ;
}
2007-11-04 03:34:51 +00:00
st [ 0 ] = xyz [ defaultSTAxis [ 0 ] ] * defaultXYZtoSTScale [ 0 ] ;
st [ 1 ] = xyz [ defaultSTAxis [ 1 ] ] * defaultXYZtoSTScale [ 1 ] ;
2012-03-17 20:01:54 +00:00
color [ 0 ] = ( picoByte_t ) ( surface - > color . rgb [ 0 ] * surface - > diffuse . val * 0xFF ) ;
color [ 1 ] = ( picoByte_t ) ( surface - > color . rgb [ 1 ] * surface - > diffuse . val * 0xFF ) ;
color [ 2 ] = ( picoByte_t ) ( surface - > color . rgb [ 2 ] * surface - > diffuse . val * 0xFF ) ;
2007-11-04 03:34:51 +00:00
color [ 3 ] = 0xFF ;
/* set from points */
2012-03-17 20:01:54 +00:00
for ( k = 0 , vm = pt - > vm ; k < pt - > nvmaps ; k + + , vm + + )
2007-11-04 03:34:51 +00:00
{
2012-03-17 20:01:54 +00:00
if ( vm - > vmap - > type = = LWID_ ( ' T ' , ' X ' , ' U ' , ' V ' ) ) {
2007-11-04 03:34:51 +00:00
/* set st coords */
st [ 0 ] = vm - > vmap - > val [ vm - > index ] [ 0 ] ;
st [ 1 ] = 1.f - vm - > vmap - > val [ vm - > index ] [ 1 ] ;
}
2012-03-17 20:01:54 +00:00
else if ( vm - > vmap - > type = = LWID_ ( ' R ' , ' G ' , ' B ' , ' A ' ) ) {
2007-11-04 03:34:51 +00:00
/* set rgba */
2012-03-17 20:01:54 +00:00
color [ 0 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 0 ] * surface - > color . rgb [ 0 ] * surface - > diffuse . val * 0xFF ) ;
color [ 1 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 1 ] * surface - > color . rgb [ 1 ] * surface - > diffuse . val * 0xFF ) ;
color [ 2 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 2 ] * surface - > color . rgb [ 2 ] * surface - > diffuse . val * 0xFF ) ;
color [ 3 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 3 ] * 0xFF ) ;
2007-11-04 03:34:51 +00:00
}
}
/* override with polygon data */
2012-03-17 20:01:54 +00:00
for ( k = 0 , vm = v - > vm ; k < v - > nvmaps ; k + + , vm + + )
2007-11-04 03:34:51 +00:00
{
2012-03-17 20:01:54 +00:00
if ( vm - > vmap - > type = = LWID_ ( ' T ' , ' X ' , ' U ' , ' V ' ) ) {
2007-11-04 03:34:51 +00:00
/* set st coords */
st [ 0 ] = vm - > vmap - > val [ vm - > index ] [ 0 ] ;
st [ 1 ] = 1.f - vm - > vmap - > val [ vm - > index ] [ 1 ] ;
}
2012-03-17 20:01:54 +00:00
else if ( vm - > vmap - > type = = LWID_ ( ' R ' , ' G ' , ' B ' , ' A ' ) ) {
2007-11-04 03:34:51 +00:00
/* set rgba */
2012-03-17 20:01:54 +00:00
color [ 0 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 0 ] * surface - > color . rgb [ 0 ] * surface - > diffuse . val * 0xFF ) ;
color [ 1 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 1 ] * surface - > color . rgb [ 1 ] * surface - > diffuse . val * 0xFF ) ;
color [ 2 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 2 ] * surface - > color . rgb [ 2 ] * surface - > diffuse . val * 0xFF ) ;
color [ 3 ] = ( picoByte_t ) ( vm - > vmap - > val [ vm - > index ] [ 3 ] * 0xFF ) ;
2007-11-04 03:34:51 +00:00
}
}
/* find vertex in this surface and if we can't find it there create it */
vertexCombinationHash = PicoFindVertexCombinationInHashTable ( hashTable , xyz , normal , st , color ) ;
2012-03-17 20:01:54 +00:00
if ( vertexCombinationHash ) {
2007-11-04 03:34:51 +00:00
/* found an existing one */
2012-03-17 20:01:54 +00:00
PicoSetSurfaceIndex ( picoSurface , ( i * 3 + j ) , vertexCombinationHash - > index ) ;
2007-11-04 03:34:51 +00:00
}
else
{
/* it is a new one */
vertexCombinationHash = PicoAddVertexCombinationToHashTable ( hashTable , xyz , normal , st , color , ( picoIndex_t ) numverts ) ;
2012-03-17 20:01:54 +00:00
if ( vertexCombinationHash = = NULL ) {
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_ERROR , " Unable to allocate hash bucket entry table " ) ;
PicoFreeVertexCombinationHashTable ( hashTable ) ;
PicoFreeModel ( picoModel ) ;
lwFreeObject ( obj ) ;
return NULL ;
}
/* add the vertex to this surface */
PicoSetSurfaceXYZ ( picoSurface , numverts , xyz ) ;
/* set dummy normal */
PicoSetSurfaceNormal ( picoSurface , numverts , normal ) ;
/* set color */
PicoSetSurfaceColor ( picoSurface , 0 , numverts , color ) ;
/* set st coords */
PicoSetSurfaceST ( picoSurface , 0 , numverts , st ) ;
/* set index */
2012-03-17 20:01:54 +00:00
PicoSetSurfaceIndex ( picoSurface , ( i * 3 + j ) , ( picoIndex_t ) numverts ) ;
2007-11-04 03:34:51 +00:00
numverts + + ;
}
}
}
/* free the hashtable */
PicoFreeVertexCombinationHashTable ( hashTable ) ;
2012-03-17 20:01:54 +00:00
/* get next surface */
2007-11-04 03:34:51 +00:00
surface = surface - > next ;
}
# ifdef DEBUG_PM_LWO
load_start = convert_finish = clock ( ) ;
# endif
lwFreeObject ( obj ) ;
# ifdef DEBUG_PM_LWO
load_finish = clock ( ) ;
2012-03-17 20:01:54 +00:00
load_elapsed + = ( double ) ( load_finish - load_start ) / CLOCKS_PER_SEC ;
convert_elapsed = ( double ) ( convert_finish - convert_start ) / CLOCKS_PER_SEC ;
2007-11-04 03:34:51 +00:00
_pico_printf ( PICO_NORMAL , " Loaded model in in %-.2f second(s) (loading: %-.2fs converting: %-.2fs) \n " , load_elapsed + convert_elapsed , load_elapsed , convert_elapsed ) ;
# endif
/* return the new pico model */
return picoModel ;
}
/* pico file format module definition */
const picoModule_t picoModuleLWO =
{
2012-03-17 20:01:54 +00:00
" 1.0 " , /* module version string */
" LightWave Object " , /* module display name */
" Arnout van Meer " , /* author's name */
" 2003 Arnout van Meer, 2000 Ernie Wright " , /* module copyright */
2007-11-04 03:34:51 +00:00
{
2012-03-17 20:01:54 +00:00
" lwo " , NULL , NULL , NULL /* default extensions to use */
2007-11-04 03:34:51 +00:00
} ,
2012-03-17 20:01:54 +00:00
_lwo_canload , /* validation routine */
_lwo_load , /* load routine */
NULL , /* save validation routine */
NULL /* save routine */
2007-11-04 03:34:51 +00:00
} ;