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 | 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 | 88 | QSharedPointer<NetworkTexture> texture = _networkTextures.value(url); |
| 89 | 89 | if (texture.isNull()) { |
| 90 | − texture = QSharedPointer<NetworkTexture>(new NetworkTexture(url)); | |
| 90 | + texture = QSharedPointer<NetworkTexture>(dilatable ? new DilatableNetworkTexture(url) : new NetworkTexture(url)); | |
| 91 | 91 | _networkTextures.insert(url, texture); |
| 92 | 92 | } |
| 93 | 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 | 198 | void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { |
| 195 | 199 | if (bytesReceived < bytesTotal && !_reply->isFinished()) { |
| 196 | 200 | return; |
| @@ -202,6 +206,7 @@ void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTo · 5 unmodified lines | ||
| 202 | 206 | _reply = NULL; |
| 203 | 207 | |
| 204 | 208 | QImage image = QImage::fromData(entirety).convertToFormat(QImage::Format_ARGB32); |
| 209 | + imageLoaded(image); | |
| 205 | 210 | glBindTexture(GL_TEXTURE_2D, getID()); |
| 206 | 211 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1, |
| 207 | 212 | GL_BGRA, GL_UNSIGNED_BYTE, image.constBits()); |
| @@ -217,35 +222,57 @@ void NetworkTexture::handleReplyError() { · 9 unmodified lines | ||
| 217 | 222 | _reply = NULL; |
| 218 | 223 | } |
| 219 | 224 | |
| 220 | −DilatedTextureCache::DilatedTextureCache(const QString& filename, int innerRadius, int outerRadius) : | |
| 221 | − _innerRadius(innerRadius), | |
| 222 | − _outerRadius(outerRadius) | |
| 225 | +DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url) : | |
| 226 | + NetworkTexture(url), | |
| 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) { | |
| 229 | − QSharedPointer<Texture> texture = _textures.value(dilation); | |
| 232 | +void DilatableNetworkTexture::imageLoaded(const QImage& image) { | |
| 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 | 254 | if (texture.isNull()) { |
| 231 | 255 | texture = QSharedPointer<Texture>(new Texture()); |
| 232 | 256 | |
| 233 | − QImage dilatedImage = _image; | |
| 234 | − QPainter painter; | |
| 235 | − painter.begin(&dilatedImage); | |
| 236 | − QPainterPath path; | |
| 237 | − qreal radius = glm::mix(_innerRadius, _outerRadius, dilation); | |
| 238 | − path.addEllipse(QPointF(_image.width() / 2.0, _image.height() / 2.0), radius, radius); | |
| 239 | − painter.fillPath(path, Qt::black); | |
| 240 | − painter.end(); | |
| 241 | − | |
| 242 | − glBindTexture(GL_TEXTURE_2D, texture->getID()); | |
| 243 | − glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 1, | |
| 244 | − GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits()); | |
| 245 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 246 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 257 | + if (!_image.isNull()) { | |
| 258 | + QImage dilatedImage = _image; | |
| 259 | + QPainter painter; | |
| 260 | + painter.begin(&dilatedImage); | |
| 261 | + QPainterPath path; | |
| 262 | + qreal radius = glm::mix(_innerRadius, _outerRadius, dilation); | |
| 263 | + path.addEllipse(QPointF(_image.width() / 2.0, _image.height() / 2.0), radius, radius); | |
| 264 | + painter.fillPath(path, Qt::black); | |
| 265 | + painter.end(); | |
| 266 | + | |
| 267 | + glBindTexture(GL_TEXTURE_2D, texture->getID()); | |
| 268 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 1, | |
| 269 | + GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits()); | |
| 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 | 276 | return texture; |
| 251 | 277 | } |
| 278 | + | |