Document LayeredTextureShaderProvider

This commit is contained in:
Kalle Bladin
2016-09-15 15:22:39 -04:00
parent f9e88fb40c
commit 80a9dfd431
2 changed files with 112 additions and 29 deletions

View File

@@ -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);
}
}

View File

@@ -25,15 +25,16 @@
#ifndef __LAYERED_TEXTURE_SHADER_PROVIDER__
#define __LAYERED_TEXTURE_SHADER_PROVIDER__
#include <modules/globebrowsing/tile/layeredtextures.h>
#include "ghoul/opengl/programobject.h"
#include <vector>
#include <array>
#include <string>
#include "ghoul/opengl/programobject.h"
#include <modules/globebrowsing/tile/layeredtextures.h>
//////////////////////////////////////////////////////////////////////////////////////////
// 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 <code>LayeredTexturePreprocessingData</code> is compared with another it can
* be determined wheter or not a <code>LayeredTextureShaderProvider</code> needs to
* recompile its shader program. For each <code>TextureCategory</code> there is
* information about how many layers it has and whether or not to blend the texture
* levels.
*/
struct LayeredTexturePreprocessingData
{
std::array<LayeredTextureInfo, LayeredTextures::NUM_TEXTURE_CATEGORIES> layeredTextureInfo;
std::array<LayeredTextureInfo, LayeredTextures::NUM_TEXTURE_CATEGORIES>
layeredTextureInfo;
std::vector<std::pair<std::string, std::string> > 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 <code>ProgramObject</code> for rendering tiles.
* \param <code>preprocessingData</code> determines wherer or not the shader
* program needs to be re-compiled. If <code>preprocessingData</code> 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 <code>LayeredTextureShaderProvider</code>s.
*/
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.
*
* <code>textureSampler</code> is the actual texture that can be sampled in the
* shader program. The associated GLSL type is <code>sampler2D</code>.
* <code>depthTransform_depthScale</code> specifies the scale part of the depth
* transform. Useful for height maps. The associated GLSL type is
* <code>float</code>.
* <code>depthTransform_depthOffset</code> specifies the offset part of the depth
* transform. Useful for height maps. The associated GLSL type is
* <code>float</code>.
* <code>uvTransform_uvOffset</code> specifies an offset that can be used when
* sampling from the texture. The associated GLSL type is <code>vec2</code>.
* <code>uvTransform_uvScale</code> specifies a scale that can be used when
* sampling from the texture. The associated GLSL type is <code>vec2</code>.
*
* The corresponding struct in GLSL code for storing these data is a
* <code>Tile</code>. The names of the uniforms are the ones specified in
* <code>glslTileDataNames</code>.
*/
enum GlslTileDataId {
textureSampler,
depthTransform_depthScale,
@@ -108,7 +155,13 @@ namespace openspace {
uvTransform_uvScale,
};
enum BlendLayerSuffix {
/**
* These suffixes are used when naming <code>Tile</code>s in GLSL code. The names
* of the <code>Tile</code>s is one of
* <code>LayeredTextures::TEXTURE_CATEGORY_NAMES</code> followed by the suffixes
* defined in <code>blendLayerSuffixes</code>.
*/
enum BlendLayerSuffixes {
none,
Parent1,
Parent2,
@@ -118,6 +171,22 @@ namespace openspace {
~LayeredTextureShaderUniformIdHandler();
void updateIdsIfNecessary(LayeredTextureShaderProvider* shaderProvider);
/**
* \param <code>category</code> can be one of the categories specified in
* <code>LayeredTextures::TextureCategory</code>.
* \param <code>blendLayer</code> can have a value between 0 and
* <code>NUM_BLEND_TEXTURES</code> and specified that it is the uniform of that
* specific blend layer that is requested.
* \param <code>layerIndex</code> should have a value between 0 and
* <code>LayeredTextures::MAX_NUM_TEXTURES_PER_CATEGORY - 1</code> and will specify
* which of the texture layers that is requested.
* \param <code>tileDataId</code> specifies what variable for the texture that
* should be returned. It can have any of the values specified in
* <code>GlslTileDataId</code>.
*
* \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__