Add geom::smooth_normals
This commit is contained in:
parent
82a81c4c77
commit
8e081d6d06
1 changed files with 20 additions and 0 deletions
|
|
@ -123,6 +123,26 @@ namespace psemek::geom
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Vertex, typename Index>
|
||||||
|
auto smooth_normals(std::vector<Vertex> const & vertices, std::vector<triangle<Index>> const & triangles)
|
||||||
|
{
|
||||||
|
using vector_type = std::decay_t<decltype(vertices[0] - vertices[0])>;
|
||||||
|
std::vector<vector_type> normals(vertices.size(), vector_type::zero());
|
||||||
|
|
||||||
|
for (auto const & t : triangles)
|
||||||
|
{
|
||||||
|
auto const n = cross(vertices[t[1]] - vertices[t[0]], vertices[t[2]] - vertices[t[0]]);
|
||||||
|
normals[t[0]] += n;
|
||||||
|
normals[t[1]] += n;
|
||||||
|
normals[t[2]] += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto & n : normals)
|
||||||
|
n = normalized(n);
|
||||||
|
|
||||||
|
return normals;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Index = std::uint32_t, typename Vertex>
|
template <typename Index = std::uint32_t, typename Vertex>
|
||||||
std::vector<triangle<Index>> triangulate_convex(std::vector<Vertex> const & v)
|
std::vector<triangle<Index>> triangulate_convex(std::vector<Vertex> const & v)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue