-
Notifications
You must be signed in to change notification settings - Fork 151
ComputeNormals
Chuck Walbourn edited this page Jan 21, 2022
·
10 revisions
DirectXMesh |
---|
Generates vertex normals for a mesh.
HRESULT ComputeNormals(
const uint16_t* indices, size_t nFaces,
const XMFLOAT3* positions, size_t nVerts,
CNORM_FLAGS flags,
XMFLOAT3* normals );
HRESULT ComputeNormals(
const uint32_t* indices, size_t nFaces,
const XMFLOAT3* positions, size_t nVerts,
CNORM_FLAGS flags,
XMFLOAT3* normals );
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.
These functions assume the triangular mesh description is valid. See Validate and Clean
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();
auto pos = std::make_unique<XMFLOAT3[]>(nVerts);
for( size_t j = 0; j < nVerts; ++j )
pos[ j ] = mesh->vertices[ j ].position;
auto normals = std::make_unique<XMFLOAT3[]>(nVerts);
if ( FAILED( ComputeNormals( mesh->indices.data(), nFaces,
pos.get(), nVerts, CNORM_DEFAULT, normals.get() ) ) )
// Error
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
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
- 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
- x86
- x64
- ARM64
- 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
DirectX Tool Kit for DirectX 11