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_OBJ_C
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "picointernal.h"
|
|
|
|
|
|
|
|
/* disable warnings */
|
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma warning( disable:4100 ) /* unref param */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* todo:
|
|
|
|
* - '_obj_load' code crashes in a weird way after
|
|
|
|
* '_obj_mtl_load' for a few .mtl files
|
|
|
|
* - process 'mtllib' rather than using <model>.mtl
|
|
|
|
*/
|
|
|
|
/* uncomment when debugging this module */
|
|
|
|
/* #define DEBUG_PM_OBJ */
|
|
|
|
/* #define DEBUG_PM_OBJ_EX */
|
|
|
|
|
|
|
|
/* this holds temporary vertex data read by parser */
|
|
|
|
typedef struct SObjVertexData
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
picoVec3_t v; /* geometric vertices */
|
|
|
|
picoVec2_t vt; /* texture vertices */
|
|
|
|
picoVec3_t vn; /* vertex normals (optional) */
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
TObjVertexData;
|
|
|
|
|
|
|
|
/* _obj_canload:
|
|
|
|
* validates a wavefront obj model file.
|
|
|
|
*/
|
2012-03-17 20:01:54 +00:00
|
|
|
static int _obj_canload( PM_PARAMS_CANLOAD ){
|
2007-11-04 03:34:51 +00:00
|
|
|
picoParser_t *p;
|
|
|
|
|
|
|
|
/* check data length */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bufSize < 30 ) {
|
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
|
|
|
|
|
|
|
/* first check file extension. we have to do this for objs */
|
|
|
|
/* cause there is no good way to identify the contents */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_stristr( fileName,".obj" ) != NULL ||
|
|
|
|
_pico_stristr( fileName,".wf" ) != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return PICO_PMV_OK;
|
|
|
|
}
|
|
|
|
/* if the extension check failed we parse through the first */
|
|
|
|
/* few lines in file and look for common keywords often */
|
|
|
|
/* appearing at the beginning of wavefront objects */
|
|
|
|
|
|
|
|
/* alllocate a new pico parser */
|
2010-10-05 08:57:07 +00:00
|
|
|
p = _pico_new_parser( (const picoByte_t *)buffer,bufSize );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return PICO_PMV_ERROR_MEMORY;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* parse obj head line by line for type check */
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( 1 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get first token on line */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_parse_first( p ) == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* we only parse the first few lines, say 80 */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->curLine > 80 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* skip empty lines */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->token == NULL || !strlen( p->token ) ) {
|
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
|
|
|
|
|
|
|
/* material library keywords are teh good */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_stricmp( p->token,"usemtl" ) ||
|
|
|
|
!_pico_stricmp( p->token,"mtllib" ) ||
|
|
|
|
!_pico_stricmp( p->token,"g" ) ||
|
|
|
|
!_pico_stricmp( p->token,"v" ) ) { /* v,g bit fishy, but uh... */
|
2007-11-04 03:34:51 +00:00
|
|
|
/* free the pico parser thing */
|
|
|
|
_pico_free_parser( p );
|
|
|
|
|
|
|
|
/* seems to be a valid wavefront obj */
|
|
|
|
return PICO_PMV_OK;
|
|
|
|
}
|
|
|
|
/* skip rest of line */
|
|
|
|
_pico_parse_skip_rest( p );
|
|
|
|
}
|
|
|
|
/* free the pico parser thing */
|
|
|
|
_pico_free_parser( p );
|
|
|
|
|
|
|
|
/* doesn't really look like an obj to us */
|
|
|
|
return PICO_PMV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SizeObjVertexData:
|
|
|
|
* This pretty piece of 'alloc ahead' code dynamically
|
|
|
|
* allocates - and reallocates as soon as required -
|
|
|
|
* my vertex data array in even steps.
|
|
|
|
*/
|
|
|
|
#define SIZE_OBJ_STEP 4096
|
|
|
|
|
|
|
|
static TObjVertexData *SizeObjVertexData(
|
|
|
|
TObjVertexData *vertexData, int reqEntries,
|
2012-03-17 20:01:54 +00:00
|
|
|
int *entries, int *allocated ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int newAllocated;
|
|
|
|
|
|
|
|
/* sanity checks */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( reqEntries < 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( entries == NULL || allocated == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL; /* must have */
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
/* no need to grow yet */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertexData && ( reqEntries < *allocated ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
*entries = reqEntries;
|
|
|
|
return vertexData;
|
|
|
|
}
|
|
|
|
/* given vertex data ptr not allocated yet */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertexData == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* how many entries to allocate */
|
2012-03-17 20:01:54 +00:00
|
|
|
newAllocated = ( reqEntries > SIZE_OBJ_STEP ) ?
|
|
|
|
reqEntries : SIZE_OBJ_STEP;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* throw out an extended debug message */
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "SizeObjVertexData: allocate (%d entries)\n",
|
|
|
|
newAllocated );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* first time allocation */
|
|
|
|
vertexData = (TObjVertexData *)
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_alloc( sizeof( TObjVertexData ) * newAllocated );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* allocation failed */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertexData == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* allocation succeeded */
|
|
|
|
*allocated = newAllocated;
|
|
|
|
*entries = reqEntries;
|
|
|
|
return vertexData;
|
|
|
|
}
|
|
|
|
/* given vertex data ptr needs to be resized */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( reqEntries == *allocated ) {
|
|
|
|
newAllocated = ( *allocated + SIZE_OBJ_STEP );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* throw out an extended debug message */
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "SizeObjVertexData: reallocate (%d entries)\n",
|
|
|
|
newAllocated );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* try to reallocate */
|
|
|
|
vertexData = (TObjVertexData *)
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_realloc( (void *)&vertexData,
|
|
|
|
sizeof( TObjVertexData ) * ( *allocated ),
|
|
|
|
sizeof( TObjVertexData ) * ( newAllocated ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* reallocation failed */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertexData == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* reallocation succeeded */
|
|
|
|
*allocated = newAllocated;
|
|
|
|
*entries = reqEntries;
|
|
|
|
return vertexData;
|
|
|
|
}
|
|
|
|
/* we're b0rked when we reach this */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void FreeObjVertexData( TObjVertexData *vertexData ){
|
|
|
|
if ( vertexData != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
free( (TObjVertexData *)vertexData );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-26 11:21:13 +00:00
|
|
|
#if 0
|
2012-03-17 20:01:54 +00:00
|
|
|
static int _obj_mtl_load( picoModel_t *model ){
|
2008-06-26 11:21:13 +00:00
|
|
|
//picoShader_t *curShader = NULL;
|
2007-11-04 03:34:51 +00:00
|
|
|
picoParser_t *p;
|
|
|
|
picoByte_t *mtlBuffer;
|
2012-03-17 20:01:54 +00:00
|
|
|
int mtlBufSize;
|
|
|
|
char *fileName;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* sanity checks */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( model == NULL || model->fileName == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* skip if we have a zero length model file name */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !strlen( model->fileName ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* helper */
|
|
|
|
#define _obj_mtl_error_return \
|
|
|
|
{ \
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_free_parser( p ); \
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_free_file( mtlBuffer ); \
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_free( fileName ); \
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
/* alloc copy of model file name */
|
2016-12-04 05:16:39 +00:00
|
|
|
fileName = _pico_clone_alloc( model->fileName );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fileName == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* change extension of model file to .mtl */
|
|
|
|
_pico_setfext( fileName, "mtl" );
|
|
|
|
|
|
|
|
/* load .mtl file contents */
|
|
|
|
_pico_load_file( fileName,&mtlBuffer,&mtlBufSize );
|
|
|
|
|
|
|
|
/* check result */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( mtlBufSize == 0 ) {
|
|
|
|
return 1; /* file is empty: no error */
|
|
|
|
}
|
|
|
|
if ( mtlBufSize < 0 ) {
|
|
|
|
return 0; /* load failed: error */
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create a new pico parser */
|
|
|
|
p = _pico_new_parser( mtlBuffer, mtlBufSize );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-06-26 11:21:13 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* doo teh .mtl parse */
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( 1 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get next token in material file */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_parse( p,1 ) == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
/* skip empty lines */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->token == NULL || !strlen( p->token ) ) {
|
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
|
|
|
|
|
|
|
/* skip comment lines */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->token[0] == '#' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_parse_skip_rest( p );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* new material */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_stricmp( p->token,"newmtl" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
picoShader_t *shader;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
/* get material name */
|
|
|
|
name = _pico_parse( p,0 );
|
|
|
|
|
|
|
|
/* validate material name */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( name == NULL || !strlen( name ) ) {
|
|
|
|
_pico_printf( PICO_ERROR,"Missing material name in MTL, line %d.",p->curLine );
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
|
|
|
}
|
|
|
|
/* create a 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
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set shader name */
|
|
|
|
PicoSetShaderName( shader,name );
|
|
|
|
|
|
|
|
/* assign pointer to current shader */
|
|
|
|
curShader = shader;
|
|
|
|
}
|
|
|
|
/* diffuse map name */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"map_kd" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
char *mapName;
|
|
|
|
|
|
|
|
/* pointer to current shader must be valid */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( curShader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get material's diffuse map name */
|
|
|
|
mapName = _pico_parse( p,0 );
|
|
|
|
|
|
|
|
/* validate map name */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( mapName == NULL || !strlen( mapName ) ) {
|
|
|
|
_pico_printf( PICO_ERROR,"Missing material map name in MTL, line %d.",p->curLine );
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
|
|
|
}
|
|
|
|
/* set shader map name */
|
|
|
|
PicoSetShaderMapName( shader,mapName );
|
|
|
|
}
|
|
|
|
/* dissolve factor (pseudo transparency 0..1) */
|
|
|
|
/* where 0 means 100% transparent and 1 means opaque */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"d" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
picoByte_t *diffuse;
|
|
|
|
float value;
|
|
|
|
|
|
|
|
|
|
|
|
/* get dissolve factor */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_float( p,&value ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set shader transparency */
|
|
|
|
PicoSetShaderTransparency( curShader,value );
|
|
|
|
|
|
|
|
/* get shader's diffuse color */
|
|
|
|
diffuse = PicoGetShaderDiffuseColor( curShader );
|
|
|
|
|
|
|
|
/* set diffuse alpha to transparency */
|
|
|
|
diffuse[ 3 ] = (picoByte_t)( value * 255.0 );
|
|
|
|
|
|
|
|
/* set shader's new diffuse color */
|
|
|
|
PicoSetShaderDiffuseColor( curShader,diffuse );
|
|
|
|
}
|
|
|
|
/* shininess (phong specular component) */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"ns" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* remark:
|
|
|
|
* - well, this is some major obj spec fuckup once again. some
|
|
|
|
* apps store this in 0..1 range, others use 0..100 range,
|
|
|
|
* even others use 0..2048 range, and again others use the
|
|
|
|
* range 0..128, some even use 0..1000, 0..200, 400..700,
|
|
|
|
* honestly, what's up with the 3d app coders? happens when
|
|
|
|
* you smoke too much weed i guess. -sea
|
|
|
|
*/
|
|
|
|
float value;
|
|
|
|
|
|
|
|
/* pointer to current shader must be valid */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( curShader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get totally screwed up shininess (a random value in fact ;) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_float( p,&value ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* okay, there is no way to set this correctly, so we simply */
|
|
|
|
/* try to guess a few ranges (most common ones i have seen) */
|
|
|
|
|
|
|
|
/* assume 0..2048 range */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( value > 1000 ) {
|
|
|
|
value = 128.0 * ( value / 2048.0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
/* assume 0..1000 range */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( value > 200 ) {
|
|
|
|
value = 128.0 * ( value / 1000.0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
/* assume 0..200 range */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( value > 100 ) {
|
|
|
|
value = 128.0 * ( value / 200.0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
/* assume 0..100 range */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( value > 1 ) {
|
|
|
|
value = 128.0 * ( value / 100.0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
/* assume 0..1 range */
|
|
|
|
else {
|
|
|
|
value *= 128.0;
|
|
|
|
}
|
|
|
|
/* negative shininess is bad (yes, i have seen it...) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( value < 0.0 ) {
|
|
|
|
value = 0.0;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set the pico shininess value in range 0..127 */
|
|
|
|
/* geez, .obj is such a mess... */
|
|
|
|
PicoSetShaderShininess( curShader,value );
|
|
|
|
}
|
|
|
|
/* kol0r ambient (wut teh fuk does "ka" stand for?) */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"ka" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
picoColor_t color;
|
2012-03-17 20:01:54 +00:00
|
|
|
picoVec3_t v;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* pointer to current shader must be valid */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( curShader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get color vector */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_vec( p,v ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* scale to byte range */
|
|
|
|
color[ 0 ] = (picoByte_t)( v[ 0 ] * 255 );
|
|
|
|
color[ 1 ] = (picoByte_t)( v[ 1 ] * 255 );
|
|
|
|
color[ 2 ] = (picoByte_t)( v[ 2 ] * 255 );
|
|
|
|
color[ 3 ] = (picoByte_t)( 255 );
|
|
|
|
|
|
|
|
/* set ambient color */
|
|
|
|
PicoSetShaderAmbientColor( curShader,color );
|
|
|
|
}
|
|
|
|
/* kol0r diffuse */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"kd" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
picoColor_t color;
|
2012-03-17 20:01:54 +00:00
|
|
|
picoVec3_t v;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* pointer to current shader must be valid */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( curShader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get color vector */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_vec( p,v ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* scale to byte range */
|
|
|
|
color[ 0 ] = (picoByte_t)( v[ 0 ] * 255 );
|
|
|
|
color[ 1 ] = (picoByte_t)( v[ 1 ] * 255 );
|
|
|
|
color[ 2 ] = (picoByte_t)( v[ 2 ] * 255 );
|
|
|
|
color[ 3 ] = (picoByte_t)( 255 );
|
|
|
|
|
|
|
|
/* set diffuse color */
|
|
|
|
PicoSetShaderDiffuseColor( curShader,color );
|
|
|
|
}
|
|
|
|
/* kol0r specular */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"ks" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
picoColor_t color;
|
2012-03-17 20:01:54 +00:00
|
|
|
picoVec3_t v;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* pointer to current shader must be valid */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( curShader == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get color vector */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_vec( p,v ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_obj_mtl_error_return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* scale to byte range */
|
|
|
|
color[ 0 ] = (picoByte_t)( v[ 0 ] * 255 );
|
|
|
|
color[ 1 ] = (picoByte_t)( v[ 1 ] * 255 );
|
|
|
|
color[ 2 ] = (picoByte_t)( v[ 2 ] * 255 );
|
|
|
|
color[ 3 ] = (picoByte_t)( 255 );
|
|
|
|
|
|
|
|
/* set specular color */
|
|
|
|
PicoSetShaderSpecularColor( curShader,color );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* skip rest of line */
|
|
|
|
_pico_parse_skip_rest( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free parser, file buffer, and file name */
|
|
|
|
_pico_free_parser( p );
|
|
|
|
_pico_free_file( mtlBuffer );
|
|
|
|
_pico_free( fileName );
|
|
|
|
|
|
|
|
/* return with success */
|
|
|
|
return 1;
|
|
|
|
}
|
2008-06-26 11:21:13 +00:00
|
|
|
#endif
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* _obj_load:
|
|
|
|
* loads a wavefront obj model file.
|
2012-03-17 20:01:54 +00:00
|
|
|
*/
|
|
|
|
static picoModel_t *_obj_load( PM_PARAMS_LOAD ){
|
2007-11-04 03:34:51 +00:00
|
|
|
TObjVertexData *vertexData = NULL;
|
|
|
|
picoModel_t *model;
|
|
|
|
picoSurface_t *curSurface = NULL;
|
|
|
|
picoParser_t *p;
|
2012-03-17 20:01:54 +00:00
|
|
|
int allocated;
|
|
|
|
int entries;
|
|
|
|
int numVerts = 0;
|
|
|
|
int numNormals = 0;
|
|
|
|
int numUVs = 0;
|
|
|
|
int curVertex = 0;
|
|
|
|
int curFace = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* helper */
|
2012-03-17 20:01:54 +00:00
|
|
|
#define _obj_error_return( m ) \
|
2007-11-04 03:34:51 +00:00
|
|
|
{ \
|
2012-03-17 20:01:54 +00:00
|
|
|
_pico_printf( PICO_ERROR,"%s in OBJ, line %d.",m,p->curLine ); \
|
|
|
|
_pico_free_parser( p ); \
|
2007-11-04 03:34:51 +00:00
|
|
|
FreeObjVertexData( vertexData ); \
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoFreeModel( model ); \
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL; \
|
|
|
|
}
|
|
|
|
/* alllocate a new pico parser */
|
2010-10-05 08:57:07 +00:00
|
|
|
p = _pico_new_parser( (const picoByte_t *)buffer,bufSize );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* create a new pico model */
|
|
|
|
model = PicoNewModel();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( model == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_free_parser( p );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* do model setup */
|
|
|
|
PicoSetModelFrameNum( model,frameNum );
|
|
|
|
PicoSetModelName( model,fileName );
|
|
|
|
PicoSetModelFileName( model,fileName );
|
|
|
|
|
|
|
|
/* try loading the materials; we don't handle the result */
|
|
|
|
#if 0
|
|
|
|
_obj_mtl_load( model );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* parse obj line by line */
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( 1 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get first token on line */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _pico_parse_first( p ) == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* skip empty lines */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->token == NULL || !strlen( p->token ) ) {
|
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
|
|
|
|
|
|
|
/* skip comment lines */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->token[0] == '#' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_pico_parse_skip_rest( p );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* vertex */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_stricmp( p->token,"v" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
TObjVertexData *data;
|
|
|
|
picoVec3_t v;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vertexData = SizeObjVertexData( vertexData,numVerts + 1,&entries,&allocated );
|
|
|
|
if ( vertexData == NULL ) {
|
|
|
|
_obj_error_return( "Realloc of vertex data failed (1)" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
data = &vertexData[ numVerts++ ];
|
|
|
|
|
|
|
|
/* get and copy vertex */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_vec( p,v ) ) {
|
|
|
|
_obj_error_return( "Vertex parse error" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
_pico_copy_vec( v,data->v );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Vertex: x: %f y: %f z: %f\n",v[0],v[1],v[2] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* uv coord */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"vt" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
TObjVertexData *data;
|
|
|
|
picoVec2_t coord;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vertexData = SizeObjVertexData( vertexData,numUVs + 1,&entries,&allocated );
|
|
|
|
if ( vertexData == NULL ) {
|
|
|
|
_obj_error_return( "Realloc of vertex data failed (2)" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
data = &vertexData[ numUVs++ ];
|
|
|
|
|
|
|
|
/* get and copy tex coord */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_vec2( p,coord ) ) {
|
|
|
|
_obj_error_return( "UV coord parse error" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
_pico_copy_vec2( coord,data->vt );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "TexCoord: u: %f v: %f\n",coord[0],coord[1] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* vertex normal */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"vn" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
TObjVertexData *data;
|
|
|
|
picoVec3_t n;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vertexData = SizeObjVertexData( vertexData,numNormals + 1,&entries,&allocated );
|
|
|
|
if ( vertexData == NULL ) {
|
|
|
|
_obj_error_return( "Realloc of vertex data failed (3)" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
data = &vertexData[ numNormals++ ];
|
|
|
|
|
|
|
|
/* get and copy vertex normal */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !_pico_parse_vec( p,n ) ) {
|
|
|
|
_obj_error_return( "Vertex normal parse error" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
_pico_copy_vec( n,data->vn );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Normal: x: %f y: %f z: %f\n",n[0],n[1],n[2] );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* new group (for us this means a new surface) */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"g" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
picoSurface_t *newSurface;
|
|
|
|
char *groupName;
|
|
|
|
|
|
|
|
/* get first group name (ignore 2nd,3rd,etc.) */
|
|
|
|
groupName = _pico_parse( p,0 );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( groupName == NULL || !strlen( groupName ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* some obj exporters feel like they don't need to */
|
|
|
|
/* supply a group name. so we gotta handle it here */
|
|
|
|
#if 1
|
|
|
|
strcpy( p->token,"default" );
|
|
|
|
groupName = p->token;
|
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
_obj_error_return( "Invalid or missing group name" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* allocate a pico surface */
|
|
|
|
newSurface = PicoNewSurface( model );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( newSurface == NULL ) {
|
|
|
|
_obj_error_return( "Error allocating surface" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2018-10-31 00:50:07 +00:00
|
|
|
/* reset face index and vertex index for surface */
|
2007-11-04 03:34:51 +00:00
|
|
|
curFace = 0;
|
2018-10-31 00:50:07 +00:00
|
|
|
curVertex = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* set ptr to current surface */
|
|
|
|
curSurface = newSurface;
|
|
|
|
|
|
|
|
/* we use triangle meshes */
|
|
|
|
PicoSetSurfaceType( newSurface,PICO_TRIANGLES );
|
|
|
|
|
|
|
|
/* set surface name */
|
|
|
|
PicoSetSurfaceName( newSurface,groupName );
|
|
|
|
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Group: '%s'\n",groupName );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* face (oh jesus, hopefully this will do the job right ;) */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !_pico_stricmp( p->token,"f" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* okay, this is a mess. some 3d apps seem to try being unique, */
|
|
|
|
/* hello cinema4d & 3d exploration, feel good today?, and save */
|
|
|
|
/* this crap in tons of different formats. gah, those screwed */
|
|
|
|
/* coders. tho the wavefront obj standard defines exactly two */
|
|
|
|
/* ways of storing face information. so, i really won't support */
|
|
|
|
/* such stupid extravaganza here! */
|
|
|
|
|
|
|
|
picoVec3_t verts [ 4 ];
|
|
|
|
picoVec3_t normals[ 4 ];
|
|
|
|
picoVec2_t coords [ 4 ];
|
|
|
|
|
|
|
|
int iv [ 4 ], has_v;
|
|
|
|
int ivt[ 4 ], has_vt = 0;
|
|
|
|
int ivn[ 4 ], has_vn = 0;
|
|
|
|
int have_quad = 0;
|
2010-10-05 08:57:07 +00:00
|
|
|
int slashcount = 0;
|
|
|
|
int doubleslash = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* group defs *must* come before faces */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( curSurface == NULL ) {
|
|
|
|
_obj_error_return( "No group defined for faces" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Face: " );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* read vertex/uv/normal indices for the first three face */
|
|
|
|
/* vertices (cause we only support triangles) into 'i*[]' */
|
|
|
|
/* store the actual vertex/uv/normal data in three arrays */
|
|
|
|
/* called 'verts','coords' and 'normals'. */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 4; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* get next vertex index string (different */
|
|
|
|
/* formats are handled below) */
|
|
|
|
str = _pico_parse( p,0 );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( str == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* just break for quads */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i == 3 ) {
|
|
|
|
break;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* error otherwise */
|
2012-03-17 20:01:54 +00:00
|
|
|
_obj_error_return( "Face parse error" );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/* if this is the fourth index string we're */
|
|
|
|
/* parsing we assume that we have a quad */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i == 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
have_quad = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get slash count once */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
slashcount = _pico_strchcount( str,'/' );
|
2012-03-17 20:01:54 +00:00
|
|
|
doubleslash = strstr( str,"//" ) != NULL;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/* handle format 'v//vn' */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( doubleslash && ( slashcount == 2 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
has_v = has_vn = 1;
|
|
|
|
sscanf( str,"%d//%d",&iv[ i ],&ivn[ i ] );
|
|
|
|
}
|
|
|
|
/* handle format 'v/vt/vn' */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !doubleslash && ( slashcount == 2 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
has_v = has_vt = has_vn = 1;
|
|
|
|
sscanf( str,"%d/%d/%d",&iv[ i ],&ivt[ i ],&ivn[ i ] );
|
|
|
|
}
|
|
|
|
/* handle format 'v/vt' (non-standard fuckage) */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !doubleslash && ( slashcount == 1 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
has_v = has_vt = 1;
|
|
|
|
sscanf( str,"%d/%d",&iv[ i ],&ivt[ i ] );
|
|
|
|
}
|
|
|
|
/* else assume face format 'v' */
|
|
|
|
/* (must have been invented by some bored granny) */
|
|
|
|
else {
|
|
|
|
/* get single vertex index */
|
|
|
|
has_v = 1;
|
|
|
|
iv[ i ] = atoi( str );
|
|
|
|
|
|
|
|
/* either invalid face format or out of range */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( iv[ i ] == 0 ) {
|
|
|
|
_obj_error_return( "Invalid face format" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/* fix useless back references */
|
|
|
|
/* todo: check if this works as it is supposed to */
|
|
|
|
|
|
|
|
/* assign new indices */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( iv [ i ] < 0 ) {
|
|
|
|
iv [ i ] = ( numVerts - iv [ i ] );
|
|
|
|
}
|
|
|
|
if ( ivt[ i ] < 0 ) {
|
|
|
|
ivt[ i ] = ( numUVs - ivt[ i ] );
|
|
|
|
}
|
|
|
|
if ( ivn[ i ] < 0 ) {
|
|
|
|
ivn[ i ] = ( numNormals - ivn[ i ] );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* validate indices */
|
|
|
|
/* - commented out. index range checks will trigger
|
2012-03-17 20:01:54 +00:00
|
|
|
if (iv [ i ] < 1) iv [ i ] = 1;
|
|
|
|
if (ivt[ i ] < 1) ivt[ i ] = 1;
|
|
|
|
if (ivn[ i ] < 1) ivn[ i ] = 1;
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set vertex origin */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( has_v ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check vertex index range */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( iv[ i ] < 1 || iv[ i ] > numVerts ) {
|
|
|
|
_obj_error_return( "Vertex index out of range" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get vertex data */
|
|
|
|
verts[ i ][ 0 ] = vertexData[ iv[ i ] - 1 ].v[ 0 ];
|
|
|
|
verts[ i ][ 1 ] = vertexData[ iv[ i ] - 1 ].v[ 1 ];
|
|
|
|
verts[ i ][ 2 ] = vertexData[ iv[ i ] - 1 ].v[ 2 ];
|
|
|
|
}
|
|
|
|
/* set vertex normal */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( has_vn ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check normal index range */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ivn[ i ] < 1 || ivn[ i ] > numNormals ) {
|
|
|
|
_obj_error_return( "Normal index out of range" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get normal data */
|
|
|
|
normals[ i ][ 0 ] = vertexData[ ivn[ i ] - 1 ].vn[ 0 ];
|
|
|
|
normals[ i ][ 1 ] = vertexData[ ivn[ i ] - 1 ].vn[ 1 ];
|
|
|
|
normals[ i ][ 2 ] = vertexData[ ivn[ i ] - 1 ].vn[ 2 ];
|
|
|
|
}
|
|
|
|
/* set texture coordinate */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( has_vt ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check uv index range */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ivt[ i ] < 1 || ivt[ i ] > numUVs ) {
|
|
|
|
_obj_error_return( "UV coord index out of range" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get uv coord data */
|
|
|
|
coords[ i ][ 0 ] = vertexData[ ivt[ i ] - 1 ].vt[ 0 ];
|
|
|
|
coords[ i ][ 1 ] = vertexData[ ivt[ i ] - 1 ].vt[ 1 ];
|
|
|
|
coords[ i ][ 1 ] = -coords[ i ][ 1 ];
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "(%4d",iv[ i ] );
|
|
|
|
if ( has_vt ) {
|
|
|
|
printf( " %4d",ivt[ i ] );
|
|
|
|
}
|
|
|
|
if ( has_vn ) {
|
|
|
|
printf( " %4d",ivn[ i ] );
|
|
|
|
}
|
|
|
|
printf( ") " );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_PM_OBJ_EX
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
/* now that we have extracted all the indices and have */
|
|
|
|
/* read the actual data we need to assign all the crap */
|
|
|
|
/* to our current pico surface */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( has_v ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
int max = 3;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( have_quad ) {
|
|
|
|
max = 4;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* assign all surface information */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < max; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
/*if( has_v )*/ PicoSetSurfaceXYZ( curSurface, ( curVertex + i ), verts [ i ] );
|
|
|
|
/*if( has_vt )*/ PicoSetSurfaceST( curSurface,0,( curVertex + i ), coords [ i ] );
|
|
|
|
/*if( has_vn )*/ PicoSetSurfaceNormal( curSurface, ( curVertex + i ), normals[ i ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/* add our triangle (A B C) */
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceIndex( curSurface,( curFace * 3 + 2 ),(picoIndex_t)( curVertex + 0 ) );
|
|
|
|
PicoSetSurfaceIndex( curSurface,( curFace * 3 + 1 ),(picoIndex_t)( curVertex + 1 ) );
|
|
|
|
PicoSetSurfaceIndex( curSurface,( curFace * 3 + 0 ),(picoIndex_t)( curVertex + 2 ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
curFace++;
|
|
|
|
|
|
|
|
/* if we don't have a simple triangle, but a quad... */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( have_quad ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* we have to add another triangle (2nd half of quad which is A C D) */
|
2012-03-17 20:01:54 +00:00
|
|
|
PicoSetSurfaceIndex( curSurface,( curFace * 3 + 2 ),(picoIndex_t)( curVertex + 0 ) );
|
|
|
|
PicoSetSurfaceIndex( curSurface,( curFace * 3 + 1 ),(picoIndex_t)( curVertex + 2 ) );
|
|
|
|
PicoSetSurfaceIndex( curSurface,( curFace * 3 + 0 ),(picoIndex_t)( curVertex + 3 ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
curFace++;
|
|
|
|
}
|
|
|
|
/* increase vertex count */
|
|
|
|
curVertex += max;
|
|
|
|
}
|
2015-10-01 15:56:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if ( !_pico_stricmp( p->token, "usemtl" ) ) {
|
|
|
|
char *materialName;
|
|
|
|
materialName = _pico_parse( p, 0 );
|
2017-02-21 17:45:37 +00:00
|
|
|
if( materialName && strlen( materialName ) ) {
|
2015-10-01 15:56:00 +00:00
|
|
|
picoShader_t *shader;
|
|
|
|
shader = PicoFindShader( model, materialName, 0 );
|
|
|
|
if( !shader ) {
|
|
|
|
shader = PicoNewShader( model );
|
2017-02-21 17:45:37 +00:00
|
|
|
if( shader ) {
|
|
|
|
PicoSetShaderMapName( shader, materialName );
|
|
|
|
PicoSetShaderName( shader, materialName );
|
|
|
|
}
|
2015-10-01 15:56:00 +00:00
|
|
|
}
|
|
|
|
if( shader && curSurface ) {
|
|
|
|
PicoSetSurfaceShader( curSurface, shader );
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/* skip unparsed rest of line and continue */
|
|
|
|
_pico_parse_skip_rest( p );
|
|
|
|
}
|
|
|
|
/* free memory used by temporary vertexdata */
|
|
|
|
FreeObjVertexData( vertexData );
|
|
|
|
|
|
|
|
/* return allocated pico model */
|
|
|
|
return model;
|
|
|
|
// return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pico file format module definition */
|
|
|
|
const picoModule_t picoModuleOBJ =
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
"0.6-b", /* module version string */
|
|
|
|
"Wavefront ASCII", /* 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
|
|
|
"obj",NULL,NULL,NULL /* default extensions to use */
|
2007-11-04 03:34:51 +00:00
|
|
|
},
|
2012-03-17 20:01:54 +00:00
|
|
|
_obj_canload, /* validation routine */
|
|
|
|
_obj_load, /* load routine */
|
|
|
|
NULL, /* save validation routine */
|
|
|
|
NULL /* save routine */
|
2007-11-04 03:34:51 +00:00
|
|
|
};
|