RenderablePicoModel::RenderablePicoModel (picoModel_t* mod) { // Get the number of surfaces to create const int nSurf = PicoGetModelNumSurfaces(mod); // Create a RenderablePicoSurface for each surface in the structure for (int n = 0; n < nSurf; ++n) { // Retrieve the surface, discarding it if it is null or non-triangulated (?) picoSurface_t* surf = PicoGetModelSurface(mod, n); if (surf == 0 || PicoGetSurfaceType(surf) != PICO_TRIANGLES) continue; // Fix the normals of the surface (?) PicoFixSurfaceNormals(surf); // Create the RenderablePicoSurface object and add it to the vector RenderablePicoSurface rSurf = RenderablePicoSurface(surf); _surfVec.push_back(rSurf); // Extend the model AABB to include the surface's AABB aabb_extend_by_aabb(_localAABB, rSurf.localAABB()); } const int nShaders = PicoGetModelNumShaders(mod); for (int n = 0; n < nShaders; n++) { const picoShader_t *shader = PicoGetModelShader(mod, n); if (shader) { modelSkinList.push_back(shader->name); } } std::stringstream polyCountStream; const int polyCount = getPolyCount(); polyCountStream << polyCount; polyCountStr = polyCountStream.str(); std::stringstream surfaceCountStream; const int surfaceCount = getSurfaceCount(); surfaceCountStream << surfaceCount; surfaceCountStr = surfaceCountStream.str(); std::stringstream vertexCountStream; const int vertexCount = getVertexCount(); vertexCountStream << vertexCount; vertexCountStr = vertexCountStream.str(); }
/* _ms3d_load: * loads a milkshape3d model file. */ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){ picoModel_t *model; unsigned char *bufptr, *bufptr0; int shaderRefs[ MS3D_MAX_GROUPS ]; int numGroups; int numMaterials; // unsigned char *ptrToGroups; int numVerts; unsigned char *ptrToVerts; int numTris; unsigned char *ptrToTris; int i,k,m; /* create new pico model */ model = PicoNewModel(); if ( model == NULL ) { return NULL; } /* do model setup */ PicoSetModelFrameNum( model, frameNum ); PicoSetModelName( model, fileName ); PicoSetModelFileName( model, fileName ); bufptr0 = bufptr = (picoByte_t*) _pico_alloc( bufSize ); memcpy( bufptr, buffer, bufSize ); /* skip header */ bufptr += sizeof( TMsHeader ); /* get number of vertices */ bufptr = GetWord( bufptr,&numVerts ); ptrToVerts = bufptr; #ifdef DEBUG_PM_MS3D printf( "NumVertices: %d\n",numVerts ); #endif /* swap verts */ for ( i = 0; i < numVerts; i++ ) { TMsVertex *vertex; vertex = (TMsVertex *)bufptr; bufptr += sizeof( TMsVertex ); vertex->xyz[ 0 ] = _pico_little_float( vertex->xyz[ 0 ] ); vertex->xyz[ 1 ] = _pico_little_float( vertex->xyz[ 1 ] ); vertex->xyz[ 2 ] = _pico_little_float( vertex->xyz[ 2 ] ); #ifdef DEBUG_PM_MS3D_EX_ printf( "Vertex: x: %f y: %f z: %f\n", msvd[i]->vertex[0], msvd[i]->vertex[1], msvd[i]->vertex[2] ); #endif } /* get number of triangles */ bufptr = GetWord( bufptr,&numTris ); ptrToTris = bufptr; #ifdef DEBUG_PM_MS3D printf( "NumTriangles: %d\n",numTris ); #endif /* swap tris */ for ( i = 0; i < numTris; i++ ) { TMsTriangle *triangle; triangle = (TMsTriangle *)bufptr; bufptr += sizeof( TMsTriangle ); triangle->flags = _pico_little_short( triangle->flags ); /* run through all tri verts */ for ( k = 0; k < 3; k++ ) { /* swap tex coords */ triangle->s[ k ] = _pico_little_float( triangle->s[ k ] ); triangle->t[ k ] = _pico_little_float( triangle->t[ k ] ); /* swap fields */ triangle->vertexIndices[ k ] = _pico_little_short( triangle->vertexIndices[ k ] ); triangle->vertexNormals[ 0 ][ k ] = _pico_little_float( triangle->vertexNormals[ 0 ][ k ] ); triangle->vertexNormals[ 1 ][ k ] = _pico_little_float( triangle->vertexNormals[ 1 ][ k ] ); triangle->vertexNormals[ 2 ][ k ] = _pico_little_float( triangle->vertexNormals[ 2 ][ k ] ); /* check for out of range indices */ if ( triangle->vertexIndices[ k ] >= numVerts ) { _pico_printf( PICO_ERROR,"Vertex %d index %d out of range (%d, max %d)",i,k,triangle->vertexIndices[k],numVerts - 1 ); PicoFreeModel( model ); _pico_free( bufptr0 ); return NULL; /* yuck */ } } } /* get number of groups */ bufptr = GetWord( bufptr,&numGroups ); // ptrToGroups = bufptr; #ifdef DEBUG_PM_MS3D printf( "NumGroups: %d\n",numGroups ); #endif /* run through all groups in model */ for ( i = 0; i < numGroups && i < MS3D_MAX_GROUPS; i++ ) { picoSurface_t *surface; TMsGroup *group; group = (TMsGroup *)bufptr; bufptr += sizeof( TMsGroup ); /* we ignore hidden groups */ if ( group->flags & MS3D_HIDDEN ) { bufptr += ( group->numTriangles * 2 ) + 1; continue; } /* forced null term of group name */ group->name[ 31 ] = '\0'; /* create new pico surface */ surface = PicoNewSurface( model ); if ( surface == NULL ) { PicoFreeModel( model ); _pico_free( bufptr0 ); return NULL; } /* do surface setup */ PicoSetSurfaceType( surface,PICO_TRIANGLES ); PicoSetSurfaceName( surface,group->name ); /* process triangle indices */ for ( k = 0; k < group->numTriangles; k++ ) { TMsTriangle *triangle; unsigned int triangleIndex; /* get triangle index */ bufptr = GetWord( bufptr,(int *)&triangleIndex ); /* get ptr to triangle data */ triangle = (TMsTriangle *)( ptrToTris + ( sizeof( TMsTriangle ) * triangleIndex ) ); /* run through triangle vertices */ for ( m = 0; m < 3; m++ ) { TMsVertex *vertex; unsigned int vertexIndex; picoVec2_t texCoord; /* get ptr to vertex data */ vertexIndex = triangle->vertexIndices[ m ]; vertex = (TMsVertex *)( ptrToVerts + ( sizeof( TMsVertex ) * vertexIndex ) ); /* store vertex origin */ PicoSetSurfaceXYZ( surface,vertexIndex,vertex->xyz ); /* store vertex color */ PicoSetSurfaceColor( surface,0,vertexIndex,white ); /* store vertex normal */ PicoSetSurfaceNormal( surface,vertexIndex,triangle->vertexNormals[ m ] ); /* store current face vertex index */ PicoSetSurfaceIndex( surface,( k * 3 + ( 2 - m ) ),(picoIndex_t)vertexIndex ); /* get texture vertex coord */ texCoord[ 0 ] = triangle->s[ m ]; texCoord[ 1 ] = -triangle->t[ m ]; /* flip t */ /* store texture vertex coord */ PicoSetSurfaceST( surface,0,vertexIndex,texCoord ); } } /* store material */ shaderRefs[ i ] = *bufptr++; #ifdef DEBUG_PM_MS3D printf( "Group %d: '%s' (%d tris)\n",i,group->name,group->numTriangles ); #endif } /* get number of materials */ bufptr = GetWord( bufptr,&numMaterials ); #ifdef DEBUG_PM_MS3D printf( "NumMaterials: %d\n",numMaterials ); #endif /* run through all materials in model */ for ( i = 0; i < numMaterials; i++ ) { picoShader_t *shader; picoColor_t ambient,diffuse,specular; TMsMaterial *material; int k; material = (TMsMaterial *)bufptr; bufptr += sizeof( TMsMaterial ); /* null term strings */ material->name [ 31 ] = '\0'; material->texture [ 127 ] = '\0'; material->alphamap[ 127 ] = '\0'; /* ltrim strings */ _pico_strltrim( material->name ); _pico_strltrim( material->texture ); _pico_strltrim( material->alphamap ); /* rtrim strings */ _pico_strrtrim( material->name ); _pico_strrtrim( material->texture ); _pico_strrtrim( material->alphamap ); /* create new pico shader */ shader = PicoNewShader( model ); if ( shader == NULL ) { PicoFreeModel( model ); _pico_free( bufptr0 ); return NULL; } /* scale shader colors */ for ( k = 0; k < 4; k++ ) { ambient [ k ] = (picoByte_t) ( material->ambient[ k ] * 255 ); diffuse [ k ] = (picoByte_t) ( material->diffuse[ k ] * 255 ); specular[ k ] = (picoByte_t) ( material->specular[ k ] * 255 ); } /* set shader colors */ PicoSetShaderAmbientColor( shader,ambient ); PicoSetShaderDiffuseColor( shader,diffuse ); PicoSetShaderSpecularColor( shader,specular ); /* set shader transparency */ PicoSetShaderTransparency( shader,material->transparency ); /* set shader shininess (0..127) */ PicoSetShaderShininess( shader,material->shininess ); /* set shader name */ PicoSetShaderName( shader,material->name ); /* set shader texture map name */ PicoSetShaderMapName( shader,material->texture ); #ifdef DEBUG_PM_MS3D printf( "Material %d: '%s' ('%s','%s')\n",i,material->name,material->texture,material->alphamap ); #endif } /* assign shaders to surfaces */ for ( i = 0; i < numGroups && i < MS3D_MAX_GROUPS; i++ ) { picoSurface_t *surface; picoShader_t *shader; /* sanity check */ if ( shaderRefs[ i ] >= MS3D_MAX_MATERIALS || shaderRefs[ i ] < 0 ) { continue; } /* get surface */ surface = PicoGetModelSurface( model,i ); if ( surface == NULL ) { continue; } /* get shader */ shader = PicoGetModelShader( model,shaderRefs[ i ] ); if ( shader == NULL ) { continue; } /* assign shader */ PicoSetSurfaceShader( surface,shader ); #ifdef DEBUG_PM_MS3D printf( "Mapped: %d ('%s') to %d (%s)\n", shaderRefs[i],shader->name,i,surface->name ); #endif } /* return allocated pico model */ _pico_free( bufptr0 ); return model; // return NULL; }