Files
OpenSpace/modules/globebrowsing/shaders/texturetilemapping.hglsl
T

287 lines
9.9 KiB
Plaintext

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef TEXTURETILEMAPPING_HGLSL
#define TEXTURETILEMAPPING_HGLSL
#include <${MODULE_GLOBEBROWSING}/shaders/tile.hglsl>
#include <${MODULE_GLOBEBROWSING}/shaders/blending.hglsl>
// First layer type from LayeredTextureShaderProvider is height map
#ifndef NUMLAYERS_HEIGHTMAP
#define NUMLAYERS_HEIGHTMAP #{lastLayerIndexHeight} + 1
#endif // NUMLAYERS_HEIGHTMAP
#ifndef USE_HEIGHTMAP
#define USE_HEIGHTMAP #{useHeightMap}
#endif // USE_HEIGHTMAP
#ifndef HEIGHTMAP_BLENDING_ENABLED
#define HEIGHTMAP_BLENDING_ENABLED #{heightMapBlendingEnabled}
#endif // HEIGHTMAP_BLENDING_ENABLED
// Second layer type from LayeredTextureShaderProvider is color texture
#ifndef NUMLAYERS_COLORTEXTURE
#define NUMLAYERS_COLORTEXTURE #{lastLayerIndexColor} + 1
#endif // NUMLAYERS_COLORTEXTURE
#ifndef USE_COLORTEXTURE
#define USE_COLORTEXTURE #{useColorTexture}
#endif // USE_COLORTEXTURE
#ifndef COLORTEXTURE_BLENDING_ENABLED
#define COLORTEXTURE_BLENDING_ENABLED #{colorTextureBlendingEnabled}
#endif // COLORTEXTURE_BLENDING_ENABLED
// Third layer type from LayeredTextureShaderProvider is water mask
#ifndef NUMLAYERS_WATERMASK
#define NUMLAYERS_WATERMASK #{lastLayerIndexWater} + 1
#endif // NUMLAYERS_WATERMASK
#ifndef USE_WATERMASK
#define USE_WATERMASK #{useWaterMask}
#endif // USE_WATERMASK
#ifndef WATERMASK_BLENDING_ENABLED
#define WATERMASK_BLENDING_ENABLED #{waterMaskBlendingEnabled}
#endif // WATERMASK_BLENDING_ENABLED
// Fourth layer type from LayeredTextureShaderProvider is night texture
#ifndef NUMLAYERS_NIGHTTEXTURE
#define NUMLAYERS_NIGHTTEXTURE #{lastLayerIndexNight} + 1
#endif // NUMLAYERS_NIGHTTEXTURE
#ifndef USE_NIGHTTEXTURE
#define USE_NIGHTTEXTURE #{useNightTexture}
#endif // USE_NIGHTTEXTURE
#ifndef NIGHTTEXTURE_BLENDING_ENABLED
#define NIGHTTEXTURE_BLENDING_ENABLED #{nightTextureBlendingEnabled}
#endif // NIGHTTEXTURE_BLENDING_ENABLED
// Fifth layer type from LayeredTextureShaderProvider is overlay
#ifndef NUMLAYERS_OVERLAY
#define NUMLAYERS_OVERLAY #{lastLayerIndexOverlay} + 1
#endif // NUMLAYERS_OVERLAY
#ifndef USE_OVERLAY
#define USE_OVERLAY #{useOverlay}
#endif // USE_OVERLAY
#ifndef OVERLAY_BLENDING_ENABLED
#define OVERLAY_BLENDING_ENABLED #{overlayBlendingEnabled}
#endif // OVERLAY_BLENDING_ENABLED
// Other key value pairs used for settings
#ifndef USE_ATMOSPHERE
#define USE_ATMOSPHERE #{useAtmosphere}
#endif // USE_ATMOSPHERE
float calculateHeight(
const vec2 uv,
const float tileInterpolationParameter,
const Tile heightTiles[NUMLAYERS_HEIGHTMAP],
const Tile heightTilesParent1[NUMLAYERS_HEIGHTMAP],
const Tile heightTilesParent2[NUMLAYERS_HEIGHTMAP]) {
float height = 0;
// The shader compiler will remove unused code when variables are multiplied by
// a constant 0
#if HEIGHTMAP_BLENDING_ENABLED
float w1 = clamp(1 - tileInterpolationParameter, 0 , 1);
float w2 = (clamp(tileInterpolationParameter, 0 , 1) - clamp(tileInterpolationParameter - 1, 0 , 1));
float w3 = clamp(tileInterpolationParameter - 1, 0 , 1);
#else // HEIGHTMAP_BLENDING_ENABLED
float w1 = 1;
float w2 = 0;
float w3 = 0;
#endif // HEIGHTMAP_BLENDING_ENABLED
#for i in 0..#{lastLayerIndexHeight}
{
float untransformedHeight =
w1 * getTexVal(heightTiles[#{i}], uv).r +
w2 * getTexVal(heightTilesParent1[#{i}], uv).r +
w3 * getTexVal(heightTilesParent2[#{i}], uv).r;
// OBS! Only the values from the last height map will be used!
height = getTransformedTexVal(heightTiles[#{i}].depthTransform, untransformedHeight);
}
#endfor
return height;
}
vec4 calculateColor(
const vec2 uv,
const float tileInterpolationParameter,
const Tile colorTiles[NUMLAYERS_COLORTEXTURE],
const Tile colorTilesParent1[NUMLAYERS_COLORTEXTURE],
const Tile colorTilesParent2[NUMLAYERS_COLORTEXTURE]) {
vec4 color = vec4(0);
// The shader compiler will remove unused code when variables are multiplied by
// a constant 0
#if COLORTEXTURE_BLENDING_ENABLED
float w1 = clamp(1 - tileInterpolationParameter, 0 , 1);
float w2 = (clamp(tileInterpolationParameter, 0 , 1) - clamp(tileInterpolationParameter - 1, 0 , 1));
float w3 = clamp(tileInterpolationParameter - 1, 0 , 1);
#else // COLORTEXTURE_BLENDING_ENABLED
float w1 = 1;
float w2 = 0;
float w3 = 0;
#endif // COLORTEXTURE_BLENDING_ENABLED
#for i in 0..#{lastLayerIndexColor}
{
vec4 colorSample =
w1 * getTexVal(colorTiles[#{i}], uv) +
w2 * getTexVal(colorTilesParent1[#{i}], uv) +
w3 * getTexVal(colorTilesParent2[#{i}], uv);
color = blendOver(color, colorSample);
}
#endfor
return color;
}
vec4 calculateNight(
const vec4 currentColor,
const vec2 uv,
const float tileInterpolationParameter,
const Tile nightTiles[NUMLAYERS_NIGHTTEXTURE],
const Tile nightTilesParent1[NUMLAYERS_NIGHTTEXTURE],
const Tile nightTilesParent2[NUMLAYERS_NIGHTTEXTURE],
const vec3 ellipsoidNormalCameraSpace) {
vec3 lightDirection = normalize(vec3(-1,-1,-1));
float cosineFactor = clamp(dot(-lightDirection, ellipsoidNormalCameraSpace), 0, 1);
vec4 nightColor = vec4(0,0,0,0);
// The shader compiler will remove unused code when variables are multiplied by
// a constant 0
#if NIGHTTEXTURE_BLENDING_ENABLED
float w1 = clamp(1 - tileInterpolationParameter, 0 , 1);
float w2 = (clamp(tileInterpolationParameter, 0 , 1) - clamp(tileInterpolationParameter - 1, 0 , 1));
float w3 = clamp(tileInterpolationParameter - 1, 0 , 1);
#else // NIGHTTEXTURE_BLENDING_ENABLED
float w1 = 1;
float w2 = 0;
float w3 = 0;
#endif // NIGHTTEXTURE_BLENDING_ENABLED
#for i in 0..#{lastLayerIndexNight}
{
vec4 colorSample =
w1 * getTexVal(nightTiles[#{i}], uv) +
w2 * getTexVal(nightTilesParent1[#{i}], uv) +
w3 * getTexVal(nightTilesParent2[#{i}], uv);
nightColor = blendOver(nightColor, colorSample);
}
#endfor
// Blend night color with base color
vec4 color = vec4(cosineFactor * vec3(currentColor) + (1 - cosineFactor) * vec3(nightColor), currentColor.a);
return color;
}
vec4 calculateOverlay(
const vec4 currentColor,
const vec2 uv,
const float tileInterpolationParameter,
const Tile overlayTiles[NUMLAYERS_OVERLAY],
const Tile overlayTilesParent1[NUMLAYERS_OVERLAY],
const Tile overlayTilesParent2[NUMLAYERS_OVERLAY]) {
vec4 color = currentColor;
// The shader compiler will remove unused code when variables are multiplied by
// a constant 0
#if OVERLAY_BLENDING_ENABLED
float w1 = clamp(1 - tileInterpolationParameter, 0 , 1);
float w2 = (clamp(tileInterpolationParameter, 0 , 1) - clamp(tileInterpolationParameter - 1, 0 , 1));
float w3 = clamp(tileInterpolationParameter - 1, 0 , 1);
#else // OVERLAY_BLENDING_ENABLED
float w1 = 1;
float w2 = 0;
float w3 = 0;
#endif // OVERLAY_BLENDING_ENABLED
#for i in 0..#{lastLayerIndexOverlay}
{
vec4 colorSample =
w1 * getTexVal(overlayTiles[#{i}], uv) +
w2 * getTexVal(overlayTilesParent1[#{i}], uv) +
w3 * getTexVal(overlayTilesParent2[#{i}], uv);
color = blendOver(color, colorSample);
}
#endfor
return color;
}
vec4 calculateWater(
const vec4 currentColor,
const vec2 uv,
const float tileInterpolationParameter,
const Tile waterTiles[NUMLAYERS_WATERMASK],
const Tile waterTilesParent1[NUMLAYERS_WATERMASK],
const Tile waterTilesParent2[NUMLAYERS_WATERMASK]) {
vec4 waterColor = vec4(0,0,0,0);
// The shader compiler will remove unused code when variables are multiplied by
// a constant 0
#if WATERMASK_BLENDING_ENABLED
float w1 = clamp(1 - tileInterpolationParameter, 0 , 1);
float w2 = (clamp(tileInterpolationParameter, 0 , 1) - clamp(tileInterpolationParameter - 1, 0 , 1));
float w3 = clamp(tileInterpolationParameter - 1, 0 , 1);
#else // WATERMASK_BLENDING_ENABLED
float w1 = 1;
float w2 = 0;
float w3 = 0;
#endif // WATERMASK_BLENDING_ENABLED
#for i in 0..#{lastLayerIndexWater}
{
vec4 colorSample =
w1 * getTexVal(waterTiles[#{i}], uv) +
w2 * getTexVal(waterTilesParent1[#{i}], uv) +
w3 * getTexVal(waterTilesParent2[#{i}], uv);
waterColor = blendOver(waterColor, colorSample);
}
#endfor
return blendOver(currentColor, waterColor);
}
#endif // TEXTURETILEMAPPING_HGLSL