Generates vertex normals for a mesh.

HRESULT ComputeNormals(
   _In_reads_(nFaces*3) const uint16_t* indices, _In_ size_t nFaces,
   _In_reads_(nVerts) const XMFLOAT3* positions, _In_ size_t nVerts, 
   _In_ DWORD flags,
   _Out_writes_(nVerts) XMFLOAT3* normals );

HRESULT ComputeNormals(
   _In_reads_(nFaces*3) const uint32_t* indices, _In_ size_t nFaces,
   _In_reads_(nVerts) const XMFLOAT3* positions, _In_ size_t nVerts, 
   _In_ DWORD flags,
   _Out_writes_(nVerts) XMFLOAT3* normals );

Parameters

flags: A combination of control flags
  • CNORM_DEFAULT is used to compute vertex normals by averaging of the face normals weighted by angle.
  • CNORM_WEIGHT_BY_AREA is used to compute vertex normals by averaging of the face normals weighted by triangle area.
  • CNORM_WEIGHT_EQUAL is used to compute vertex normals by average the face normals with equal weight.
  • CNORM_WIND_CW is used to indicate that the face winding order is clockwise, rather than the default of counter-clockwise winding. This impacts the direction of the generated face normals used in this computation.

Remarks

These functions assume the triangular mesh description is valid. See Mesh cleanup and validation

Example

std::unique_ptr<WaveFrontReader<uint16_t>> mesh( new WaveFrontReader<uint16_t>() );

if ( FAILED( mesh->Load( L"test.obj" ) ) )
   // Error

if ( mesh->hasNormals )
   // Skip next computation

size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();

std::unique_ptr<XMFLOAT3[]> pos( new XMFLOAT3[ nVerts ] );
for( size_t j = 0; j < nVerts; ++j )
   pos[ j ] = mesh->vertices[ j ].position;

std::unique_ptr<XMFLOAT3[]> normals( new XMFLOAT3[ nVerts ] );
if ( FAILED( ComputeNormals( &mesh->indices.front(), nFaces,
   pos.get(), nVerts, CNORM_DEFAULT, normals.get() ) ) )
   // Error

Further Reading

S Jin, R R Lewis, and D West; "A comparison of algorithms for vertex normal computation".
http://www.tricity.wsu.edu/~bobl/personal/mypubs/2003_vertnorm_tvc.pdf

Nelson Max, "Weights for Computing Vertex Normals from Facet Normals"
https://computing.llnl.gov/vis/images/pdf/max_jgt99.pdf

Max Wagner, "Generating Vertex Normals"
http://www.emeyex.com/site/tuts/VertexNormals.pdf

Last edited Nov 20, 2014 at 6:57 PM by walbourn, version 4