ObjectDB *getObjectDB( LWItemID id, GlobalFunc *global ) { LWObjectInfo *objinfo; LWMeshInfo *mesh; LWPntID ptid; ObjectDB *odb; const char *name; int npts, npols, nverts, i, j, k, ok = 0; /* get the object info global */ objinfo = global( LWOBJECTINFO_GLOBAL, GFUSE_TRANSIENT ); if ( !objinfo ) return NULL; /* get the mesh info for the object */ mesh = objinfo->meshInfo( id, 1 ); if ( !mesh ) return NULL; /* alloc the object database */ odb = calloc( 1, sizeof( ObjectDB )); if ( !odb ) goto Finish; odb->id = id; name = objinfo->filename( id ); odb->filename = malloc( strlen( name ) + 1 ); if ( !odb->filename ) goto Finish; strcpy( odb->filename, name ); /* alloc and init the points array */ npts = mesh->numPoints( mesh ); odb->pt = calloc( npts, sizeof( DBPoint )); if ( !odb->pt ) goto Finish; if ( mesh->scanPoints( mesh, pntScan, odb )) goto Finish; /* alloc and init the polygons array */ npols = mesh->numPolygons( mesh ); odb->pol = calloc( npols, sizeof( DBPolygon )); if ( !odb->pol ) goto Finish; if ( mesh->scanPolys( mesh, polScan, odb )) goto Finish; /* get the vertices of each polygon */ for ( i = 0; i < npols; i++ ) { nverts = mesh->polSize( mesh, odb->pol[ i ].id ); odb->pol[ i ].v = calloc( nverts, sizeof( DBPolVert )); if ( !odb->pol[ i ].v ) goto Finish; odb->pol[ i ].nverts = nverts; for ( j = 0; j < nverts; j++ ) { ptid = mesh->polVertex( mesh, odb->pol[ i ].id, j ); odb->pol[ i ].v[ j ].index = findVert( odb, ptid ); } } /* count the number of polygons per point */ for ( i = 0; i < npols; i++ ) for ( j = 0; j < odb->pol[ i ].nverts; j++ ) ++odb->pt[ odb->pol[ i ].v[ j ].index ].npols; /* alloc per-point polygon arrays */ for ( i = 0; i < npts; i++ ) { if ( odb->pt[ i ].npols == 0 ) continue; odb->pt[ i ].pol = calloc( odb->pt[ i ].npols, sizeof( int )); if ( !odb->pt[ i ].pol ) goto Finish; odb->pt[ i ].npols = 0; } /* fill in polygon array for each point */ for ( i = 0; i < npols; i++ ) { for ( j = 0; j < odb->pol[ i ].nverts; j++ ) { k = odb->pol[ i ].v[ j ].index; odb->pt[ k ].pol[ odb->pt[ k ].npols ] = i; ++odb->pt[ k ].npols; } } /* get the position of each point */ for ( i = 0; i < npts; i++ ) { mesh->pntBasePos( mesh, odb->pt[ i ].id, odb->pt[ i ].pos[ 0 ] ); mesh->pntOtherPos( mesh, odb->pt[ i ].id, odb->pt[ i ].pos[ 1 ] ); } /* init the point search array */ /* Ordinarily, you won't need this because you can look up points by their IDs. Uncomment this if you do need to search by point position instead. The second argument is 0 for base position and 1 for final position. if ( !initPointSearch( odb, 0 )) goto Finish; */ /* calculate the normal of each polygon */ getPolyNormals( odb, 0 ); getPolyNormals( odb, 1 ); /* get the vmaps */ if ( !getObjectVMaps( odb, mesh, global )) goto Finish; /* get the surfaces */ if ( !getObjectSurfs( odb, mesh, global )) goto Finish; /* calculate vertex normals */ getVertNormals( odb, 0 ); getVertNormals( odb, 1 ); /* done */ ok = 1; Finish: if ( mesh->destroy ) mesh->destroy( mesh ); if ( !ok ) { freeObjectDB( odb ); return NULL; } return odb; }
ObjectDB *getObjectDB( LWItemID id, GlobalFunc *global ) { LWObjectInfo *objinfo; LWMeshInfo *mesh; LWPntID ptid; ObjectDB *odb; const char *name; int npts, npols, nverts, i, j, k, ok = 0; /* get the object info global */ objinfo = (st_LWObjectInfo*)global( LWOBJECTINFO_GLOBAL, GFUSE_TRANSIENT ); if ( !objinfo ) return NULL; /* get the mesh info for the object */ mesh = objinfo->meshInfo( id, 1 ); if ( !mesh ) return NULL; /* alloc the object database */ odb = (st_ObjectDB*)calloc( 1, sizeof( ObjectDB )); if ( !odb ) goto Finish; odb->id = id; name = objinfo->filename( id ); odb->filename = (char*)malloc( xr_strlen( name ) + 1 ); if ( !odb->filename ) goto Finish; strcpy( odb->filename, name ); /* alloc and init the points array */ npts = mesh->numPoints( mesh ); odb->pt = (st_DBPoint*)calloc( npts, sizeof( DBPoint )); if ( !odb->pt ) goto Finish; if ( mesh->scanPoints( mesh, (int (__cdecl *)(void *,struct st_GCoreVertex *))pntScan, odb )) goto Finish; /* alloc and init the polygons array */ npols = mesh->numPolygons( mesh ); odb->pol = (st_DBPolygon*)calloc( npols, sizeof( DBPolygon )); if ( !odb->pol ) goto Finish; if ( mesh->scanPolys( mesh, (int (__cdecl *)(void *,struct st_GCorePolygon *))polScan, odb )) goto Finish; /* get the vertices of each polygon */ for ( i = 0; i < npols; i++ ) { nverts = mesh->polSize( mesh, odb->pol[ i ].id ); odb->pol[ i ].v = (st_DBPolVert*)calloc( nverts, sizeof( DBPolVert )); if ( !odb->pol[ i ].v ) goto Finish; odb->pol[ i ].nverts = nverts; for ( j = 0; j < nverts; j++ ) { ptid = mesh->polVertex( mesh, odb->pol[ i ].id, j ); odb->pol[ i ].v[ j ].index = findVert( odb, ptid ); } } /* count the number of polygons per point */ for ( i = 0; i < npols; i++ ) for ( j = 0; j < odb->pol[ i ].nverts; j++ ) ++odb->pt[ odb->pol[ i ].v[ j ].index ].npols; /* alloc per-point polygon arrays */ for ( i = 0; i < npts; i++ ) { if ( odb->pt[ i ].npols == 0 ) continue; odb->pt[ i ].pol = (int*)calloc( odb->pt[ i ].npols, sizeof( int )); if ( !odb->pt[ i ].pol ) goto Finish; odb->pt[ i ].npols = 0; } /* fill in polygon array for each point */ for ( i = 0; i < npols; i++ ) { for ( j = 0; j < odb->pol[ i ].nverts; j++ ) { k = odb->pol[ i ].v[ j ].index; odb->pt[ k ].pol[ odb->pt[ k ].npols ] = i; ++odb->pt[ k ].npols; } } /* get the base position of each point */ for ( i = 0; i < npts; i++ ) mesh->pntBasePos( mesh, odb->pt[ i ].id, odb->pt[ i ].pos[ 0 ] ); /* init the point search array */ /* do this here if you need to search by base pos, or later if you need to search by final pos */ // if ( !initPointSearch( odb, 0 )) // goto Finish; /* calculate the base normal of each polygon */ getPolyNormals( odb, 0 ); /* get the vmaps */ if ( !getObjectVMaps( odb, mesh, global )) goto Finish; /* get the surfaces */ if ( !getObjectSurfs( odb, mesh, global )) goto Finish; /* calculate initial vertex normals */ getVertNormals( odb, 0 ); /* done */ ok = 1; Finish: if ( mesh->destroy ) mesh->destroy( mesh ); if ( !ok ) { freeObjectDB( odb ); return NULL; } return odb; }