Showing the changes to interface/src/renderer/TextureCache.cpp (1 of 8 files). Show all
Unified · Split
| @@ -9,6 +9,7 @@ · 8 unmodified lines | ||
| 9 | 9 | #include "InterfaceConfig.h" |
| 10 | 10 | |
| 11 | 11 | #include <QGLWidget> |
| 12 | +#include <QNetworkReply> | |
| 12 | 13 | #include <QOpenGLFramebufferObject> |
| 13 | 14 | |
| 14 | 15 | #include <glm/gtc/random.hpp> |
| @@ -83,6 +84,15 @@ GLuint TextureCache::getFileTextureID(const QString& filename) { · 68 unmodified lines | ||
| 83 | 84 | return id; |
| 84 | 85 | } |
| 85 | 86 | |
| 87 | +QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url) { | |
| 88 | + QSharedPointer<NetworkTexture> texture = _networkTextures.value(url); | |
| 89 | + if (texture.isNull()) { | |
| 90 | + texture = QSharedPointer<NetworkTexture>(new NetworkTexture(url)); | |
| 91 | + _networkTextures.insert(url, texture); | |
| 92 | + } | |
| 93 | + return texture; | |
| 94 | +} | |
| 95 | + | |
| 86 | 96 | QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { |
| 87 | 97 | if (_primaryFramebufferObject == NULL) { |
| 88 | 98 | _primaryFramebufferObject = createFramebufferObject(); |
| @@ -163,6 +173,41 @@ Texture::~Texture() { · 74 unmodified lines | ||
| 163 | 173 | glDeleteTextures(1, &_id); |
| 164 | 174 | } |
| 165 | 175 | |
| 176 | +NetworkTexture::NetworkTexture(const QUrl& url) { | |
| 177 | + QNetworkRequest request(url); | |
| 178 | + request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); | |
| 179 | + _reply = Application::getInstance()->getNetworkAccessManager()->get(request); | |
| 180 | + | |
| 181 | + connect(_reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleDownloadProgress(qint64,qint64))); | |
| 182 | + connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(handleReplyError())); | |
| 183 | +} | |
| 184 | + | |
| 185 | +void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { | |
| 186 | + if (bytesReceived < bytesTotal && !_reply->isFinished()) { | |
| 187 | + return; | |
| 188 | + } | |
| 189 | + | |
| 190 | + QByteArray entirety = _reply->readAll(); | |
| 191 | + _reply->disconnect(this); | |
| 192 | + _reply->deleteLater(); | |
| 193 | + _reply = NULL; | |
| 194 | + | |
| 195 | + QImage image = QImage::fromData(entirety).convertToFormat(QImage::Format_ARGB32); | |
| 196 | + glBindTexture(GL_TEXTURE_2D, getID()); | |
| 197 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1, | |
| 198 | + GL_BGRA, GL_UNSIGNED_BYTE, image.constBits()); | |
| 199 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 200 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 201 | +} | |
| 202 | + | |
| 203 | +void NetworkTexture::handleReplyError() { | |
| 204 | + qDebug() << _reply->errorString() << "\n"; | |
| 205 | + | |
| 206 | + _reply->disconnect(this); | |
| 207 | + _reply->deleteLater(); | |
| 208 | + _reply = NULL; | |
| 209 | +} | |
| 210 | + | |
| 166 | 211 | DilatedTextureCache::DilatedTextureCache(const QString& filename, int innerRadius, int outerRadius) : |
| 167 | 212 | _innerRadius(innerRadius), |
| 168 | 213 | _outerRadius(outerRadius) |