From 8ecefdfe392931be3cab7fdbebf1ae0239a3d816 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Fri, 10 Jul 2015 16:03:27 -0700 Subject: [PATCH 1/2] If an external texture isn't found, still display the model If the texture is present the next Interface is run then it will be used. --- .../src/RenderableModelEntityItem.cpp | 12 +++---- libraries/render-utils/src/Model.cpp | 33 +++++++++---------- libraries/render-utils/src/Model.h | 3 +- libraries/render-utils/src/TextureCache.cpp | 20 +++++++++-- libraries/render-utils/src/TextureCache.h | 8 +++++ 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 85b7bafc78..791b1de469 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -63,11 +63,11 @@ void RenderableModelEntityItem::remapTextures() { return; // nothing to do if we don't have a model } - if (!_model->isLoadedWithTextures()) { - return; // nothing to do if the model has not yet loaded its default textures + if (!_model->isLoaded()) { + return; // nothing to do if the model has not yet loaded } - if (!_originalTexturesRead && _model->isLoadedWithTextures()) { + if (!_originalTexturesRead) { const QSharedPointer& networkGeometry = _model->getGeometry(); if (networkGeometry) { _originalTextures = networkGeometry->getTextureNames(); @@ -119,7 +119,7 @@ bool RenderableModelEntityItem::readyToAddToScene(RenderArgs* renderArgs) { EntityTreeRenderer* renderer = static_cast(renderArgs->_renderer); getModel(renderer); } - if (renderArgs && _model && _needsInitialSimulation && _model->isActive() && _model->isLoadedWithTextures()) { + if (renderArgs && _model && _needsInitialSimulation && _model->isActive() && _model->isLoaded()) { _model->setScaleToFit(true, getDimensions()); _model->setSnapModelToRegistrationPoint(true, getRegistrationPoint()); _model->setRotation(getRotation()); @@ -397,8 +397,8 @@ bool RenderableModelEntityItem::isReadyToComputeShape() { const QSharedPointer collisionNetworkGeometry = _model->getCollisionGeometry(); const QSharedPointer renderNetworkGeometry = _model->getGeometry(); - if ((! collisionNetworkGeometry.isNull() && collisionNetworkGeometry->isLoadedWithTextures()) && - (! renderNetworkGeometry.isNull() && renderNetworkGeometry->isLoadedWithTextures())) { + if ((! collisionNetworkGeometry.isNull() && collisionNetworkGeometry->isLoaded()) && + (! renderNetworkGeometry.isNull() && renderNetworkGeometry->isLoaded())) { // we have both URLs AND both geometries AND they are both fully loaded. return true; } diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 03140c4dfb..edc74eac2f 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -824,7 +824,7 @@ void Model::renderSetup(RenderArgs* args) { } } - if (!_meshGroupsKnown && isLoadedWithTextures()) { + if (!_meshGroupsKnown && isLoaded()) { segregateMeshGroups(); } } @@ -883,7 +883,7 @@ void Model::setVisibleInScene(bool newValue, std::shared_ptr scen bool Model::addToScene(std::shared_ptr scene, render::PendingChanges& pendingChanges) { - if (!_meshGroupsKnown && isLoadedWithTextures()) { + if (!_meshGroupsKnown && isLoaded()) { segregateMeshGroups(); } @@ -913,7 +913,7 @@ bool Model::addToScene(std::shared_ptr scene, render::PendingChan } bool Model::addToScene(std::shared_ptr scene, render::PendingChanges& pendingChanges, render::Item::Status::Getters& statusGetters) { - if (!_meshGroupsKnown && isLoadedWithTextures()) { + if (!_meshGroupsKnown && isLoaded()) { segregateMeshGroups(); } @@ -2029,12 +2029,10 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran } } } - static bool showDiffuse = true; - if (showDiffuse && diffuseMap) { + if (diffuseMap && static_cast(diffuseMap)->isLoaded()) { batch.setUniformTexture(0, diffuseMap->getGPUTexture()); - } else { - batch.setUniformTexture(0, textureCache->getWhiteTexture()); + batch.setUniformTexture(0, textureCache->getGrayTexture()); } if (locations->texcoordMatrices >= 0) { @@ -2049,16 +2047,15 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran } if (!mesh.tangents.isEmpty()) { - Texture* normalMap = networkPart.normalTexture.data(); - batch.setUniformTexture(1, !normalMap ? - textureCache->getBlueTexture() : normalMap->getGPUTexture()); - + NetworkTexture* normalMap = networkPart.normalTexture.data(); + batch.setUniformTexture(1, !normalMap || !normalMap->isLoaded() ? + textureCache->getBlueTexture() : normalMap->getGPUTexture()); } if (locations->specularTextureUnit >= 0) { - Texture* specularMap = networkPart.specularTexture.data(); - batch.setUniformTexture(locations->specularTextureUnit, !specularMap ? - textureCache->getWhiteTexture() : specularMap->getGPUTexture()); + NetworkTexture* specularMap = networkPart.specularTexture.data(); + batch.setUniformTexture(locations->specularTextureUnit, !specularMap || !specularMap->isLoaded() ? + textureCache->getBlackTexture() : specularMap->getGPUTexture()); } if (args) { @@ -2073,9 +2070,9 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran float emissiveScale = part.emissiveParams.y; GLBATCH(glUniform2f)(locations->emissiveParams, emissiveOffset, emissiveScale); - Texture* emissiveMap = networkPart.emissiveTexture.data(); - batch.setUniformTexture(locations->emissiveTextureUnit, !emissiveMap ? - textureCache->getWhiteTexture() : emissiveMap->getGPUTexture()); + NetworkTexture* emissiveMap = networkPart.emissiveTexture.data(); + batch.setUniformTexture(locations->emissiveTextureUnit, !emissiveMap || !emissiveMap->isLoaded() ? + textureCache->getGrayTexture() : emissiveMap->getGPUTexture()); } if (translucent && locations->lightBufferUnit >= 0) { @@ -2188,7 +2185,7 @@ void Model::pickPrograms(gpu::Batch& batch, RenderMode mode, bool translucent, f } bool Model::initWhenReady(render::ScenePointer scene) { - if (isActive() && isRenderable() && !_meshGroupsKnown && isLoadedWithTextures()) { + if (isActive() && isRenderable() && !_meshGroupsKnown && isLoaded()) { segregateMeshGroups(); render::PendingChanges pendingChanges; diff --git a/libraries/render-utils/src/Model.h b/libraries/render-utils/src/Model.h index a6ce566a36..e1fcbcef25 100644 --- a/libraries/render-utils/src/Model.h +++ b/libraries/render-utils/src/Model.h @@ -106,6 +106,7 @@ public: void setVisibleInScene(bool newValue, std::shared_ptr scene); bool isVisible() const { return _isVisible; } + bool isLoaded() const { return _geometry && _geometry->isLoaded(); } bool isLoadedWithTextures() const { return _geometry && _geometry->isLoadedWithTextures(); } void init(); @@ -116,7 +117,7 @@ public: // new Scene/Engine rendering support bool needsFixupInScene() { return !_readyWhenAdded && readyToAddToScene(); } - bool readyToAddToScene(RenderArgs* renderArgs = nullptr) { return !_needsReload && isRenderable() && isActive() && isLoadedWithTextures(); } + bool readyToAddToScene(RenderArgs* renderArgs = nullptr) { return !_needsReload && isRenderable() && isActive() && isLoaded(); } bool addToScene(std::shared_ptr scene, render::PendingChanges& pendingChanges); bool addToScene(std::shared_ptr scene, render::PendingChanges& pendingChanges, render::Item::Status::Getters& statusGetters); void removeFromScene(std::shared_ptr scene, render::PendingChanges& pendingChanges); diff --git a/libraries/render-utils/src/TextureCache.cpp b/libraries/render-utils/src/TextureCache.cpp index 97385cb060..4df9718e24 100644 --- a/libraries/render-utils/src/TextureCache.cpp +++ b/libraries/render-utils/src/TextureCache.cpp @@ -118,9 +118,9 @@ const gpu::TexturePointer& TextureCache::getPermutationNormalTexture() { } const unsigned char OPAQUE_WHITE[] = { 0xFF, 0xFF, 0xFF, 0xFF }; -//const unsigned char TRANSPARENT_WHITE[] = { 0xFF, 0xFF, 0xFF, 0x0 }; -//const unsigned char OPAQUE_BLACK[] = { 0x0, 0x0, 0x0, 0xFF }; +const unsigned char OPAQUE_GRAY[] = { 0x80, 0x80, 0x80, 0xFF }; const unsigned char OPAQUE_BLUE[] = { 0x80, 0x80, 0xFF, 0xFF }; +const unsigned char OPAQUE_BLACK[] = { 0x00, 0x00, 0x00, 0xFF }; /* static void loadSingleColorTexture(const unsigned char* color) { @@ -137,6 +137,14 @@ const gpu::TexturePointer& TextureCache::getWhiteTexture() { return _whiteTexture; } +const gpu::TexturePointer& TextureCache::getGrayTexture() { + if (!_grayTexture) { + _grayTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA), 1, 1)); + _grayTexture->assignStoredMip(0, _whiteTexture->getTexelFormat(), sizeof(OPAQUE_WHITE), OPAQUE_GRAY); + } + return _grayTexture; +} + const gpu::TexturePointer& TextureCache::getBlueTexture() { if (!_blueTexture) { _blueTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA), 1, 1)); @@ -145,6 +153,14 @@ const gpu::TexturePointer& TextureCache::getBlueTexture() { return _blueTexture; } +const gpu::TexturePointer& TextureCache::getBlackTexture() { + if (!_blackTexture) { + _blackTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA), 1, 1)); + _blackTexture->assignStoredMip(0, _whiteTexture->getTexelFormat(), sizeof(OPAQUE_BLACK), OPAQUE_BLACK); + } + return _blackTexture; +} + /// Extra data for creating textures. class TextureExtra { public: diff --git a/libraries/render-utils/src/TextureCache.h b/libraries/render-utils/src/TextureCache.h index ba7176b2a4..fc02438c84 100644 --- a/libraries/render-utils/src/TextureCache.h +++ b/libraries/render-utils/src/TextureCache.h @@ -52,9 +52,15 @@ public: /// Returns an opaque white texture (useful for a default). const gpu::TexturePointer& getWhiteTexture(); + /// Returns an opaque gray texture (useful for a default). + const gpu::TexturePointer& getGrayTexture(); + /// Returns the a pale blue texture (useful for a normal map). const gpu::TexturePointer& getBlueTexture(); + /// Returns the a black texture (useful for a default). + const gpu::TexturePointer& getBlackTexture(); + /// Returns a texture version of an image file static gpu::TexturePointer getImageTexture(const QString& path); @@ -112,7 +118,9 @@ private: gpu::TexturePointer _permutationNormalTexture; gpu::TexturePointer _whiteTexture; + gpu::TexturePointer _grayTexture; gpu::TexturePointer _blueTexture; + gpu::TexturePointer _blackTexture; QHash > _dilatableNetworkTextures; From 2bab7d1c037845c87dbc18b521d546bf361a175a Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 13 Jul 2015 16:38:51 -0700 Subject: [PATCH 2/2] Add parentheses --- libraries/render-utils/src/Model.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 00d1d89fab..4d1a37fe61 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -2048,13 +2048,13 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran if (!mesh.tangents.isEmpty()) { NetworkTexture* normalMap = networkPart.normalTexture.data(); - batch.setResourceTexture(1, !normalMap || !normalMap->isLoaded() ? + batch.setResourceTexture(1, (!normalMap || !normalMap->isLoaded()) ? textureCache->getBlueTexture() : normalMap->getGPUTexture()); } if (locations->specularTextureUnit >= 0) { NetworkTexture* specularMap = networkPart.specularTexture.data(); - batch.setResourceTexture(locations->specularTextureUnit, !specularMap || !specularMap->isLoaded() ? + batch.setResourceTexture(locations->specularTextureUnit, (!specularMap || !specularMap->isLoaded()) ? textureCache->getBlackTexture() : specularMap->getGPUTexture()); } @@ -2071,7 +2071,7 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran GLBATCH(glUniform2f)(locations->emissiveParams, emissiveOffset, emissiveScale); NetworkTexture* emissiveMap = networkPart.emissiveTexture.data(); - batch.setResourceTexture(locations->emissiveTextureUnit, !emissiveMap || !emissiveMap->isLoaded() ? + batch.setResourceTexture(locations->emissiveTextureUnit, (!emissiveMap || !emissiveMap->isLoaded()) ? textureCache->getGrayTexture() : emissiveMap->getGPUTexture()); }