Showing the changes to libraries/render-utils/src/TextureCache.cpp (1 of 10 files). Show all
Unified · Split
| @@ -0,0 +1,622 @@ | ||
| 1 | +// | |
| 2 | +// TextureCache.cpp | |
| 3 | +// interface/src/renderer | |
| 4 | +// | |
| 5 | +// Created by Andrzej Kapolka on 8/6/13. | |
| 6 | +// Copyright 2013 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +// include this before QGLWidget, which includes an earlier version of OpenGL | |
| 13 | + | |
| 14 | +#include <QEvent> | |
| 15 | +#include <QGLWidget> | |
| 16 | +#include <QNetworkReply> | |
| 17 | +#include <QOpenGLFramebufferObject> | |
| 18 | +#include <QResizeEvent> | |
| 19 | +#include <QRunnable> | |
| 20 | +#include <QThreadPool> | |
| 21 | + | |
| 22 | +#include <glm/glm.hpp> | |
| 23 | +#include <glm/gtc/random.hpp> | |
| 24 | + | |
| 25 | +#include "TextureCache.h" | |
| 26 | + | |
| 27 | +TextureCache* TextureCache::getInstance() { | |
| 28 | + static TextureCache instance; | |
| 29 | + return &instance; | |
| 30 | +} | |
| 31 | + | |
| 32 | + | |
| 33 | +TextureCache::TextureCache() : | |
| 34 | + _permutationNormalTextureID(0), | |
| 35 | + _whiteTextureID(0), | |
| 36 | + _blueTextureID(0), | |
| 37 | + _primaryDepthTextureID(0), | |
| 38 | + _primaryNormalTextureID(0), | |
| 39 | + _primarySpecularTextureID(0), | |
| 40 | + _primaryFramebufferObject(NULL), | |
| 41 | + _secondaryFramebufferObject(NULL), | |
| 42 | + _tertiaryFramebufferObject(NULL), | |
| 43 | + _shadowFramebufferObject(NULL), | |
| 44 | + _frameBufferSize(100, 100), | |
| 45 | + _associatedWidget(NULL) | |
| 46 | +{ | |
| 47 | +} | |
| 48 | + | |
| 49 | +TextureCache::~TextureCache() { | |
| 50 | + if (_permutationNormalTextureID != 0) { | |
| 51 | + glDeleteTextures(1, &_permutationNormalTextureID); | |
| 52 | + } | |
| 53 | + if (_whiteTextureID != 0) { | |
| 54 | + glDeleteTextures(1, &_whiteTextureID); | |
| 55 | + } | |
| 56 | + if (_primaryFramebufferObject) { | |
| 57 | + glDeleteTextures(1, &_primaryDepthTextureID); | |
| 58 | + glDeleteTextures(1, &_primaryNormalTextureID); | |
| 59 | + glDeleteTextures(1, &_primarySpecularTextureID); | |
| 60 | + } | |
| 61 | + | |
| 62 | + if (_primaryFramebufferObject) { | |
| 63 | + delete _primaryFramebufferObject; | |
| 64 | + } | |
| 65 | + | |
| 66 | + if (_secondaryFramebufferObject) { | |
| 67 | + delete _secondaryFramebufferObject; | |
| 68 | + } | |
| 69 | + | |
| 70 | + if (_tertiaryFramebufferObject) { | |
| 71 | + delete _tertiaryFramebufferObject; | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | +void TextureCache::setFrameBufferSize(QSize frameBufferSize) { | |
| 76 | + //If the size changed, we need to delete our FBOs | |
| 77 | + if (_frameBufferSize != frameBufferSize) { | |
| 78 | + _frameBufferSize = frameBufferSize; | |
| 79 | + | |
| 80 | + if (_primaryFramebufferObject) { | |
| 81 | + delete _primaryFramebufferObject; | |
| 82 | + _primaryFramebufferObject = NULL; | |
| 83 | + glDeleteTextures(1, &_primaryDepthTextureID); | |
| 84 | + _primaryDepthTextureID = 0; | |
| 85 | + glDeleteTextures(1, &_primaryNormalTextureID); | |
| 86 | + _primaryNormalTextureID = 0; | |
| 87 | + glDeleteTextures(1, &_primarySpecularTextureID); | |
| 88 | + _primarySpecularTextureID = 0; | |
| 89 | + } | |
| 90 | + | |
| 91 | + if (_secondaryFramebufferObject) { | |
| 92 | + delete _secondaryFramebufferObject; | |
| 93 | + _secondaryFramebufferObject = NULL; | |
| 94 | + } | |
| 95 | + | |
| 96 | + if (_tertiaryFramebufferObject) { | |
| 97 | + delete _tertiaryFramebufferObject; | |
| 98 | + _tertiaryFramebufferObject = NULL; | |
| 99 | + } | |
| 100 | + } | |
| 101 | +} | |
| 102 | + | |
| 103 | +// use fixed table of permutations. Could also make ordered list programmatically | |
| 104 | +// and then shuffle algorithm. For testing, this ensures consistent behavior in each run. | |
| 105 | +// this list taken from Ken Perlin's Improved Noise reference implementation (orig. in Java) at | |
| 106 | +// http://mrl.nyu.edu/~perlin/noise/ | |
| 107 | + | |
| 108 | +const int permutation[256] = | |
| 109 | +{ | |
| 110 | + 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, | |
| 111 | + 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, | |
| 112 | + 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, | |
| 113 | + 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, | |
| 114 | + 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, | |
| 115 | + 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, | |
| 116 | + 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, | |
| 117 | + 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, | |
| 118 | + 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, | |
| 119 | + 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, | |
| 120 | + 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, | |
| 121 | + 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, | |
| 122 | + 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, | |
| 123 | + 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, | |
| 124 | + 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, | |
| 125 | + 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180 | |
| 126 | +}; | |
| 127 | + | |
| 128 | +#define USE_CHRIS_NOISE 1 | |
| 129 | + | |
| 130 | +GLuint TextureCache::getPermutationNormalTextureID() { | |
| 131 | + if (_permutationNormalTextureID == 0) { | |
| 132 | + glGenTextures(1, &_permutationNormalTextureID); | |
| 133 | + glBindTexture(GL_TEXTURE_2D, _permutationNormalTextureID); | |
| 134 | + | |
| 135 | + // the first line consists of random permutation offsets | |
| 136 | + unsigned char data[256 * 2 * 3]; | |
| 137 | +#if (USE_CHRIS_NOISE==1) | |
| 138 | + for (int i = 0; i < 256; i++) { | |
| 139 | + data[3*i+0] = permutation[i]; | |
| 140 | + data[3*i+1] = permutation[i]; | |
| 141 | + data[3*i+2] = permutation[i]; | |
| 142 | +#else | |
| 143 | + for (int i = 0; i < 256 * 3; i++) { | |
| 144 | + data[i] = rand() % 256; | |
| 145 | +#endif | |
| 146 | + } | |
| 147 | + | |
| 148 | + for (int i = 256 * 3; i < 256 * 3 * 2; i += 3) { | |
| 149 | + glm::vec3 randvec = glm::sphericalRand(1.0f); | |
| 150 | + data[i] = ((randvec.x + 1.0f) / 2.0f) * 255.0f; | |
| 151 | + data[i + 1] = ((randvec.y + 1.0f) / 2.0f) * 255.0f; | |
| 152 | + data[i + 2] = ((randvec.z + 1.0f) / 2.0f) * 255.0f; | |
| 153 | + } | |
| 154 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, data); | |
| 155 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 156 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 157 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 158 | + } | |
| 159 | + return _permutationNormalTextureID; | |
| 160 | +} | |
| 161 | + | |
| 162 | +const unsigned char OPAQUE_WHITE[] = { 0xFF, 0xFF, 0xFF, 0xFF }; | |
| 163 | +const unsigned char TRANSPARENT_WHITE[] = { 0xFF, 0xFF, 0xFF, 0x0 }; | |
| 164 | +const unsigned char OPAQUE_BLACK[] = { 0x0, 0x0, 0x0, 0xFF }; | |
| 165 | +const unsigned char OPAQUE_BLUE[] = { 0x80, 0x80, 0xFF, 0xFF }; | |
| 166 | + | |
| 167 | +static void loadSingleColorTexture(const unsigned char* color) { | |
| 168 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, color); | |
| 169 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 170 | +} | |
| 171 | + | |
| 172 | +GLuint TextureCache::getWhiteTextureID() { | |
| 173 | + if (_whiteTextureID == 0) { | |
| 174 | + glGenTextures(1, &_whiteTextureID); | |
| 175 | + glBindTexture(GL_TEXTURE_2D, _whiteTextureID); | |
| 176 | + loadSingleColorTexture(OPAQUE_WHITE); | |
| 177 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 178 | + } | |
| 179 | + return _whiteTextureID; | |
| 180 | +} | |
| 181 | + | |
| 182 | +GLuint TextureCache::getBlueTextureID() { | |
| 183 | + if (_blueTextureID == 0) { | |
| 184 | + glGenTextures(1, &_blueTextureID); | |
| 185 | + glBindTexture(GL_TEXTURE_2D, _blueTextureID); | |
| 186 | + loadSingleColorTexture(OPAQUE_BLUE); | |
| 187 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 188 | + } | |
| 189 | + return _blueTextureID; | |
| 190 | +} | |
| 191 | + | |
| 192 | +/// Extra data for creating textures. | |
| 193 | +class TextureExtra { | |
| 194 | +public: | |
| 195 | + TextureType type; | |
| 196 | + const QByteArray& content; | |
| 197 | +}; | |
| 198 | + | |
| 199 | +NetworkTexturePointer TextureCache::getTexture(const QUrl& url, TextureType type, bool dilatable, const QByteArray& content) { | |
| 200 | + if (!dilatable) { | |
| 201 | + TextureExtra extra = { type, content }; | |
| 202 | + return ResourceCache::getResource(url, QUrl(), false, &extra).staticCast<NetworkTexture>(); | |
| 203 | + } | |
| 204 | + NetworkTexturePointer texture = _dilatableNetworkTextures.value(url); | |
| 205 | + if (texture.isNull()) { | |
| 206 | + texture = NetworkTexturePointer(new DilatableNetworkTexture(url, content), &Resource::allReferencesCleared); | |
| 207 | + texture->setSelf(texture); | |
| 208 | + texture->setCache(this); | |
| 209 | + _dilatableNetworkTextures.insert(url, texture); | |
| 210 | + } else { | |
| 211 | + _unusedResources.remove(texture->getLRUKey()); | |
| 212 | + } | |
| 213 | + return texture; | |
| 214 | +} | |
| 215 | + | |
| 216 | +QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { | |
| 217 | + | |
| 218 | + if (!_primaryFramebufferObject) { | |
| 219 | + _primaryFramebufferObject = createFramebufferObject(); | |
| 220 | + | |
| 221 | + glGenTextures(1, &_primaryDepthTextureID); | |
| 222 | + glBindTexture(GL_TEXTURE_2D, _primaryDepthTextureID); | |
| 223 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _frameBufferSize.width(), _frameBufferSize.height(), | |
| 224 | + 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); | |
| 225 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 226 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 227 | + | |
| 228 | + glGenTextures(1, &_primaryNormalTextureID); | |
| 229 | + glBindTexture(GL_TEXTURE_2D, _primaryNormalTextureID); | |
| 230 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), | |
| 231 | + 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | |
| 232 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 233 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 234 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 235 | + | |
| 236 | + glGenTextures(1, &_primarySpecularTextureID); | |
| 237 | + glBindTexture(GL_TEXTURE_2D, _primarySpecularTextureID); | |
| 238 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), | |
| 239 | + 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | |
| 240 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 241 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 242 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 243 | + | |
| 244 | + _primaryFramebufferObject->bind(); | |
| 245 | + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _primaryDepthTextureID, 0); | |
| 246 | + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _primaryNormalTextureID, 0); | |
| 247 | + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, _primarySpecularTextureID, 0); | |
| 248 | + _primaryFramebufferObject->release(); | |
| 249 | + } | |
| 250 | + return _primaryFramebufferObject; | |
| 251 | +} | |
| 252 | + | |
| 253 | +GLuint TextureCache::getPrimaryDepthTextureID() { | |
| 254 | + // ensure that the primary framebuffer object is initialized before returning the depth texture id | |
| 255 | + getPrimaryFramebufferObject(); | |
| 256 | + return _primaryDepthTextureID; | |
| 257 | +} | |
| 258 | + | |
| 259 | +GLuint TextureCache::getPrimaryNormalTextureID() { | |
| 260 | + // ensure that the primary framebuffer object is initialized before returning the normal texture id | |
| 261 | + getPrimaryFramebufferObject(); | |
| 262 | + return _primaryNormalTextureID; | |
| 263 | +} | |
| 264 | + | |
| 265 | +GLuint TextureCache::getPrimarySpecularTextureID() { | |
| 266 | + getPrimaryFramebufferObject(); | |
| 267 | + return _primarySpecularTextureID; | |
| 268 | +} | |
| 269 | + | |
| 270 | +void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) { | |
| 271 | + GLenum buffers[3]; | |
| 272 | + int bufferCount = 0; | |
| 273 | + if (color) { | |
| 274 | + buffers[bufferCount++] = GL_COLOR_ATTACHMENT0; | |
| 275 | + } | |
| 276 | + if (normal) { | |
| 277 | + buffers[bufferCount++] = GL_COLOR_ATTACHMENT1; | |
| 278 | + } | |
| 279 | + if (specular) { | |
| 280 | + buffers[bufferCount++] = GL_COLOR_ATTACHMENT2; | |
| 281 | + } | |
| 282 | + glDrawBuffers(bufferCount, buffers); | |
| 283 | +} | |
| 284 | + | |
| 285 | +QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { | |
| 286 | + if (!_secondaryFramebufferObject) { | |
| 287 | + _secondaryFramebufferObject = createFramebufferObject(); | |
| 288 | + } | |
| 289 | + return _secondaryFramebufferObject; | |
| 290 | +} | |
| 291 | + | |
| 292 | +QOpenGLFramebufferObject* TextureCache::getTertiaryFramebufferObject() { | |
| 293 | + if (!_tertiaryFramebufferObject) { | |
| 294 | + _tertiaryFramebufferObject = createFramebufferObject(); | |
| 295 | + } | |
| 296 | + return _tertiaryFramebufferObject; | |
| 297 | +} | |
| 298 | + | |
| 299 | +QOpenGLFramebufferObject* TextureCache::getShadowFramebufferObject() { | |
| 300 | + if (!_shadowFramebufferObject) { | |
| 301 | + const int SHADOW_MAP_SIZE = 2048; | |
| 302 | + _shadowFramebufferObject = new QOpenGLFramebufferObject(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, | |
| 303 | + QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGB); | |
| 304 | + | |
| 305 | + glGenTextures(1, &_shadowDepthTextureID); | |
| 306 | + glBindTexture(GL_TEXTURE_2D, _shadowDepthTextureID); | |
| 307 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, | |
| 308 | + 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); | |
| 309 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 310 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 311 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); | |
| 312 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); | |
| 313 | + const float DISTANT_BORDER[] = { 1.0f, 1.0f, 1.0f, 1.0f }; | |
| 314 | + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, DISTANT_BORDER); | |
| 315 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); | |
| 316 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 317 | + | |
| 318 | + _shadowFramebufferObject->bind(); | |
| 319 | + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowDepthTextureID, 0); | |
| 320 | + _shadowFramebufferObject->release(); | |
| 321 | + } | |
| 322 | + return _shadowFramebufferObject; | |
| 323 | +} | |
| 324 | + | |
| 325 | +GLuint TextureCache::getShadowDepthTextureID() { | |
| 326 | + // ensure that the shadow framebuffer object is initialized before returning the depth texture id | |
| 327 | + getShadowFramebufferObject(); | |
| 328 | + return _shadowDepthTextureID; | |
| 329 | +} | |
| 330 | + | |
| 331 | +bool TextureCache::eventFilter(QObject* watched, QEvent* event) { | |
| 332 | + if (event->type() == QEvent::Resize) { | |
| 333 | + QSize size = static_cast<QResizeEvent*>(event)->size(); | |
| 334 | + if (_primaryFramebufferObject && _primaryFramebufferObject->size() != size) { | |
| 335 | + delete _primaryFramebufferObject; | |
| 336 | + _primaryFramebufferObject = NULL; | |
| 337 | + glDeleteTextures(1, &_primaryDepthTextureID); | |
| 338 | + glDeleteTextures(1, &_primaryNormalTextureID); | |
| 339 | + glDeleteTextures(1, &_primarySpecularTextureID); | |
| 340 | + } | |
| 341 | + if (_secondaryFramebufferObject && _secondaryFramebufferObject->size() != size) { | |
| 342 | + delete _secondaryFramebufferObject; | |
| 343 | + _secondaryFramebufferObject = NULL; | |
| 344 | + } | |
| 345 | + if (_tertiaryFramebufferObject && _tertiaryFramebufferObject->size() != size) { | |
| 346 | + delete _tertiaryFramebufferObject; | |
| 347 | + _tertiaryFramebufferObject = NULL; | |
| 348 | + } | |
| 349 | + } | |
| 350 | + return false; | |
| 351 | +} | |
| 352 | + | |
| 353 | +QSharedPointer<Resource> TextureCache::createResource(const QUrl& url, | |
| 354 | + const QSharedPointer<Resource>& fallback, bool delayLoad, const void* extra) { | |
| 355 | + const TextureExtra* textureExtra = static_cast<const TextureExtra*>(extra); | |
| 356 | + return QSharedPointer<Resource>(new NetworkTexture(url, textureExtra->type, textureExtra->content), | |
| 357 | + &Resource::allReferencesCleared); | |
| 358 | +} | |
| 359 | + | |
| 360 | +void TextureCache::associateWithWidget(QGLWidget* widget) { | |
| 361 | + if (_associatedWidget) { | |
| 362 | + _associatedWidget->removeEventFilter(this); | |
| 363 | + } | |
| 364 | + _associatedWidget = widget; | |
| 365 | + _associatedWidget->installEventFilter(this); | |
| 366 | +} | |
| 367 | + | |
| 368 | +QOpenGLFramebufferObject* TextureCache::createFramebufferObject() { | |
| 369 | + QOpenGLFramebufferObject* fbo = new QOpenGLFramebufferObject(_frameBufferSize); | |
| 370 | + | |
| 371 | + glBindTexture(GL_TEXTURE_2D, fbo->texture()); | |
| 372 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 373 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 374 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 375 | + | |
| 376 | + return fbo; | |
| 377 | +} | |
| 378 | + | |
| 379 | +Texture::Texture() { | |
| 380 | + glGenTextures(1, &_id); | |
| 381 | +} | |
| 382 | + | |
| 383 | +Texture::~Texture() { | |
| 384 | + glDeleteTextures(1, &_id); | |
| 385 | +} | |
| 386 | + | |
| 387 | +NetworkTexture::NetworkTexture(const QUrl& url, TextureType type, const QByteArray& content) : | |
| 388 | + Resource(url, !content.isEmpty()), | |
| 389 | + _type(type), | |
| 390 | + _translucent(false) { | |
| 391 | + | |
| 392 | + if (!url.isValid()) { | |
| 393 | + _loaded = true; | |
| 394 | + } | |
| 395 | + | |
| 396 | + // default to white/blue/black | |
| 397 | + glBindTexture(GL_TEXTURE_2D, getID()); | |
| 398 | + switch (type) { | |
| 399 | + case NORMAL_TEXTURE: | |
| 400 | + loadSingleColorTexture(OPAQUE_BLUE); | |
| 401 | + break; | |
| 402 | + | |
| 403 | + case SPECULAR_TEXTURE: | |
| 404 | + loadSingleColorTexture(OPAQUE_BLACK); | |
| 405 | + break; | |
| 406 | + | |
| 407 | + case SPLAT_TEXTURE: | |
| 408 | + loadSingleColorTexture(TRANSPARENT_WHITE); | |
| 409 | + break; | |
| 410 | + | |
| 411 | + default: | |
| 412 | + loadSingleColorTexture(OPAQUE_WHITE); | |
| 413 | + break; | |
| 414 | + } | |
| 415 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 416 | + | |
| 417 | + // if we have content, load it after we have our self pointer | |
| 418 | + if (!content.isEmpty()) { | |
| 419 | + _startedLoading = true; | |
| 420 | + QMetaObject::invokeMethod(this, "loadContent", Qt::QueuedConnection, Q_ARG(const QByteArray&, content)); | |
| 421 | + } | |
| 422 | +} | |
| 423 | + | |
| 424 | +class ImageReader : public QRunnable { | |
| 425 | +public: | |
| 426 | + | |
| 427 | + ImageReader(const QWeakPointer<Resource>& texture, QNetworkReply* reply, const QUrl& url = QUrl(), | |
| 428 | + const QByteArray& content = QByteArray()); | |
| 429 | + | |
| 430 | + virtual void run(); | |
| 431 | + | |
| 432 | +private: | |
| 433 | + | |
| 434 | + QWeakPointer<Resource> _texture; | |
| 435 | + QNetworkReply* _reply; | |
| 436 | + QUrl _url; | |
| 437 | + QByteArray _content; | |
| 438 | +}; | |
| 439 | + | |
| 440 | +ImageReader::ImageReader(const QWeakPointer<Resource>& texture, QNetworkReply* reply, | |
| 441 | + const QUrl& url, const QByteArray& content) : | |
| 442 | + _texture(texture), | |
| 443 | + _reply(reply), | |
| 444 | + _url(url), | |
| 445 | + _content(content) { | |
| 446 | +} | |
| 447 | + | |
| 448 | +void ImageReader::run() { | |
| 449 | + QSharedPointer<Resource> texture = _texture.toStrongRef(); | |
| 450 | + if (texture.isNull()) { | |
| 451 | + if (_reply) { | |
| 452 | + _reply->deleteLater(); | |
| 453 | + } | |
| 454 | + return; | |
| 455 | + } | |
| 456 | + if (_reply) { | |
| 457 | + _url = _reply->url(); | |
| 458 | + _content = _reply->readAll(); | |
| 459 | + _reply->deleteLater(); | |
| 460 | + } | |
| 461 | + QImage image = QImage::fromData(_content); | |
| 462 | + | |
| 463 | + // enforce a fixed maximum | |
| 464 | + const int MAXIMUM_SIZE = 1024; | |
| 465 | + if (image.width() > MAXIMUM_SIZE || image.height() > MAXIMUM_SIZE) { | |
| 466 | + qDebug() << "Image greater than maximum size:" << _url << image.width() << image.height(); | |
| 467 | + image = image.scaled(MAXIMUM_SIZE, MAXIMUM_SIZE, Qt::KeepAspectRatio); | |
| 468 | + } | |
| 469 | + int imageArea = image.width() * image.height(); | |
| 470 | + | |
| 471 | + const int EIGHT_BIT_MAXIMUM = 255; | |
| 472 | + if (!image.hasAlphaChannel()) { | |
| 473 | + if (image.format() != QImage::Format_RGB888) { | |
| 474 | + image = image.convertToFormat(QImage::Format_RGB888); | |
| 475 | + } | |
| 476 | + int redTotal = 0, greenTotal = 0, blueTotal = 0; | |
| 477 | + for (int y = 0; y < image.height(); y++) { | |
| 478 | + for (int x = 0; x < image.width(); x++) { | |
| 479 | + QRgb rgb = image.pixel(x, y); | |
| 480 | + redTotal += qRed(rgb); | |
| 481 | + greenTotal += qGreen(rgb); | |
| 482 | + blueTotal += qBlue(rgb); | |
| 483 | + } | |
| 484 | + } | |
| 485 | + QColor averageColor(EIGHT_BIT_MAXIMUM, EIGHT_BIT_MAXIMUM, EIGHT_BIT_MAXIMUM); | |
| 486 | + if (imageArea > 0) { | |
| 487 | + averageColor.setRgb(redTotal / imageArea, greenTotal / imageArea, blueTotal / imageArea); | |
| 488 | + } | |
| 489 | + QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image), Q_ARG(bool, false), | |
| 490 | + Q_ARG(const QColor&, averageColor)); | |
| 491 | + return; | |
| 492 | + } | |
| 493 | + if (image.format() != QImage::Format_ARGB32) { | |
| 494 | + image = image.convertToFormat(QImage::Format_ARGB32); | |
| 495 | + } | |
| 496 | + | |
| 497 | + // check for translucency/false transparency | |
| 498 | + int opaquePixels = 0; | |
| 499 | + int translucentPixels = 0; | |
| 500 | + int redTotal = 0, greenTotal = 0, blueTotal = 0, alphaTotal = 0; | |
| 501 | + for (int y = 0; y < image.height(); y++) { | |
| 502 | + for (int x = 0; x < image.width(); x++) { | |
| 503 | + QRgb rgb = image.pixel(x, y); | |
| 504 | + redTotal += qRed(rgb); | |
| 505 | + greenTotal += qGreen(rgb); | |
| 506 | + blueTotal += qBlue(rgb); | |
| 507 | + int alpha = qAlpha(rgb); | |
| 508 | + alphaTotal += alpha; | |
| 509 | + if (alpha == EIGHT_BIT_MAXIMUM) { | |
| 510 | + opaquePixels++; | |
| 511 | + } else if (alpha != 0) { | |
| 512 | + translucentPixels++; | |
| 513 | + } | |
| 514 | + } | |
| 515 | + } | |
| 516 | + if (opaquePixels == imageArea) { | |
| 517 | + qDebug() << "Image with alpha channel is completely opaque:" << _url; | |
| 518 | + image = image.convertToFormat(QImage::Format_RGB888); | |
| 519 | + } | |
| 520 | + QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image), | |
| 521 | + Q_ARG(bool, translucentPixels >= imageArea / 2), Q_ARG(const QColor&, QColor(redTotal / imageArea, | |
| 522 | + greenTotal / imageArea, blueTotal / imageArea, alphaTotal / imageArea))); | |
| 523 | +} | |
| 524 | + | |
| 525 | +void NetworkTexture::downloadFinished(QNetworkReply* reply) { | |
| 526 | + // send the reader off to the thread pool | |
| 527 | + QThreadPool::globalInstance()->start(new ImageReader(_self, reply)); | |
| 528 | +} | |
| 529 | + | |
| 530 | +void NetworkTexture::loadContent(const QByteArray& content) { | |
| 531 | + QThreadPool::globalInstance()->start(new ImageReader(_self, NULL, _url, content)); | |
| 532 | +} | |
| 533 | + | |
| 534 | +void NetworkTexture::setImage(const QImage& image, bool translucent, const QColor& averageColor) { | |
| 535 | + _translucent = translucent; | |
| 536 | + _averageColor = averageColor; | |
| 537 | + | |
| 538 | + finishedLoading(true); | |
| 539 | + imageLoaded(image); | |
| 540 | + glBindTexture(GL_TEXTURE_2D, getID()); | |
| 541 | + if (image.hasAlphaChannel()) { | |
| 542 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, | |
| 543 | + GL_BGRA, GL_UNSIGNED_BYTE, image.constBits()); | |
| 544 | + } else { | |
| 545 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, | |
| 546 | + GL_RGB, GL_UNSIGNED_BYTE, image.constBits()); | |
| 547 | + } | |
| 548 | + // generate mipmaps | |
| 549 | + glGenerateMipmap(GL_TEXTURE_2D); | |
| 550 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); | |
| 551 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 552 | +} | |
| 553 | + | |
| 554 | +void NetworkTexture::imageLoaded(const QImage& image) { | |
| 555 | + // nothing by default | |
| 556 | +} | |
| 557 | + | |
| 558 | +DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url, const QByteArray& content) : | |
| 559 | + NetworkTexture(url, DEFAULT_TEXTURE, content), | |
| 560 | + _innerRadius(0), | |
| 561 | + _outerRadius(0) | |
| 562 | +{ | |
| 563 | +} | |
| 564 | + | |
| 565 | +QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilation) { | |
| 566 | + QSharedPointer<Texture> texture = _dilatedTextures.value(dilation); | |
| 567 | + if (texture.isNull()) { | |
| 568 | + texture = QSharedPointer<Texture>(new Texture()); | |
| 569 | + | |
| 570 | + if (!_image.isNull()) { | |
| 571 | + QImage dilatedImage = _image; | |
| 572 | + QPainter painter; | |
| 573 | + painter.begin(&dilatedImage); | |
| 574 | + QPainterPath path; | |
| 575 | + qreal radius = glm::mix((float) _innerRadius, (float) _outerRadius, dilation); | |
| 576 | + path.addEllipse(QPointF(_image.width() / 2.0, _image.height() / 2.0), radius, radius); | |
| 577 | + painter.fillPath(path, Qt::black); | |
| 578 | + painter.end(); | |
| 579 | + | |
| 580 | + glBindTexture(GL_TEXTURE_2D, texture->getID()); | |
| 581 | + if (dilatedImage.hasAlphaChannel()) { | |
| 582 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 0, | |
| 583 | + GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits()); | |
| 584 | + } else { | |
| 585 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dilatedImage.width(), dilatedImage.height(), 0, | |
| 586 | + GL_RGB, GL_UNSIGNED_BYTE, dilatedImage.constBits()); | |
| 587 | + } | |
| 588 | + glGenerateMipmap(GL_TEXTURE_2D); | |
| 589 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); | |
| 590 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 591 | + } | |
| 592 | + | |
| 593 | + _dilatedTextures.insert(dilation, texture); | |
| 594 | + } | |
| 595 | + return texture; | |
| 596 | +} | |
| 597 | + | |
| 598 | +void DilatableNetworkTexture::imageLoaded(const QImage& image) { | |
| 599 | + _image = image; | |
| 600 | + | |
| 601 | + // scan out from the center to find inner and outer radii | |
| 602 | + int halfWidth = image.width() / 2; | |
| 603 | + int halfHeight = image.height() / 2; | |
| 604 | + const int BLACK_THRESHOLD = 32; | |
| 605 | + while (_innerRadius < halfWidth && qGray(image.pixel(halfWidth + _innerRadius, halfHeight)) < BLACK_THRESHOLD) { | |
| 606 | + _innerRadius++; | |
| 607 | + } | |
| 608 | + _outerRadius = _innerRadius; | |
| 609 | + const int TRANSPARENT_THRESHOLD = 32; | |
| 610 | + while (_outerRadius < halfWidth && qAlpha(image.pixel(halfWidth + _outerRadius, halfHeight)) > TRANSPARENT_THRESHOLD) { | |
| 611 | + _outerRadius++; | |
| 612 | + } | |
| 613 | + | |
| 614 | + // clear out any textures we generated before loading | |
| 615 | + _dilatedTextures.clear(); | |
| 616 | +} | |
| 617 | + | |
| 618 | +void DilatableNetworkTexture::reinsert() { | |
| 619 | + static_cast<TextureCache*>(_cache.data())->_dilatableNetworkTextures.insert(_url, | |
| 620 | + qWeakPointerCast<NetworkTexture, Resource>(_self)); | |
| 621 | +} | |
| 622 | + | |