From cc663c5e36927a0d1266ba8c49e8117ee0443c7b Mon Sep 17 00:00:00 2001 From: Guillaume Damiand Date: Wed, 13 Nov 2019 12:18:27 +0100 Subject: [PATCH] Bugfix for negate normals. --- GraphicsView/include/CGAL/Buffer_for_vao.h | 52 ++++++++++++------- .../include/CGAL/Qt/Basic_viewer_qt.h | 10 ++-- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/GraphicsView/include/CGAL/Buffer_for_vao.h b/GraphicsView/include/CGAL/Buffer_for_vao.h index 4d4f48a68e1..7ba5a1b7317 100644 --- a/GraphicsView/include/CGAL/Buffer_for_vao.h +++ b/GraphicsView/include/CGAL/Buffer_for_vao.h @@ -151,6 +151,7 @@ public: m_zero_x(true), m_zero_y(true), m_zero_z(true), + m_inverse_normal(false), m_face_started(false) {} @@ -201,6 +202,17 @@ public: bool has_zero_z() const { return m_zero_z; } + void negate_normals() + { + m_inverse_normal=!m_inverse_normal; + for (std::vector*array=m_flat_normal_buffer; array!=nullptr; + array=(array==m_gouraud_normal_buffer?nullptr:m_gouraud_normal_buffer)) + { + for (std::size_t i=0; isize(); ++i) + { (*array)[i]=-(*array)[i]; } + } + } + // 1.1) Add a point, without color. Return the index of the added point. template std::size_t add_point(const KPoint& kp) @@ -445,7 +457,7 @@ public: /// adds `kp` coordinates to `buffer` template - static void add_point_in_buffer(const KPoint& kp, std::vector& buffer) + void add_point_in_buffer(const KPoint& kp, std::vector& buffer) { Local_point p=get_local_point(kp); buffer.push_back(p.x()); @@ -455,16 +467,16 @@ public: /// adds `kv` coordinates to `buffer` template - static void add_normal_in_buffer(const KVector& kv, std::vector& buffer) + void add_normal_in_buffer(const KVector& kv, std::vector& buffer) { - Local_vector n=get_local_vector(kv); + Local_vector n=(m_inverse_normal?-get_local_vector(kv):get_local_vector(kv)); buffer.push_back(n.x()); buffer.push_back(n.y()); buffer.push_back(n.z()); } ///adds `acolor` RGB components to `buffer` - static void add_color_in_buffer(const CGAL::Color& acolor, std::vector& buffer) + void add_color_in_buffer(const CGAL::Color& acolor, std::vector& buffer) { buffer.push_back((float)acolor.red()/(float)255); buffer.push_back((float)acolor.green()/(float)255); @@ -541,23 +553,23 @@ public: if (m_indices_of_points_of_face.size()>0) { add_indexed_point(m_indices_of_points_of_face[i]); - } + } else { add_point(m_points_of_face[i]); // Add the position of the point - if (m_started_face_is_colored) - { add_color(m_color_of_face); } // Add the color - add_flat_normal(normal); // Add the flat normal - // Its smooth normal (if given by the user) - if (m_vertex_normals_for_face.size()>0) - { // Here we have 3 vertex normals; we can use Gouraud - add_gouraud_normal(m_vertex_normals_for_face[i]); - } - else - { // Here user does not provide all vertex normals: we use face normal istead - // and thus we will not be able to use Gouraud - add_gouraud_normal(normal); - } + if (m_started_face_is_colored) + { add_color(m_color_of_face); } // Add the color + add_flat_normal(normal); // Add the flat normal + // Its smooth normal (if given by the user) + if (m_vertex_normals_for_face.size()>0) + { // Here we have 3 vertex normals; we can use Gouraud + add_gouraud_normal(m_vertex_normals_for_face[i]); + } + else + { // Here user does not provide all vertex normals: we use face normal istead + // and thus we will not be able to use Gouraud + add_gouraud_normal(normal); + } } } } @@ -883,8 +895,10 @@ protected: bool m_zero_x; /// True iff all points have x==0 bool m_zero_y; /// True iff all points have y==0 bool m_zero_z; /// True iff all points have z==0 + + bool m_inverse_normal;; - // Local variables, used when we started a new face. + // Local variables, used when we started a new face.g bool m_face_started; bool m_started_face_is_colored; bool m_started_face_has_normal; diff --git a/GraphicsView/include/CGAL/Qt/Basic_viewer_qt.h b/GraphicsView/include/CGAL/Qt/Basic_viewer_qt.h index 87762ba030b..ddb4c99fa57 100644 --- a/GraphicsView/include/CGAL/Qt/Basic_viewer_qt.h +++ b/GraphicsView/include/CGAL/Qt/Basic_viewer_qt.h @@ -329,6 +329,9 @@ public: setWindowTitle(title); resize(500, 450); + + if (inverse_normal) + { negate_all_normals(); } } ~Basic_viewer_qt() @@ -1261,11 +1264,8 @@ protected: void negate_all_normals() { - for (unsigned int k=BEGIN_NORMAL; k