From 345643bad10d9f539eebe0139fa69f11e3f14f09 Mon Sep 17 00:00:00 2001 From: Tats Date: Thu, 17 Mar 2016 16:05:03 -0400 Subject: [PATCH] Fixed: Flickering video images when loading from the commandline (closes #218). --- Paint.cpp | 9 +++++++++ Paint.h | 5 ++++- ShapeGraphicsItem.cpp | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Paint.cpp b/Paint.cpp index b4e6f52..580c12c 100644 --- a/Paint.cpp +++ b/Paint.cpp @@ -26,6 +26,14 @@ MM_BEGIN_NAMESPACE UidAllocator Paint::allocator; +void Texture::update() +{ + if (textureId == 0) + { + glGenTextures(1, &textureId); + } +} + void Texture::read(const QDomElement& obj) { Paint::read(obj); @@ -111,6 +119,7 @@ int Video::getHeight() const void Video::update() { _impl->update(); + Texture::update(); } void Video::play() diff --git a/Paint.h b/Paint.h index 9dfaabc..6203966 100644 --- a/Paint.h +++ b/Paint.h @@ -135,16 +135,19 @@ protected: x(0), y(0) { - glGenTextures(1, &textureId); } public: virtual ~Texture() { + // TODO: this needs to be fixed: it will not work unless it is executed from within a GL context + // see issue #229 if (textureId != 0) glDeleteTextures(1, &textureId); } public: + virtual void update(); + GLuint getTextureId() const { return textureId; } virtual int getWidth() const = 0; virtual int getHeight() const = 0; diff --git a/ShapeGraphicsItem.cpp b/ShapeGraphicsItem.cpp index 6736f2a..c91745f 100644 --- a/ShapeGraphicsItem.cpp +++ b/ShapeGraphicsItem.cpp @@ -229,17 +229,16 @@ void TextureGraphicsItem::_prePaint(QPainter* painter, const QStyleOptionGraphicsItem *option) { Q_UNUSED(option); + QSharedPointer texture = _texture.toStrongRef(); painter->beginNativePainting(); - QSharedPointer texture = _texture.toStrongRef(); + // Project source texture and sent it to destination. + texture->update(); // Only works for similar shapes. // TODO:remettre //Q_ASSERT( _inputShape->nVertices() == outputShape->nVertices()); - // Project source texture and sent it to destination. - texture->update(); - // Allow alpha blending. glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -253,8 +252,14 @@ void TextureGraphicsItem::_prePaint(QPainter* painter, if (texture->bitsHaveChanged()) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - texture->getWidth(), texture->getHeight(), 0, GL_RGBA, - GL_UNSIGNED_BYTE, texture->getBits()); + texture->getWidth(), texture->getHeight(), 0, GL_RGBA, + GL_UNSIGNED_BYTE, texture->getBits()); + // NOTE: We would gain in efficiency if we were able to just update the texture using glTexSubImage2D + // See: http://stackoverflow.com/questions/11217121/how-to-manage-memory-with-texture-in-opengl +// glTexSubImage2D(GL_TEXTURE_2D, +// 0, 0, +// texture->getWidth(), texture->getHeight(), 0, +// GL_RGBA, GL_UNSIGNED_BYTE, texture->getBits()); } texture->unlockMutex(); @@ -266,6 +271,7 @@ void TextureGraphicsItem::_prePaint(QPainter* painter, // Set texture color (apply opacity). glColor4f(1.0f, 1.0f, 1.0f, isOutput() ? getMapping()->getComputedOpacity() : getMapping()->getPaint()->getOpacity()); + } void TextureGraphicsItem::_postPaint(QPainter* painter,