From 737269bf5a074a9aa037ea68c8cb0aa240f6478e Mon Sep 17 00:00:00 2001 From: Bruno Date: Wed, 3 Mar 2021 22:36:59 +0100 Subject: [PATCH] New Primitive LineLoop (and cleanup associated visitors) --- GarbageVisitor.cpp | 5 --- GarbageVisitor.h | 1 - Primitives.cpp | 92 ++++++++++++++++++++++++++++++++++++++-------- Primitives.h | 20 +++++++--- SessionCreator.h | 9 ----- SessionVisitor.cpp | 10 ----- SessionVisitor.h | 1 - Visitor.h | 1 - 8 files changed, 92 insertions(+), 47 deletions(-) diff --git a/GarbageVisitor.cpp b/GarbageVisitor.cpp index a57f3da..9fde05f 100644 --- a/GarbageVisitor.cpp +++ b/GarbageVisitor.cpp @@ -132,11 +132,6 @@ void GarbageVisitor::visit(LineSquare &) } -void GarbageVisitor::visit(LineCircle &n) -{ - -} - void GarbageVisitor::visit(Mesh &n) { diff --git a/GarbageVisitor.h b/GarbageVisitor.h index 6e3c53e..6d8d0a3 100644 --- a/GarbageVisitor.h +++ b/GarbageVisitor.h @@ -30,7 +30,6 @@ public: void visit(FrameBufferSurface& n) override; void visit(LineStrip& n) override; void visit(LineSquare&) override; - void visit(LineCircle& n) override; void visit(Mesh& n) override; void visit(Frame& n) override; diff --git a/Primitives.cpp b/Primitives.cpp index 4cc8c66..4bcf7fd 100644 --- a/Primitives.cpp +++ b/Primitives.cpp @@ -428,7 +428,6 @@ LineStrip::LineStrip(std::vector path, float linewidth) : Primitive(n path_ = path; linewidth_ = 0.002f * linewidth; - size_t index = 0; for(size_t i = 1; i < path_.size(); ++i) { glm::vec3 begin = glm::vec3(path_[i-1], 0.f); @@ -438,19 +437,19 @@ LineStrip::LineStrip(std::vector path, float linewidth) : Primitive(n points_.push_back( begin + perp * linewidth_ ); colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); - indices_.push_back ( index++ ); + indices_.push_back ( indices_.size() ); points_.push_back( begin - perp * linewidth_ ); colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); - indices_.push_back ( index++ ); + indices_.push_back ( indices_.size() ); points_.push_back( end + perp * linewidth_ ); colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); - indices_.push_back ( index++ ); + indices_.push_back ( indices_.size() ); points_.push_back( end - perp * linewidth_ ); colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); - indices_.push_back ( index++ ); + indices_.push_back ( indices_.size() ); } drawMode_ = GL_TRIANGLE_STRIP; @@ -534,7 +533,7 @@ void LineStrip::updatePath() // bind the vertex array and change the point coordinates glBindVertexArray( vao_ ); glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer_); - glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(glm::vec3) * points_.size(), &points_[0] ); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3) * points_.size(), &points_[0] ); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -576,26 +575,89 @@ void LineStrip::accept(Visitor& v) v.visit(*this); } + +LineLoop::LineLoop(std::vector path, float linewidth) : LineStrip(path, linewidth) +{ + // close linestrip loop + glm::vec3 begin = glm::vec3(path_[path_.size()-1], 0.f); + glm::vec3 end = glm::vec3(path_[0], 0.f); + glm::vec3 dir = end - begin; + glm::vec3 perp = glm::cross(dir, glm::vec3(0.f, 0.f, 1.f)); + + points_.push_back( begin + perp * linewidth_ ); + colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); + indices_.push_back ( indices_.size() ); + + points_.push_back( begin - perp * linewidth_ ); + colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); + indices_.push_back ( indices_.size() ); + + points_.push_back( end + perp * linewidth_ ); + colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); + indices_.push_back ( indices_.size() ); + + points_.push_back( end - perp * linewidth_ ); + colors_.push_back( glm::vec4( 1.f, 1.f, 1.f, 1.f ) ); + indices_.push_back ( indices_.size() ); +} + +void LineLoop::updatePath() +{ + glm::vec3 begin; + glm::vec3 end; + glm::vec3 dir; + glm::vec3 perp; + + // redo points_ array + points_.clear(); + size_t i = 1; + for(; i < path_.size(); ++i) + { + begin = glm::vec3(path_[i-1], 0.f); + end = glm::vec3(path_[i], 0.f); + dir = end - begin; + perp = glm::normalize(glm::cross(dir, glm::vec3(0.f, 0.f, 1.f))); + + points_.push_back( begin + perp * linewidth_ ); + points_.push_back( begin - perp * linewidth_ ); + points_.push_back( end + perp * linewidth_ ); + points_.push_back( end - perp * linewidth_ ); + } + + // close linestrip loop + begin = glm::vec3(path_[i-1], 0.f); + end = glm::vec3(path_[0], 0.f); + dir = end - begin; + perp = glm::normalize(glm::cross(dir, glm::vec3(0.f, 0.f, 1.f))); + points_.push_back( begin + perp * linewidth_ ); + points_.push_back( begin - perp * linewidth_ ); + points_.push_back( end + perp * linewidth_ ); + points_.push_back( end - perp * linewidth_ ); + + // bind the vertex array and change the point coordinates + glBindVertexArray( vao_ ); + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer_); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3) * points_.size(), &points_[0] ); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + // re-compute AxisAlignedBoundingBox + bbox_.extend(points_); +} + #define LINE_CIRCLE_DENSITY 72 -LineCircle::LineCircle(float linewidth) : LineStrip(std::vector(LINE_CIRCLE_DENSITY), linewidth) +LineCircle::LineCircle(float linewidth) : LineLoop(std::vector(LINE_CIRCLE_DENSITY), linewidth) { static float a = glm::two_pi() / static_cast(LINE_CIRCLE_DENSITY-1); // loop to build a circle glm::vec3 P(1.f, 0.f, 0.f); - for (int i = 0; i < LINE_CIRCLE_DENSITY - 1 ; i++ ){ + for (int i = 0; i < LINE_CIRCLE_DENSITY - 1; i++ ){ path_[i] = glm::vec2(P); P = glm::rotateZ(P, a); } - path_[LINE_CIRCLE_DENSITY-1] = glm::vec2(1.f, 0.f); updatePath(); } -void LineCircle::accept(Visitor& v) -{ - Primitive::accept(v); - v.visit(*this); -} - diff --git a/Primitives.h b/Primitives.h index 2e34bd2..3f14643 100644 --- a/Primitives.h +++ b/Primitives.h @@ -193,21 +193,31 @@ protected: float linewidth_; uint arrayBuffer_; std::vector path_; - void updatePath(); + virtual void updatePath(); }; +/** + * @brief The LineLoop class is a LineStrip with closed path + */ +class LineLoop : public LineStrip { + +public: + LineLoop(std::vector path, float linewidth = 1.f); + +protected: + void updatePath() override; +}; + /** - * @brief The LineCircle class is a circular LineStrip (diameter = 1.0) + * @brief The LineCircle class is a circular LineLoop (diameter = 1.0) */ -class LineCircle : public LineStrip { +class LineCircle : public LineLoop { public: LineCircle(float linewidth = 1.f); - virtual void accept(Visitor& v) override; - }; diff --git a/SessionCreator.h b/SessionCreator.h index e472b92..3996632 100644 --- a/SessionCreator.h +++ b/SessionCreator.h @@ -23,19 +23,10 @@ public: // Elements of Scene void visit (Node& n) override; - void visit (Scene&) override {} void visit (Group&) override {} void visit (Switch&) override {} void visit (Primitive&) override {} - void visit (Surface&) override {} - void visit (ImageSurface&) override {} - void visit (MediaSurface&) override {} - void visit (FrameBufferSurface&) override {} - void visit (LineStrip&) override {} - void visit (LineSquare&) override {} - void visit (LineCircle&) override {} - void visit (Mesh&) override {} // Elements with attributes void visit (MediaPlayer& n) override; diff --git a/SessionVisitor.cpp b/SessionVisitor.cpp index 337ac9f..2f8139b 100644 --- a/SessionVisitor.cpp +++ b/SessionVisitor.cpp @@ -293,16 +293,6 @@ void SessionVisitor::visit(LineSquare &) } -void SessionVisitor::visit(LineCircle &) -{ - // Node of a different type - xmlCurrent_->SetAttribute("type", "LineCircle"); - -// XMLElement *color = xmlDoc_->NewElement("color"); -// color->InsertEndChild( XMLElementFromGLM(xmlDoc_, n.getColor()) ); -// xmlCurrent_->InsertEndChild(color); -} - void SessionVisitor::visit(Mesh &n) { // Node of a different type diff --git a/SessionVisitor.h b/SessionVisitor.h index bed9508..a16e113 100644 --- a/SessionVisitor.h +++ b/SessionVisitor.h @@ -30,7 +30,6 @@ public: void visit(FrameBufferSurface&) override; void visit(LineStrip& n) override; void visit(LineSquare&) override; - void visit(LineCircle&) override; void visit(Mesh& n) override; void visit(Frame& n) override; diff --git a/Visitor.h b/Visitor.h index c16e156..da0bdff 100644 --- a/Visitor.h +++ b/Visitor.h @@ -56,7 +56,6 @@ public: virtual void visit (FrameBufferSurface&) {} virtual void visit (LineStrip&) {} virtual void visit (LineSquare&) {} - virtual void visit (LineCircle&) {} virtual void visit (Mesh&) {} virtual void visit (Frame&) {} virtual void visit (Handles&) {}