History

Dilate our network-downloaded textures.

Andrzej Kapolka committed 13 years ago

Commit c98dec81 parent d87fe209 files · +93 −46Browse files

Showing the changes to interface/src/renderer/TextureCache.cpp (1 of 9 files). Show all

Unified · Split

interface/src/renderer/TextureCache.cpp+51−24
@@ -84,10 +84,10 @@ GLuint TextureCache::getFileTextureID(const QString& filename) { · 83 unmodified lines
8484 return id;
8585 }
8686
87−QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url) {
87+QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool dilatable) {
8888 QSharedPointer<NetworkTexture> texture = _networkTextures.value(url);
8989 if (texture.isNull()) {
90− texture = QSharedPointer<NetworkTexture>(new NetworkTexture(url));
90+ texture = QSharedPointer<NetworkTexture>(dilatable ? new DilatableNetworkTexture(url) : new NetworkTexture(url));
9191 _networkTextures.insert(url, texture);
9292 }
9393 return texture;
@@ -191,6 +191,10 @@ NetworkTexture::~NetworkTexture() { · 97 unmodified lines
191191 }
192192 }
193193
194+void NetworkTexture::imageLoaded(const QImage& image) {
195+ // nothing by default
196+}
197+
194198 void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
195199 if (bytesReceived < bytesTotal && !_reply->isFinished()) {
196200 return;
@@ -202,6 +206,7 @@ void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTo · 5 unmodified lines
202206 _reply = NULL;
203207
204208 QImage image = QImage::fromData(entirety).convertToFormat(QImage::Format_ARGB32);
209+ imageLoaded(image);
205210 glBindTexture(GL_TEXTURE_2D, getID());
206211 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1,
207212 GL_BGRA, GL_UNSIGNED_BYTE, image.constBits());
@@ -217,35 +222,57 @@ void NetworkTexture::handleReplyError() { · 9 unmodified lines
217222 _reply = NULL;
218223 }
219224
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)
223229 {
224− switchToResourcesParentIfRequired();
225− _image = QImage(filename).convertToFormat(QImage::Format_ARGB32);
226230 }
227231
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);
230254 if (texture.isNull()) {
231255 texture = QSharedPointer<Texture>(new Texture());
232256
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+ }
247273
248− _textures.insert(dilation, texture);
274+ _dilatedTextures.insert(dilation, texture);
249275 }
250276 return texture;
251277 }
278+

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t