MCPcopy Create free account
hub / github.com/TorqueGameEngines/Torque3D / createTriMesh

Method createTriMesh

Engine/source/ts/tsMeshFit.cpp:323–389  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

321}
322
323TSMesh* 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();

Callers

nothing calls this directly

Calls 12

copyMeshMethod · 0.80
incrementMethod · 0.80
createTangentsMethod · 0.80
mCrossFunction · 0.50
setFlagsMethod · 0.45
reserveMethod · 0.45
push_backMethod · 0.45
setMethod · 0.45
setSizeMethod · 0.45
sizeMethod · 0.45
normalizeMethod · 0.45
lastMethod · 0.45

Tested by

no test coverage detected