From 57fc66c546b4123ff2ab40a86d64cd751138c2b4 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Mon, 13 Apr 2020 11:48:06 +0200 Subject: [PATCH] Fixed init of Group (actually should NOT init its children nodes). Ensure Nodes deletetion is done. --- CMakeLists.txt | 4 ++++ Mesh.cpp | 7 ++++++- Mesh.h | 1 + Primitives.cpp | 17 +++++++++++++++++ Primitives.h | 7 ++++++- Scene.cpp | 27 +++++++-------------------- Scene.h | 1 - main.cpp | 16 +++++++++------- 8 files changed, 50 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 60cb315..12acf51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,6 +242,10 @@ set(VMIX_RSC_FILES ./rsc/mesh/disk.ply ./rsc/mesh/shadow.ply ./rsc/mesh/shadow.png + ./rsc/mesh/target.ply + ./rsc/mesh/border.ply + ./rsc/mesh/icon_video.ply + ./rsc/mesh/icon_image.ply ) add_executable(${VMIX_BINARY} diff --git a/Mesh.cpp b/Mesh.cpp index db7f9b5..369fbf5 100644 --- a/Mesh.cpp +++ b/Mesh.cpp @@ -336,7 +336,7 @@ Mesh::Mesh(const std::string& path, const std::string& texture) : Primitive(), r Mesh::~Mesh() { - + delete shader_; } void Mesh::init() @@ -373,3 +373,8 @@ void Mesh::accept(Visitor& v) Primitive::accept(v); v.visit(*this); } + +//void Mesh::update( float dt ) +//{ +// scale_.x = scale_.y; +//} diff --git a/Mesh.h b/Mesh.h index 73b2b63..9559684 100644 --- a/Mesh.h +++ b/Mesh.h @@ -22,6 +22,7 @@ public: void init () override; void draw (glm::mat4 modelview, glm::mat4 projection) override; void accept (Visitor& v) override; +// void update (float dt) override; inline std::string getResource() const { return resource_; } inline std::string getTexture() const { return texturefilename_; } diff --git a/Primitives.cpp b/Primitives.cpp index 6e45a86..f596b40 100644 --- a/Primitives.cpp +++ b/Primitives.cpp @@ -41,6 +41,11 @@ ImageSurface::ImageSurface(const std::string& path) : Primitive(), resource_(pat drawMode_ = GL_TRIANGLE_STRIP; } +ImageSurface::~ImageSurface() +{ + if (shader_) + delete shader_; +} void ImageSurface::init() { @@ -166,6 +171,12 @@ Points::Points(std::vector points, glm::vec4 color, uint pointsize) : pointsize_ = pointsize; } +Points::~Points() +{ + if (shader_) + delete shader_; +} + void Points::init() { Primitive::init(); @@ -200,6 +211,12 @@ LineStrip::LineStrip(std::vector points, glm::vec4 color, uint linewi drawMode_ = GL_LINE_STRIP; } +LineStrip::~LineStrip() +{ + if (shader_) + delete shader_; +} + void LineStrip::init() { Primitive::init(); diff --git a/Primitives.h b/Primitives.h index 877c47d..f2e0fee 100644 --- a/Primitives.h +++ b/Primitives.h @@ -12,6 +12,7 @@ class ImageSurface : public Primitive { public: ImageSurface(const std::string& path = "" ); + ~ImageSurface(); void init () override; void draw (glm::mat4 modelview, glm::mat4 projection) override; @@ -25,7 +26,7 @@ protected: }; -// Draw a Rectangle (triangle strip) with a media as animated texture +// Draw a Rectangle with a media as animated texture class MediaPlayer; class MediaSurface : public ImageSurface { @@ -66,6 +67,7 @@ class Points : public Primitive { public: Points(std::vector points, glm::vec4 color, uint pointsize = 10); + ~Points(); virtual void init() override; virtual void draw(glm::mat4 modelview, glm::mat4 projection) override; @@ -85,6 +87,7 @@ class LineStrip : public Primitive { public: LineStrip(std::vector points, glm::vec4 color, uint linewidth = 1); + ~LineStrip(); virtual void init() override; virtual void draw(glm::mat4 modelview, glm::mat4 projection) override; @@ -97,6 +100,7 @@ public: inline uint getLineWidth() const { return linewidth_; } }; + class LineSquare : public LineStrip { void deleteGLBuffers_() override {} @@ -108,6 +112,7 @@ public: void accept(Visitor& v) override; }; + class LineCircle : public LineStrip { void deleteGLBuffers_() override {} diff --git a/Scene.cpp b/Scene.cpp index 2a7d0d6..9d87ef9 100644 --- a/Scene.cpp +++ b/Scene.cpp @@ -51,7 +51,6 @@ void Node::update( float dt ) } - void Node::accept(Visitor& v) { v.visit(*this); @@ -62,12 +61,6 @@ void Node::accept(Visitor& v) Primitive::~Primitive() { deleteGLBuffers_(); - -// points_.clear(); -// colors_.clear(); -// texCoords_.clear(); -// indices_.clear(); - } void Primitive::init() @@ -119,8 +112,12 @@ void Primitive::init() drawCount_ = indices_.size(); // delete temporary buffers - if ( arrayBuffer_ ) glDeleteBuffers ( 1, &arrayBuffer_); - if ( elementBuffer_ ) glDeleteBuffers ( 1, &elementBuffer_); + if ( arrayBuffer_ ) + glDeleteBuffers ( 1, &arrayBuffer_); + if ( elementBuffer_ ) + glDeleteBuffers ( 1, &elementBuffer_); + + // arrays of vertices are not needed anymore (STATIC DRAW of vertex object) points_.clear(); colors_.clear(); texCoords_.clear(); @@ -146,7 +143,7 @@ void Primitive::draw(glm::mat4 modelview, glm::mat4 projection) // // draw vertex array object // - if (drawMode_) { + if (vao_) { glBindVertexArray( vao_ ); glDrawElements( drawMode_, drawCount_, GL_UNSIGNED_INT, 0 ); glBindVertexArray(0); @@ -174,16 +171,6 @@ Group::~Group() children_.clear(); } -void Group::init() -{ - for (NodeSet::iterator node = children_.begin(); - node != children_.end(); node++) { - (*node)->init(); - } - - Node::init(); -} - void Group::update( float dt ) { Node::update(dt); diff --git a/Scene.h b/Scene.h index fe628c3..78de93d 100644 --- a/Scene.h +++ b/Scene.h @@ -104,7 +104,6 @@ public: Group() : Node() {} virtual ~Group(); - virtual void init () override; virtual void update (float dt) override; virtual void accept (Visitor& v) override; virtual void draw (glm::mat4 modelview, glm::mat4 projection) override; diff --git a/main.cpp b/main.cpp index 546bc65..1bfc384 100644 --- a/main.cpp +++ b/main.cpp @@ -263,38 +263,40 @@ int main(int, char**) glm::vec4 color( 0.8f, 0.8f, 0.f, 1.f); LineSquare border(color, 5); -// ObjModel shadow("models/square_border.obj"); Mesh shadow("mesh/shadow.ply", "mesh/shadow.png"); Group g1; g1.translation_ = glm::vec3(1.f, 1.f, 0.2f); - g1.scale_ = glm::vec3(1.2f, 1.2f, 1.f); + g1.scale_ = glm::vec3(0.8f, 0.8f, 1.f); Group g2; g2.translation_ = glm::vec3(-1.f, -1.f, 0.4f); Animation A; - A.speed_ = 0.08f; + A.speed_ = 0.05f; A.axis_ = glm::vec3(1.f, 1.f, 1.f); // std::vector pts = std::vector { glm::vec3( 0.f, 0.f, 0.f ) }; // Points P(pts, pink); // P.setPointSize(60); - Mesh P("mesh/point.ply"); - P.scale_ = glm::vec3(0.05f); + Mesh P("mesh/target.ply"); + P.scale_ = glm::vec3(0.5f); + + Mesh meshicon("mesh/icon_video.ply"); // build tree scene.root_.addChild(&disk); scene.root_.addChild(&circle); - g1.addChild(&shadow); g1.addChild(&testnode3); g1.addChild(&border); + g1.addChild(&shadow); scene.root_.addChild(&g1); - g2.addChild(&shadow); g2.addChild(&testnode1); g2.addChild(&border); + g2.addChild(&shadow); + g2.addChild(&meshicon); scene.root_.addChild(&g2); A.addChild(&P);