4 polyset_t *Polyset_SplitSets( polyset_t *psets, int numpolysets, int *pNumNewPolysets, int maxTris )
7 int numNewPolysets = 0;
8 int numSplitPolysets = 0;
12 for ( p = 0; p < numpolysets; p++ )
14 numNewPolysets += psets[p].numtriangles / maxTris + 1;
17 if ( numNewPolysets == numpolysets )
20 printf( "Warning: creating %d polysets from input of %d polysets\n", numNewPolysets, numpolysets );
22 newpsets = calloc( sizeof( polyset_t ) * numNewPolysets, 1 );
24 for ( np = 0, op = 0; op < numpolysets; op++ )
26 numSplitPolysets = ( psets[op].numtriangles / ( maxTris + 1 ) ) + 1;
27 if ( numSplitPolysets == 1 )
29 memcpy( &newpsets[np], &psets[op], sizeof( polyset_t ) );
36 // split this pset into multiple smaller psets
37 for ( p = 0; p < numSplitPolysets; p++, np++ )
39 memcpy( &newpsets[np], &psets[op], sizeof( polyset_t ) );
41 newpsets[np].triangles = psets[op].triangles + sumTriangles;
43 if ( sumTriangles + maxTris > psets[op].numtriangles )
44 newpsets[np].numtriangles = psets[op].numtriangles - sumTriangles;
46 newpsets[np].numtriangles = maxTris;
48 sumTriangles += newpsets[np].numtriangles;
53 *pNumNewPolysets = numNewPolysets;
58 polyset_t *Polyset_LoadSets( const char *file, int *numpolysets, int maxTrisPerSet )
61 polyset_t *finalpsets;
66 if ( strstr( file, ".3DS" ) || strstr( file, ".3ds" ) )
67 _3DS_LoadPolysets( file, &psets, numpolysets, g_verbose );
69 Error( "TRI files no longer supported" );
70 // TRI_LoadPolysets( file, &psets, numpolysets );
76 for ( i = 0; i < psets; i++ )
80 for ( j = 0; j < psets[i].numtriangles; j++ )
87 // split polysets if necessary
89 finalpsets = Polyset_SplitSets( psets, *numpolysets, numpolysets, maxTrisPerSet );
94 polyset_t *Polyset_CollapseSets( polyset_t *psets, int numpolysets )
99 polyset_t *oldpsets = psets;
102 // no tag checking because this is an $oldbase and thus shouldn't have any
105 for ( p = 0; p < numpolysets; p++ )
107 sumtriangles += oldpsets[p].numtriangles;
110 psets = calloc( 1, sizeof( polyset_t ) );
111 psets[0].numtriangles = sumtriangles;
112 psets[0].triangles = malloc( MD3_MAX_TRIANGLES * sizeof( triangle_t ) );
114 // each call to "LoadPolysets" only allocates a single large chunk of
115 // triangle memory that is utilized by all the polysets loaded by
117 memcpy( psets[0].triangles, oldpsets[0].triangles, sizeof( triangle_t ) * sumtriangles );
119 free( oldpsets[0].triangles );
125 static float SnapFloat( float x )
129 x *= 1.0f / MD3_XYZ_SCALE;
137 void Polyset_SnapSets( polyset_t *psets, int numpolysets )
141 for ( p = 0; p < numpolysets; p++ )
145 for ( t = 0; t < psets[p].numtriangles; t++ )
149 for ( v = 0; v < 3; v++ )
151 psets[p].triangles[t].verts[v][0] = SnapFloat( psets[p].triangles[t].verts[v][0] );
152 psets[p].triangles[t].verts[v][1] = SnapFloat( psets[p].triangles[t].verts[v][1] );
153 psets[p].triangles[t].verts[v][2] = SnapFloat( psets[p].triangles[t].verts[v][2] );
159 void Polyset_ComputeNormals( polyset_t *psets, int numpolysets )
163 int vertexIndex[MD3_MAX_TRIANGLES][3];
164 vec3_t verts[MD3_MAX_VERTS];
165 vec3_t normals[MD3_MAX_VERTS];
166 vec3_t faceNormals[MD3_MAX_TRIANGLES];
169 // iterate through polysets
171 for ( p = 0; p < numpolysets; p++ )
173 int numUniqueVertices = 0;
175 assert( psets[p].numtriangles < MD3_MAX_TRIANGLES );
177 memset( vertexIndex, 0xff, sizeof( vertexIndex ) );
178 memset( verts, 0, sizeof( verts ) );
179 memset( normals, 0, sizeof( normals ) );
184 for ( t = 0; t < psets[p].numtriangles; t++ )
188 for ( j = 0; j < 3; j++ )
190 for ( i = 0; i < numUniqueVertices; i++ )
192 if ( VectorCompare( psets[p].triangles[t].verts[j], verts[i] ) )
197 if ( i == numUniqueVertices )
199 vertexIndex[t][j] = numUniqueVertices;
200 VectorCopy( (psets[p].triangles[t].verts[j]), (verts[numUniqueVertices]) );
205 vertexIndex[t][j] = i;
211 // compute face normals
213 for ( t = 0; t < psets[p].numtriangles; t++ )
215 vec3_t side0, side1, facenormal;
217 VectorSubtract( psets[p].triangles[t].verts[0], psets[p].triangles[t].verts[1], side0 );
218 VectorSubtract( psets[p].triangles[t].verts[2], psets[p].triangles[t].verts[1], side1);
220 CrossProduct( side0, side1, facenormal );
221 VectorNormalize( facenormal, faceNormals[t] );
225 // sum normals and copy them back
227 for ( i = 0; i < numUniqueVertices; i++ )
229 for ( t = 0; t < psets[p].numtriangles; t++ )
231 if ( vertexIndex[t][0] == i ||
232 vertexIndex[t][1] == i ||
233 vertexIndex[t][2] == i )
235 normals[i][0] += faceNormals[t][0];
236 normals[i][1] += faceNormals[t][1];
237 normals[i][2] += faceNormals[t][2];
240 VectorNormalize( normals[i], normals[i] );
244 for ( t = 0; t < psets[p].numtriangles; t++ )
246 VectorCopy( normals[vertexIndex[t][0]], psets[p].triangles[t].normals[0] );
247 VectorCopy( normals[vertexIndex[t][1]], psets[p].triangles[t].normals[1] );
248 VectorCopy( normals[vertexIndex[t][2]], psets[p].triangles[t].normals[2] );