Subject: CGAL users discussion list
List archive
[cgal-discuss] Properly oriented Polyhedron_3 vertex normals for rendering with OpenGL
Chronological Thread
- From: Petros Kataras <>
- To:
- Subject: [cgal-discuss] Properly oriented Polyhedron_3 vertex normals for rendering with OpenGL
- Date: Sat, 6 Feb 2016 18:44:38 +0100
- Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None ; spf=Pass ; spf=None
- Ironport-phdr: 9a23:8O/aqBTbhrnrjGOp+HzdMQbx+Npsv+yvbD5Q0YIujvd0So/mwa64ZxyN2/xhgRfzUJnB7Loc0qyN4/+mAzBLsMbJmUtBWaIPfidNsd8RkQ0kDZzNImzAB9muURYHGt9fXkRu5XCxPBsdMs//Y1rPvi/6tmZKSV3BPAZ4bt74BpTVx5zukbvipNuJP04Z1HKUWvBbElaflU3prM4YgI9veO4a6yDihT92QdlQ3n5iPlmJnhzxtY+a9Z9n9DlM6bp6r5YTGfayQ6NtRrNRCHEqMns++dbwnRjFVwqGoHUGAUsMlR8dOwXZ9hbhFrT4riLgv/E1jCDBO8TxS5g7XD2j6+FgTxq+23RPDCIw7GyC0p84t6lcuh/0/xE=
Hello,
I am trying to render 3d meshes produced by CGAL through OpenGL and I am hitting a wall regarding the calculation of properly oriented normals.
I have reduced my test case to the absolute minimal of creating and rendering a simple tetrahedron generated from Polyhedron_3::make_tetrahedron.
After the creation I am iterating over the vertices of the tetrahedron and calculate the per vertex-normal based on the following functions that I found :
// Compute facet normal
template <class Facet, class Kernel>
typename Kernel::Vector_3 compute_facet_normal(const Facet& f)
{
typedef typename Kernel::Point_3 Point;
typedef typename Kernel::Vector_3 Vector;
typedef typename Facet::Halfedge_around_facet_const_circulator HF_circulator;
Vector normal = CGAL::NULL_VECTOR;
HF_circulator he = f.facet_begin();
HF_circulator end = he;
CGAL_For_all(he,end)
{
const Point& prev = he->prev()->vertex()->point();
const Point& curr = he->vertex()->point();
const Point& next = he->next()->vertex()->point();
Vector n = CGAL::cross_product(next-curr,prev-curr);
normal = normal + n;
}
return normal / std::sqrt(normal * normal);
}
typename Kernel::Vector_3 compute_facet_normal(const Facet& f)
{
typedef typename Kernel::Point_3 Point;
typedef typename Kernel::Vector_3 Vector;
typedef typename Facet::Halfedge_around_facet_const_circulator HF_circulator;
Vector normal = CGAL::NULL_VECTOR;
HF_circulator he = f.facet_begin();
HF_circulator end = he;
CGAL_For_all(he,end)
{
const Point& prev = he->prev()->vertex()->point();
const Point& curr = he->vertex()->point();
const Point& next = he->next()->vertex()->point();
Vector n = CGAL::cross_product(next-curr,prev-curr);
normal = normal + n;
}
return normal / std::sqrt(normal * normal);
}
// Compute vertex normal
template <class Vertex, class Kernel>
typename Kernel::Vector_3 compute_vertex_normal(const Vertex& v)
{
typedef typename Kernel::Vector_3 Vector;
typedef typename Vertex::Halfedge_around_vertex_const_circulator HV_circulator;
typedef typename Vertex::Facet Facet;
Vector normal = CGAL::NULL_VECTOR;
HV_circulator he = v.vertex_begin();
HV_circulator end = he;
CGAL_For_all(he,end)
{
if(!he->is_border())
{
Vector n = compute_facet_normal<Facet,Kernel>(*he->facet());
normal = normal + (n / std::sqrt(n*n));
}
}
return normal / std::sqrt(normal * normal);
}
typename Kernel::Vector_3 compute_vertex_normal(const Vertex& v)
{
typedef typename Kernel::Vector_3 Vector;
typedef typename Vertex::Halfedge_around_vertex_const_circulator HV_circulator;
typedef typename Vertex::Facet Facet;
Vector normal = CGAL::NULL_VECTOR;
HV_circulator he = v.vertex_begin();
HV_circulator end = he;
CGAL_For_all(he,end)
{
if(!he->is_border())
{
Vector n = compute_facet_normal<Facet,Kernel>(*he->facet());
normal = normal + (n / std::sqrt(n*n));
}
}
return normal / std::sqrt(normal * normal);
}
I would expect this to be enough to give me properly oriented normals based on the fact that CGAL maintains CCW order but obviously I am missing something since the result is not correct. The normals of the front facet especially look like the winding order is reversed for the specific triangle. The attached image shows the result that I get.
Is there a way to ensure CCW order on the vertices of facets so that normals are pointing outwards or am I totally missing something?
Thanks in advance for any hints,
Petros
- [cgal-discuss] Properly oriented Polyhedron_3 vertex normals for rendering with OpenGL, Petros Kataras, 02/06/2016
- Re: [cgal-discuss] Properly oriented Polyhedron_3 vertex normals for rendering with OpenGL, Andreas Fabri, 02/06/2016
- Re: [cgal-discuss] Properly oriented Polyhedron_3 vertex normals for rendering with OpenGL, Petros Kataras, 02/07/2016
- Re: [cgal-discuss] Properly oriented Polyhedron_3 vertex normals for rendering with OpenGL, Andreas Fabri, 02/06/2016
Archive powered by MHonArc 2.6.18.