| 321 | } |
| 322 | |
| 323 | TSMesh* MeshFit::createTriMesh( F32* verts, S32 numVerts, U32* indices, S32 numTris ) const |
| 324 | { |
| 325 | TSMesh* mesh = mShape->copyMesh( NULL ); |
| 326 | mesh->numFrames = 1; |
| 327 | mesh->numMatFrames = 1; |
| 328 | mesh->vertsPerFrame = numVerts; |
| 329 | mesh->setFlags(0); |
| 330 | mesh->mNumVerts = numVerts; |
| 331 | |
| 332 | mesh->mIndices.reserve( numTris * 3 ); |
| 333 | for ( S32 i = 0; i < numTris; i++ ) |
| 334 | { |
| 335 | mesh->mIndices.push_back( indices[i*3 + 0] ); |
| 336 | mesh->mIndices.push_back( indices[i*3 + 2] ); |
| 337 | mesh->mIndices.push_back( indices[i*3 + 1] ); |
| 338 | } |
| 339 | |
| 340 | mesh->mVerts.set( verts, numVerts ); |
| 341 | |
| 342 | // Compute mesh normals |
| 343 | mesh->mNorms.setSize( mesh->mVerts.size() ); |
| 344 | for (S32 iNorm = 0; iNorm < mesh->mNorms.size(); iNorm++) |
| 345 | mesh->mNorms[iNorm] = Point3F::Zero; |
| 346 | |
| 347 | // Sum triangle normals for each vertex |
| 348 | for (S32 iInd = 0; iInd < mesh->mIndices.size(); iInd += 3) |
| 349 | { |
| 350 | // Compute the normal for this triangle |
| 351 | S32 idx0 = mesh->mIndices[iInd + 0]; |
| 352 | S32 idx1 = mesh->mIndices[iInd + 1]; |
| 353 | S32 idx2 = mesh->mIndices[iInd + 2]; |
| 354 | |
| 355 | const Point3F& v0 = mesh->mVerts[idx0]; |
| 356 | const Point3F& v1 = mesh->mVerts[idx1]; |
| 357 | const Point3F& v2 = mesh->mVerts[idx2]; |
| 358 | |
| 359 | Point3F n; |
| 360 | mCross(v2 - v0, v1 - v0, &n); |
| 361 | n.normalize(); // remove this to use 'weighted' normals (large triangles will have more effect) |
| 362 | |
| 363 | mesh->mNorms[idx0] += n; |
| 364 | mesh->mNorms[idx1] += n; |
| 365 | mesh->mNorms[idx2] += n; |
| 366 | } |
| 367 | |
| 368 | // Normalize the vertex normals (this takes care of averaging the triangle normals) |
| 369 | for (S32 iNorm = 0; iNorm < mesh->mNorms.size(); iNorm++) |
| 370 | mesh->mNorms[iNorm].normalize(); |
| 371 | |
| 372 | // Set some dummy UVs |
| 373 | mesh->mTverts.setSize( numVerts ); |
| 374 | for ( S32 j = 0; j < mesh->mTverts.size(); j++ ) |
| 375 | mesh->mTverts[j].set( 0, 0 ); |
| 376 | |
| 377 | // Add a single triangle-list primitive |
| 378 | mesh->mPrimitives.increment(); |
| 379 | mesh->mPrimitives.last().start = 0; |
| 380 | mesh->mPrimitives.last().numElements = mesh->mIndices.size(); |
nothing calls this directly
no test coverage detected