diff --git a/modules/globebrowsing/tile/layeredtextureshaderprovider.cpp b/modules/globebrowsing/tile/layeredtextureshaderprovider.cpp index d64a6b84f4..a7af680705 100644 --- a/modules/globebrowsing/tile/layeredtextureshaderprovider.cpp +++ b/modules/globebrowsing/tile/layeredtextureshaderprovider.cpp @@ -111,21 +111,27 @@ namespace openspace { ghoul::Dictionary shaderDictionary; - // Different texture types can be height maps or color texture for example + // Different texture types can be height maps or color texture for example. // These are used differently within the shaders. auto textureTypes = _preprocessingData.layeredTextureInfo; for (size_t i = 0; i < textureTypes.size(); i++) { // lastLayerIndex must be at least 0 for the shader to compile, - // the layer type is inactivated by setting useThisLayerType to false + // the layer type is inactivated by setting use to false shaderDictionary.setValue( - LayeredTextureInfo::glslKeyPrefixes[LayeredTextureInfo::GlslKeyPrefixes::lastLayerIndex] + - LayeredTextures::TEXTURE_CATEGORY_NAMES[i], glm::max(textureTypes[i].lastLayerIdx, 0)); + LayeredTextureInfo::glslKeyPrefixes[ + LayeredTextureInfo::GlslKeyPrefixes::lastLayerIndex] + + LayeredTextures::TEXTURE_CATEGORY_NAMES[i], + glm::max(textureTypes[i].lastLayerIdx, 0)); shaderDictionary.setValue( - LayeredTextureInfo::glslKeyPrefixes[LayeredTextureInfo::GlslKeyPrefixes::use] + - LayeredTextures::TEXTURE_CATEGORY_NAMES[i], textureTypes[i].lastLayerIdx >= 0); + LayeredTextureInfo::glslKeyPrefixes[ + LayeredTextureInfo::GlslKeyPrefixes::use] + + LayeredTextures::TEXTURE_CATEGORY_NAMES[i], + textureTypes[i].lastLayerIdx >= 0); shaderDictionary.setValue( - LayeredTextureInfo::glslKeyPrefixes[LayeredTextureInfo::GlslKeyPrefixes::blend] + - LayeredTextures::TEXTURE_CATEGORY_NAMES[i], textureTypes[i].layerBlendingEnabled); + LayeredTextureInfo::glslKeyPrefixes[ + LayeredTextureInfo::GlslKeyPrefixes::blend] + + LayeredTextures::TEXTURE_CATEGORY_NAMES[i], + textureTypes[i].layerBlendingEnabled); } // Other settings such as "useAtmosphere" @@ -153,7 +159,8 @@ namespace openspace { } - const std::string LayeredTextureShaderUniformIdHandler::glslTileDataNames[NUM_TILE_DATA_VARIABLES] = + const std::string LayeredTextureShaderUniformIdHandler::glslTileDataNames[ + NUM_TILE_DATA_VARIABLES] = { "textureSampler", "depthTransform.depthScale", @@ -162,7 +169,8 @@ namespace openspace { "uvTransform.uvScale" }; - const std::string LayeredTextureShaderUniformIdHandler::blendLayerSuffixes[NUM_BLEND_TEXTURES] = + const std::string LayeredTextureShaderUniformIdHandler::blendLayerSuffixes[ + NUM_BLEND_TEXTURES] = { "", "Parent1", @@ -176,32 +184,40 @@ namespace openspace { LayeredTextureShaderUniformIdHandler::~LayeredTextureShaderUniformIdHandler() { + } - void LayeredTextureShaderUniformIdHandler::updateIdsIfNecessary(LayeredTextureShaderProvider* shaderProvider) + void LayeredTextureShaderUniformIdHandler::updateIdsIfNecessary( + LayeredTextureShaderProvider* shaderProvider) { if (shaderProvider->updatedOnLastCall()) { _shaderProvider = shaderProvider; - _shaderProvider->_programObject->setIgnoreUniformLocationError(ProgramObject::IgnoreError::Yes); + // Ignore errors since this loops through even uniforms that does not exist. + _shaderProvider->_programObject->setIgnoreUniformLocationError( + ProgramObject::IgnoreError::Yes); for (size_t i = 0; i < LayeredTextures::NUM_TEXTURE_CATEGORIES; i++) { for (size_t j = 0; j < NUM_BLEND_TEXTURES; j++) { - for (size_t k = 0; k < LayeredTextures::MAX_NUM_TEXTURES_PER_CATEGORY; k++) + for (size_t k = 0; k < LayeredTextures::MAX_NUM_TEXTURES_PER_CATEGORY; + k++) { for (size_t l = 0; l < NUM_TILE_DATA_VARIABLES; l++) { - _tileUniformIds[i][j][k][l] = _shaderProvider->_programObject->uniformLocation( - LayeredTextures::TEXTURE_CATEGORY_NAMES[i] + - blendLayerSuffixes[j] + - "[" + std::to_string(k) + "]." + - glslTileDataNames[l]); + _tileUniformIds[i][j][k][l] = + _shaderProvider->_programObject->uniformLocation( + LayeredTextures::TEXTURE_CATEGORY_NAMES[i] + + blendLayerSuffixes[j] + + "[" + std::to_string(k) + "]." + + glslTileDataNames[l]); } } } } - _shaderProvider->_programObject->setIgnoreUniformLocationError(ProgramObject::IgnoreError::No); + // Reset ignore errors + _shaderProvider->_programObject->setIgnoreUniformLocationError( + ProgramObject::IgnoreError::No); } } diff --git a/modules/globebrowsing/tile/layeredtextureshaderprovider.h b/modules/globebrowsing/tile/layeredtextureshaderprovider.h index 4c6ac01561..841afd7ace 100644 --- a/modules/globebrowsing/tile/layeredtextureshaderprovider.h +++ b/modules/globebrowsing/tile/layeredtextureshaderprovider.h @@ -25,15 +25,16 @@ #ifndef __LAYERED_TEXTURE_SHADER_PROVIDER__ #define __LAYERED_TEXTURE_SHADER_PROVIDER__ +#include + +#include "ghoul/opengl/programobject.h" + #include #include #include -#include "ghoul/opengl/programobject.h" - -#include ////////////////////////////////////////////////////////////////////////////////////////// -// LAYERED TEXTURE SHADER PROVIDER // +// LAYERED TEXTURE SHADER PROVIDER // ////////////////////////////////////////////////////////////////////////////////////////// namespace openspace { @@ -41,6 +42,9 @@ namespace openspace { class LayeredTextureShaderUniformIdHandler; + /** + * Settings per texture category that contains shader preprocessing information. + */ struct LayeredTextureInfo { static const size_t NUM_SETTINGS_PER_CATEGORY = 3; @@ -58,13 +62,27 @@ namespace openspace { bool operator==(const LayeredTextureInfo& other) const; }; + /** + * Data needed for shader preprocessing before compiling a layered texture shader + * program. + * + * If a LayeredTexturePreprocessingData is compared with another it can + * be determined wheter or not a LayeredTextureShaderProvider needs to + * recompile its shader program. For each TextureCategory there is + * information about how many layers it has and whether or not to blend the texture + * levels. + */ struct LayeredTexturePreprocessingData { - std::array layeredTextureInfo; + std::array + layeredTextureInfo; std::vector > keyValuePairs; bool operator==(const LayeredTexturePreprocessingData& other) const; }; + /** + * This class has ownership of an updated shader program for rendering tiles. + */ class LayeredTextureShaderProvider { public: @@ -74,6 +92,13 @@ namespace openspace { const std::string& fsPath); ~LayeredTextureShaderProvider(); + /** + * Returns a pointer to a ProgramObject for rendering tiles. + * \param preprocessingData determines wherer or not the shader + * program needs to be re-compiled. If preprocessingData is different + * from the last time this function was called the shader program will be + * recompiled before returned. + */ ProgramObject* getUpdatedShaderProgram( LayeredTexturePreprocessingData preprocessingData); @@ -92,14 +117,36 @@ namespace openspace { bool _updatedOnLastCall; }; + /** + * This class caches OpenGL uniform IDs for LayeredTextureShaderProviders. + */ class LayeredTextureShaderUniformIdHandler { - public: - static const size_t NUM_TILE_DATA_VARIABLES = 5; static const size_t NUM_BLEND_TEXTURES = 3; + /** + * Each texture can have these uniform variables associated with it in the shader + * code. + * + * textureSampler is the actual texture that can be sampled in the + * shader program. The associated GLSL type is sampler2D. + * depthTransform_depthScale specifies the scale part of the depth + * transform. Useful for height maps. The associated GLSL type is + * float. + * depthTransform_depthOffset specifies the offset part of the depth + * transform. Useful for height maps. The associated GLSL type is + * float. + * uvTransform_uvOffset specifies an offset that can be used when + * sampling from the texture. The associated GLSL type is vec2. + * uvTransform_uvScale specifies a scale that can be used when + * sampling from the texture. The associated GLSL type is vec2. + * + * The corresponding struct in GLSL code for storing these data is a + * Tile. The names of the uniforms are the ones specified in + * glslTileDataNames. + */ enum GlslTileDataId { textureSampler, depthTransform_depthScale, @@ -108,7 +155,13 @@ namespace openspace { uvTransform_uvScale, }; - enum BlendLayerSuffix { + /** + * These suffixes are used when naming Tiles in GLSL code. The names + * of the Tiles is one of + * LayeredTextures::TEXTURE_CATEGORY_NAMES followed by the suffixes + * defined in blendLayerSuffixes. + */ + enum BlendLayerSuffixes { none, Parent1, Parent2, @@ -118,6 +171,22 @@ namespace openspace { ~LayeredTextureShaderUniformIdHandler(); void updateIdsIfNecessary(LayeredTextureShaderProvider* shaderProvider); + /** + * \param category can be one of the categories specified in + * LayeredTextures::TextureCategory. + * \param blendLayer can have a value between 0 and + * NUM_BLEND_TEXTURES and specified that it is the uniform of that + * specific blend layer that is requested. + * \param layerIndex should have a value between 0 and + * LayeredTextures::MAX_NUM_TEXTURES_PER_CATEGORY - 1 and will specify + * which of the texture layers that is requested. + * \param tileDataId specifies what variable for the texture that + * should be returned. It can have any of the values specified in + * GlslTileDataId. + * + * \returns an OpenGL uniform ID for the specified arguments. If the uniform does + * not exist in the shader program it returns -1. + */ GLint getId( LayeredTextures::TextureCategory category, size_t blendLayer, @@ -140,9 +209,7 @@ namespace openspace { _tileUniformIds; LayeredTextureShaderProvider* _shaderProvider; - }; - } // namespace openspace #endif // __LAYERED_TEXTURE_SHADER_PROVIDER__ \ No newline at end of file