scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
normals.cpp
Go to the documentation of this file.
1#include <scimesh/normals.h>
2
3namespace scimesh {
4
5void compute_vertex_normals(const Mesh &mesh, std::vector<Vec3> &normals) {
6 normals.assign(mesh.vertices.size(), Vec3(0.0f));
7
8 for (const auto &tri : mesh.triangles) {
9 Vec3 face_normal = compute_face_normal(
10 mesh.vertices[tri.v0],
11 mesh.vertices[tri.v1],
12 mesh.vertices[tri.v2]);
13
14 normals[tri.v0] += face_normal;
15 normals[tri.v1] += face_normal;
16 normals[tri.v2] += face_normal;
17 }
18
19 for (auto &n : normals) {
20 float len = glm::length(n);
21 if (len > 1e-12f)
22 n /= len;
23 else
24 n = Vec3(0.0f, 0.0f, 1.0f);
25 }
26}
27
28} // namespace scimesh
void compute_vertex_normals(const Mesh &mesh, std::vector< Vec3 > &normals)
Compute per-vertex normals by averaging adjacent face normals.
Definition normals.cpp:5
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
Vec3 compute_face_normal(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2)
Compute the unit-length normal vector of a triangle face.
Definition math_utils.h:37
Compute per-vertex surface normals for lighting.
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
std::vector< Vec3 > vertices
3D vertex positions.
Definition mesh.h:86
std::vector< Triangle > triangles
Triangle index triplets.
Definition mesh.h:94