Skip to content

ComputeNormals

Chuck Walbourn edited this page Mar 27, 2018 · 10 revisions

Generates vertex normals for a mesh.

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

HRESULT ComputeNormals(
   const uint32_t* indices, size_t nFaces,
   const XMFLOAT3* positions, size_t nVerts, 
   DWORD flags,
   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 Validate and Clean

Example

auto mesh = std::make_unique<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.data(), 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". link

Nelson Max, "Weights for Computing Vertex Normals from Facet Normals" link

Max Wagner, "Generating Vertex Normals" link

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Windows 7 Service Pack 1
  • Xbox One
  • Xbox Series X|S
  • Windows Subsystem for Linux

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • GCC 10.5, 11.4, 12.3
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 11

DirectX Tool Kit for DirectX 12

DirectXTex

DirectXMath

Tools

Test Suite

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally