Showing the changes to interface/src/renderer/TextureCache.cpp (1 of 9 files). Show all
Unified · Split
| @@ -84,10 +84,10 @@ GLuint TextureCache::getFileTextureID(const QString& filename) { · 83 unmodified lines | |||
| 84 | return id; | 84 | return id; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url) { | 87 | QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool dilatable) { |
| 88 | QSharedPointer<NetworkTexture> texture = _networkTextures.value(url); | 88 | QSharedPointer<NetworkTexture> texture = _networkTextures.value(url); |
| 89 | if (texture.isNull()) { | 89 | if (texture.isNull()) { |
| 90 | texture = QSharedPointer<NetworkTexture>(new NetworkTexture(url)); | 90 | texture = QSharedPointer<NetworkTexture>(dilatable ? new DilatableNetworkTexture(url) : new NetworkTexture(url)); |
| 91 | _networkTextures.insert(url, texture); | 91 | _networkTextures.insert(url, texture); |
| 92 | } | 92 | } |
| 93 | return texture; | 93 | return texture; |
| @@ -191,6 +191,10 @@ NetworkTexture::~NetworkTexture() { · 97 unmodified lines | |||
| 191 | } | 191 | } |
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | void NetworkTexture::imageLoaded(const QImage& image) { | ||
| 195 | // nothing by default | ||
| 196 | } | ||
| 197 | |||
| 194 | void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { | 198 | void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { |
| 195 | if (bytesReceived < bytesTotal && !_reply->isFinished()) { | 199 | if (bytesReceived < bytesTotal && !_reply->isFinished()) { |
| 196 | return; | 200 | return; |
| @@ -202,6 +206,7 @@ void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTo · 5 unmodified lines | |||
| 202 | _reply = NULL; | 206 | _reply = NULL; |
| 203 | 207 | ||
| 204 | QImage image = QImage::fromData(entirety).convertToFormat(QImage::Format_ARGB32); | 208 | QImage image = QImage::fromData(entirety).convertToFormat(QImage::Format_ARGB32); |
| 209 | imageLoaded(image); | ||
| 205 | glBindTexture(GL_TEXTURE_2D, getID()); | 210 | glBindTexture(GL_TEXTURE_2D, getID()); |
| 206 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1, | 211 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1, |
| 207 | GL_BGRA, GL_UNSIGNED_BYTE, image.constBits()); | 212 | GL_BGRA, GL_UNSIGNED_BYTE, image.constBits()); |
| @@ -217,35 +222,57 @@ void NetworkTexture::handleReplyError() { · 9 unmodified lines | |||
| 217 | _reply = NULL; | 222 | _reply = NULL; |
| 218 | } | 223 | } |
| 219 | 224 | ||
| 220 | DilatedTextureCache::DilatedTextureCache(const QString& filename, int innerRadius, int outerRadius) : | 225 | DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url) : |
| 221 | _innerRadius(innerRadius), | 226 | NetworkTexture(url), |
| 222 | _outerRadius(outerRadius) | 227 | _innerRadius(0), |
| 228 | _outerRadius(0) | ||
| 223 | { | 229 | { |
| 224 | switchToResourcesParentIfRequired(); | ||
| 225 | _image = QImage(filename).convertToFormat(QImage::Format_ARGB32); | ||
| 226 | } | 230 | } |
| 227 | 231 | ||
| 228 | QSharedPointer<Texture> DilatedTextureCache::getTexture(float dilation) { | 232 | void DilatableNetworkTexture::imageLoaded(const QImage& image) { |
| 229 | QSharedPointer<Texture> texture = _textures.value(dilation); | 233 | _image = image; |
| 234 | |||
| 235 | // scan out from the center to find inner and outer radii | ||
| 236 | int halfWidth = image.width() / 2; | ||
| 237 | int halfHeight = image.height() / 2; | ||
| 238 | const int BLACK_THRESHOLD = 32; | ||
| 239 | while (_innerRadius < halfWidth && qGray(image.pixel(halfWidth + _innerRadius, halfHeight)) < BLACK_THRESHOLD) { | ||
| 240 | _innerRadius++; | ||
| 241 | } | ||
| 242 | _outerRadius = _innerRadius; | ||
| 243 | const int TRANSPARENT_THRESHOLD = 32; | ||
| 244 | while (_outerRadius < halfWidth && qAlpha(image.pixel(halfWidth + _outerRadius, halfHeight)) > TRANSPARENT_THRESHOLD) { | ||
| 245 | _outerRadius++; | ||
| 246 | } | ||
| 247 | |||
| 248 | // clear out any textures we generated before loading | ||
| 249 | _dilatedTextures.clear(); | ||
| 250 | } | ||
| 251 | |||
| 252 | QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilation) { | ||
| 253 | QSharedPointer<Texture> texture = _dilatedTextures.value(dilation); | ||
| 230 | if (texture.isNull()) { | 254 | if (texture.isNull()) { |
| 231 | texture = QSharedPointer<Texture>(new Texture()); | 255 | texture = QSharedPointer<Texture>(new Texture()); |
| 232 | 256 | ||
| 233 | QImage dilatedImage = _image; | 257 | if (!_image.isNull()) { |
| 234 | QPainter painter; | 258 | QImage dilatedImage = _image; |
| 235 | painter.begin(&dilatedImage); | 259 | QPainter painter; |
| 236 | QPainterPath path; | 260 | painter.begin(&dilatedImage); |
| 237 | qreal radius = glm::mix(_innerRadius, _outerRadius, dilation); | 261 | QPainterPath path; |
| 238 | path.addEllipse(QPointF(_image.width() / 2.0, _image.height() / 2.0), radius, radius); | 262 | qreal radius = glm::mix(_innerRadius, _outerRadius, dilation); |
| 239 | painter.fillPath(path, Qt::black); | 263 | path.addEllipse(QPointF(_image.width() / 2.0, _image.height() / 2.0), radius, radius); |
| 240 | painter.end(); | 264 | painter.fillPath(path, Qt::black); |
| 241 | 265 | painter.end(); | |
| 242 | glBindTexture(GL_TEXTURE_2D, texture->getID()); | 266 | |
| 243 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 1, | 267 | glBindTexture(GL_TEXTURE_2D, texture->getID()); |
| 244 | GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits()); | 268 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 1, |
| 245 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 269 | GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits()); |
| 246 | glBindTexture(GL_TEXTURE_2D, 0); | 270 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 271 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 272 | } | ||
| 247 | 273 | ||
| 248 | _textures.insert(dilation, texture); | 274 | _dilatedTextures.insert(dilation, texture); |
| 249 | } | 275 | } |
| 250 | return texture; | 276 | return texture; |
| 251 | } | 277 | } |
| 278 | |||