From ba2f7d88ce3cdd7c8ad372457fc5cf9c7c709345 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 21 Jan 2015 15:09:09 -0800 Subject: [PATCH] Update ImageOverlay to use TextureCache --- interface/src/ui/overlays/ImageOverlay.cpp | 51 ++++++++-------------- interface/src/ui/overlays/ImageOverlay.h | 7 +-- 2 files changed, 19 insertions(+), 39 deletions(-) diff --git a/interface/src/ui/overlays/ImageOverlay.cpp b/interface/src/ui/overlays/ImageOverlay.cpp index 1ecfc74e44..164b3916db 100644 --- a/interface/src/ui/overlays/ImageOverlay.cpp +++ b/interface/src/ui/overlays/ImageOverlay.cpp @@ -22,9 +22,7 @@ #include "ImageOverlay.h" ImageOverlay::ImageOverlay() : - _textureID(0), _renderImage(false), - _textureBound(false), _wantClipFromImage(false) { _isLoaded = false; @@ -32,63 +30,48 @@ ImageOverlay::ImageOverlay() : ImageOverlay::ImageOverlay(const ImageOverlay* imageOverlay) : Overlay2D(imageOverlay), + _texture(imageOverlay->_texture), _imageURL(imageOverlay->_imageURL), _textureImage(imageOverlay->_textureImage), - _textureID(0), - _fromImage(), + _fromImage(imageOverlay->_fromImage), _renderImage(imageOverlay->_renderImage), - _textureBound(false), - _wantClipFromImage(false) + _wantClipFromImage(imageOverlay->_wantClipFromImage) { } ImageOverlay::~ImageOverlay() { - if (_parent && _textureID) { - // do we need to call this? - //_parent->deleteTexture(_textureID); - } } // TODO: handle setting image multiple times, how do we manage releasing the bound texture? void ImageOverlay::setImageURL(const QUrl& url) { _imageURL = url; _isLoaded = false; - QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); - QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); - connect(reply, &QNetworkReply::finished, this, &ImageOverlay::replyFinished); -} - -void ImageOverlay::replyFinished() { - QNetworkReply* reply = static_cast(sender()); - - // replace our byte array with the downloaded data - QByteArray rawData = reply->readAll(); - _textureImage.loadFromData(rawData); - _renderImage = true; - _isLoaded = true; - reply->deleteLater(); } void ImageOverlay::render(RenderArgs* args) { - if (!_visible || !_isLoaded) { - return; // do nothing if we're not visible + if (!_isLoaded && !_imageURL.isEmpty()) { + _isLoaded = true; + _renderImage = true; + qDebug() << "Now loding texture for ImageOverlay"; + _texture = DependencyManager::get()->getTexture(_imageURL); } - if (_renderImage && !_textureBound) { - _textureID = _parent->bindTexture(_textureImage); - _textureBound = true; + + if (!_visible || !_isLoaded || !_texture || !_texture->isLoaded()) { + return; } if (_renderImage) { + qDebug() << "Rendering: " << _imageURL << ", " << _texture->getWidth() << ", " << _texture->getHeight(); glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, _textureID); + glBindTexture(GL_TEXTURE_2D, _texture->getID()); } const float MAX_COLOR = 255.0f; xColor color = getColor(); float alpha = getAlpha(); glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); - float imageWidth = _textureImage.width(); - float imageHeight = _textureImage.height(); + float imageWidth = _texture->getWidth(); + float imageHeight = _texture->getHeight(); QRect fromImage; if (_wantClipFromImage) { @@ -111,8 +94,8 @@ void ImageOverlay::render(RenderArgs* args) { glm::vec2 topLeft(left, top); glm::vec2 bottomRight(right, bottom); - glm::vec2 texCoordTopLeft(x, 1.0f - y); - glm::vec2 texCoordBottomRight(x + w, 1.0f - (y + h)); + glm::vec2 texCoordTopLeft(x, y); + glm::vec2 texCoordBottomRight(x + w, y + h); if (_renderImage) { DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); diff --git a/interface/src/ui/overlays/ImageOverlay.h b/interface/src/ui/overlays/ImageOverlay.h index cff557654f..e167c4e755 100644 --- a/interface/src/ui/overlays/ImageOverlay.h +++ b/interface/src/ui/overlays/ImageOverlay.h @@ -24,6 +24,7 @@ #include #include +#include #include "Overlay.h" #include "Overlay2D.h" @@ -49,18 +50,14 @@ public: virtual ImageOverlay* createClone() const; -private slots: - void replyFinished(); // we actually want to hide this... - private: QUrl _imageURL; QImage _textureImage; - GLuint _textureID; + NetworkTexturePointer _texture; QRect _fromImage; // where from in the image to sample bool _renderImage; // is there an image associated with this overlay, or is it just a colored rectangle - bool _textureBound; // has the texture been bound bool _wantClipFromImage; };