Exemplo n.º 1
0
static int GetMeshShader (T3dsLoaderPers *pers)
{
    char shaderName[255] = { 0 };
    picoShader_t  *shader;
    int  numSharedVerts;
    int  setShaderName = 0;
    int  i;
    
    /* the shader is either the color or the texture map of the */
    /* object. it can also hold other information like the brightness, */
    /* shine, etc. stuff we don't really care about. we just want the */
    /* color, or the texture map file name really */

    /* get in the shader name */
    if (!GetASCIIZ(pers,shaderName,sizeof(shaderName)))
        return 0;
    
    /* ydnar: trim to first whitespace */
    _pico_first_token( shaderName );
    
    /* now that we have the shader name we need to go through all of */
    /* the shaders and check the name against each shader. when we */
    /* find a shader in our shader list that matches this name we */
    /* just read in, then we assign the shader's id of the object to */
    /* that shader */

    /* get shader id for shader name */
    shader = PicoFindShader( pers->model, shaderName, 1 );

    /* we've found a matching shader */
    if ((shader != NULL) && pers->surface)
    {
        char  mapName[1024+1];
        char *mapNamePtr;
        memset( mapName,0,sizeof(mapName) );

        /* get ptr to shader's map name */
        mapNamePtr = PicoGetShaderMapName( shader );

        /* we have a valid map name ptr */
        if (mapNamePtr != NULL)
        {
            char  temp[128];
            const char *name;

            /* copy map name to local buffer */
            strcpy( mapName,mapNamePtr );

            /* extract file name */
            name = _pico_nopath( mapName );
            strncpy( temp, name, sizeof(temp) );

            /* remove file extension */
            /* name = _pico_setfext( name,"" ); */

            /* assign default name if no name available */
            if (strlen(temp) < 1)
                strcpy(temp,pers->basename);

            /* build shader name */
            _pico_strlwr( temp ); /* gaynux update -sea */
            sprintf( mapName,"models/mapobjects/%s/%s",pers->basename,temp );

            /* set shader name */
            /* PicoSetShaderName( shader,mapName ); */    /* ydnar: this will screw up the named shader */

            /* set surface's shader index */
            PicoSetSurfaceShader( pers->surface, shader );

            setShaderName = 1;
        }
    }
    /* we didn't set a shader name; throw out warning */
    if (!setShaderName)
    {
        _pico_printf( PICO_WARNING,"3DS mesh is missing shader name");
    }
    /* we don't process the list of shared vertices here; there is a */
    /* short int that gives the number of faces of the mesh concerned */
    /* by this shader, then there is the list itself of these faces. */
    /* 0000 means the first face of the (4120) face list */

    /* get number of shared verts */
    numSharedVerts = GetWord(pers);

#ifdef DEBUG_PM_3DS
    printf("GetMeshShader: uses shader '%s' (nsv %d)\n",shaderName,numSharedVerts);
#endif
    /* skip list of shared verts */
    for (i=0; i<numSharedVerts; i++)
    {
        GetWord(pers);
    }
    /* success (no errors occured) */
    return 1;
}
Exemplo n.º 2
0
/* _obj_load:
 *  loads a wavefront obj model file.
 */
static picoModel_t *_obj_load( PM_PARAMS_LOAD ){
	TObjVertexData *vertexData  = NULL;
	picoModel_t    *model;
	picoSurface_t  *curSurface  = NULL;
	picoParser_t   *p;
	int allocated = 0;
	int entries;
	int numVerts    = 0;
	int numNormals  = 0;
	int numUVs      = 0;
	int curVertex   = 0;
	int curFace     = 0;

	int autoGroupNumber = 0;
	char autoGroupNameBuf[64];

#define AUTO_GROUPNAME( namebuf ) \
	sprintf( namebuf, "__autogroup_%d", autoGroupNumber++ )
#define NEW_SURFACE( name )	\
	{ \
		picoSurface_t *newSurface; \
		/* allocate a pico surface */ \
		newSurface = PicoNewSurface( model ); \
		if ( newSurface == NULL ) {	\
			_obj_error_return( "Error allocating surface" ); } \
		/* reset face index for surface */ \
		curFace = 0; \
		curVertex = 0; \
		/* if we can, assign the previous shader to this surface */	\
		if ( curSurface ) {	\
			PicoSetSurfaceShader( newSurface, curSurface->shader ); } \
		/* set ptr to current surface */ \
		curSurface = newSurface; \
		/* we use triangle meshes */ \
		PicoSetSurfaceType( newSurface,PICO_TRIANGLES ); \
		/* set surface name */ \
		PicoSetSurfaceName( newSurface,name ); \
	}

	/* helper */
#define _obj_error_return( m ) \
	{ \
		_pico_printf( PICO_ERROR,"%s in OBJ, line %d.",m,p->curLine ); \
		_pico_free_parser( p );	\
		FreeObjVertexData( vertexData ); \
		PicoFreeModel( model );	\
		return NULL; \
	}
	/* allocate a new pico parser */
	p = _pico_new_parser( (const picoByte_t *)buffer,bufSize );
	if ( p == NULL ) {
		return NULL;
	}

	/* create a new pico model */
	model = PicoNewModel();
	if ( model == NULL ) {
		_pico_free_parser( p );
		return NULL;
	}
	/* do model setup */
	PicoSetModelFrameNum( model,frameNum );
	PicoSetModelName( model,fileName );
	PicoSetModelFileName( model,fileName );

	/* try loading the materials */
	_obj_mtl_load( model );

	/* parse obj line by line */
	while ( 1 )
	{
		/* get first token on line */
		if ( _pico_parse_first( p ) == NULL ) {
			break;
		}

		/* skip empty lines */
		if ( p->token == NULL || !strlen( p->token ) ) {
			continue;
		}

		/* skip comment lines */
		if ( p->token[0] == '#' ) {
			_pico_parse_skip_rest( p );
			continue;
		}
		/* vertex */
		if ( !_pico_stricmp( p->token,"v" ) ) {
			TObjVertexData *data;
			picoVec3_t v;

			vertexData = SizeObjVertexData( vertexData,numVerts + 1,&entries,&allocated );
			if ( vertexData == NULL ) {
				_obj_error_return( "Realloc of vertex data failed (1)" );
			}

			data = &vertexData[ numVerts++ ];

			/* get and copy vertex */
			if ( !_pico_parse_vec( p,v ) ) {
				_obj_error_return( "Vertex parse error" );
			}

			_pico_copy_vec( v,data->v );

#ifdef DEBUG_PM_OBJ_EX
			printf( "Vertex: x: %f y: %f z: %f\n",v[0],v[1],v[2] );
#endif
		}
		/* uv coord */
		else if ( !_pico_stricmp( p->token,"vt" ) ) {
			TObjVertexData *data;
			picoVec2_t coord;

			vertexData = SizeObjVertexData( vertexData,numUVs + 1,&entries,&allocated );
			if ( vertexData == NULL ) {
				_obj_error_return( "Realloc of vertex data failed (2)" );
			}

			data = &vertexData[ numUVs++ ];

			/* get and copy tex coord */
			if ( !_pico_parse_vec2( p,coord ) ) {
				_obj_error_return( "UV coord parse error" );
			}

			_pico_copy_vec2( coord,data->vt );

#ifdef DEBUG_PM_OBJ_EX
			printf( "TexCoord: u: %f v: %f\n",coord[0],coord[1] );
#endif
		}
		/* vertex normal */
		else if ( !_pico_stricmp( p->token,"vn" ) ) {
			TObjVertexData *data;
			picoVec3_t n;

			vertexData = SizeObjVertexData( vertexData,numNormals + 1,&entries,&allocated );
			if ( vertexData == NULL ) {
				_obj_error_return( "Realloc of vertex data failed (3)" );
			}

			data = &vertexData[ numNormals++ ];

			/* get and copy vertex normal */
			if ( !_pico_parse_vec( p,n ) ) {
				_obj_error_return( "Vertex normal parse error" );
			}

			_pico_copy_vec( n,data->vn );

#ifdef DEBUG_PM_OBJ_EX
			printf( "Normal: x: %f y: %f z: %f\n",n[0],n[1],n[2] );
#endif
		}
		/* new group (for us this means a new surface) */
		else if ( !_pico_stricmp( p->token,"g" ) ) {
			char *groupName;

			/* get first group name (ignore 2nd,3rd,etc.) */
			groupName = _pico_parse( p,0 );
			if ( groupName == NULL || !strlen( groupName ) ) {
				/* 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
				_obj_error_return( "Invalid or missing group name" );
#endif
			}

			if ( curFace == 0 && curSurface != NULL ) {
				PicoSetSurfaceName( curSurface,groupName );
			}
			else
			{
				NEW_SURFACE( groupName );
			}

#ifdef DEBUG_PM_OBJ_EX
			printf( "Group: '%s'\n",groupName );
#endif
		}
		/* face (oh jesus, hopefully this will do the job right ;) */
		else if ( !_pico_stricmp( p->token,"f" ) ) {
			/* 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! */
			const int numPointsMax = 128;
			picoVec3_t verts  [ numPointsMax ];
			picoVec3_t normals[ numPointsMax ];
			picoVec2_t coords [ numPointsMax ];

			int iv [ numPointsMax ], has_v;
			int ivt[ numPointsMax ], has_vt = 0;
			int ivn[ numPointsMax ], has_vn = 0;
			int slashcount = 0;
			int doubleslash = 0;
			int i;

			if ( curSurface == NULL ) {
				_pico_printf( PICO_WARNING,"No group defined for faces, so creating an autoSurface in OBJ, line %d.",p->curLine );
				AUTO_GROUPNAME( autoGroupNameBuf );
				NEW_SURFACE( autoGroupNameBuf );
			}

			/* group defs *must* come before faces */
			if ( curSurface == NULL ) {
				_obj_error_return( "No group defined for faces" );
			}

#ifdef DEBUG_PM_OBJ_EX
			printf( "Face: " );
#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'. */
			for ( i = 0; i < numPointsMax; i++ )
			{
				char *str;

				/* get next vertex index string (different */
				/* formats are handled below) */
				str = _pico_parse( p,0 );
				if ( str == NULL ) {
					/* got nuff points */
					if ( i >= 3 ) {
						break;
					}

					/* error otherwise */
					_obj_error_return( "Face parse error" );
				}

				/* get slash count once */
				if ( i == 0 ) {
					slashcount  = _pico_strchcount( str,'/' );
					doubleslash =  strstr( str,"//" ) != NULL;
				}
				/* handle format 'v//vn' */
				if ( doubleslash && ( slashcount == 2 ) ) {
					has_v = has_vn = 1;
					sscanf( str,"%d//%d",&iv[ i ],&ivn[ i ] );
				}
				/* handle format 'v/vt/vn' */
				else if ( !doubleslash && ( slashcount == 2 ) ) {
					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) */
				else if ( !doubleslash && ( slashcount == 1 ) ) {
					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 */
					if ( iv[ i ] == 0 ) {
						_obj_error_return( "Invalid face format" );
					}
				}
				/* fix useless back references */
				/* assign new indices */
				if ( iv [ i ] < 0 ) {
					iv [ i ] = ( numVerts   + iv [ i ] + 1 );
				}
				if ( ivt[ i ] < 0 ) {
					ivt[ i ] = ( numUVs     + ivt[ i ] + 1 );
				}
				if ( ivn[ i ] < 0 ) {
					ivn[ i ] = ( numNormals + ivn[ i ] + 1 );
				}

				/* validate indices */
				/* - commented out. index range checks will trigger
				   if (iv [ i ] < 1) iv [ i ] = 1;
				   if (ivt[ i ] < 1) ivt[ i ] = 1;
				   if (ivn[ i ] < 1) ivn[ i ] = 1;
				 */
				/* set vertex origin */
				if ( has_v ) {
					/* check vertex index range */
					if ( iv[ i ] < 1 || iv[ i ] > numVerts ) {
						_obj_error_return( "Vertex index out of range" );
					}

					/* get vertex data */
					verts[ i ][ 0 ] =  vertexData[ iv[ i ] - 1 ].v[ 0 ];
					verts[ i ][ 1 ] = -vertexData[ iv[ i ] - 1 ].v[ 2 ];
					verts[ i ][ 2 ] =  vertexData[ iv[ i ] - 1 ].v[ 1 ];
				}
				/* set vertex normal */
				if ( has_vn ) {
					/* check normal index range */
					if ( ivn[ i ] < 1 || ivn[ i ] > numNormals ) {
						_obj_error_return( "Normal index out of range" );
					}

					/* get normal data */
					normals[ i ][ 0 ] =  vertexData[ ivn[ i ] - 1 ].vn[ 0 ];
					normals[ i ][ 1 ] = -vertexData[ ivn[ i ] - 1 ].vn[ 2 ];
					normals[ i ][ 2 ] =  vertexData[ ivn[ i ] - 1 ].vn[ 1 ];
				}
				/* set texture coordinate */
				if ( has_vt ) {
					/* check uv index range */
					if ( ivt[ i ] < 1 || ivt[ i ] > numUVs ) {
						_obj_error_return( "UV coord index out of range" );
					}

					/* get uv coord data */
					coords[ i ][ 0 ] =  vertexData[ ivt[ i ] - 1 ].vt[ 0 ];
					coords[ i ][ 1 ] = -vertexData[ ivt[ i ] - 1 ].vt[ 1 ];
				}
#ifdef DEBUG_PM_OBJ_EX
				printf( "(%4d",iv[ i ] );
				if ( has_vt ) {
					printf( " %4d",ivt[ i ] );
				}
				if ( has_vn ) {
					printf( " %4d",ivn[ i ] );
				}
				printf( ") " );
#endif
			}
#ifdef DEBUG_PM_OBJ_EX
			printf( "\n" );
#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 */
			if ( has_v ) {
				const int numPoints = i;

				/* assign all surface information */
				for ( i = 0; i < numPoints; i++ )
				{
					/*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 ] );
					if( curSurface && curSurface->shader )
						PicoSetSurfaceColor( curSurface, 0, ( curVertex + i ), curSurface->shader->diffuseColor );
				}
				/* add triangles */
				for ( i = 1; i < numPoints - 1; ++i )
				{
					PicoSetSurfaceIndex( curSurface,( curFace * 3 + 2 ),(picoIndex_t)( curVertex + 0 ) );
					PicoSetSurfaceIndex( curSurface,( curFace * 3 + 1 ),(picoIndex_t)( curVertex + i ) );
					PicoSetSurfaceIndex( curSurface,( curFace * 3 + 0 ),(picoIndex_t)( curVertex + i + 1 ) );
					curFace++;
				}
				/* increase vertex count */
				curVertex += numPoints;
			}
		}
		else if ( !_pico_stricmp( p->token,"usemtl" ) ) {
			picoShader_t *shader;
			char *name;

			/* get material name */
			name = _pico_parse( p,0 );

			if ( curFace != 0 || curSurface == NULL ) {
				_pico_printf( PICO_WARNING,"No group defined for usemtl, so creating an autoSurface in OBJ, line %d.",p->curLine );
				AUTO_GROUPNAME( autoGroupNameBuf );
				NEW_SURFACE( autoGroupNameBuf );
			}

			/* validate material name */
			if ( name == NULL || !strlen( name ) ) {
				_pico_printf( PICO_ERROR,"Missing material name in OBJ, line %d.",p->curLine );
			}
			else
			{
				shader = PicoFindShader( model, name, 1 );
				if ( shader == NULL ) {
					_pico_printf( PICO_WARNING, "Undefined material name \"%s\" in OBJ, line %d. Making a default shader.", name, p->curLine );

					/* create a new pico shader */
					shader = PicoNewShader( model );
					if ( shader != NULL ) {
						PicoSetShaderName( shader,name );
						PicoSetShaderMapName( shader,name );
						PicoSetSurfaceShader( curSurface, shader );
					}
				}
				else
				{
					PicoSetSurfaceShader( curSurface, shader );
				}
			}
		}
		/* 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;
}