From 4e95aaab8250a0736e605b2ef61fa0ba0e521eff Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 12 May 2016 11:06:49 -0400 Subject: [PATCH 1/2] Using scaling and offset for heightmap based on dataset. --- modules/globebrowsing/globes/chunk.cpp | 7 +++-- .../globebrowsing/other/gdaldataconverter.inl | 14 ++++----- modules/globebrowsing/other/tileprovider.cpp | 14 ++++++++- modules/globebrowsing/other/tileprovider.h | 13 ++++++-- .../globebrowsing/rendering/patchrenderer.cpp | 30 ++++++++++++------- .../shaders/globalchunkedlodpatch_fs.glsl | 1 + .../shaders/globalchunkedlodpatch_vs.glsl | 7 ++++- .../shaders/localchunkedlodpatch_fs.glsl | 2 ++ .../shaders/localchunkedlodpatch_vs.glsl | 7 +++-- 9 files changed, 67 insertions(+), 28 deletions(-) diff --git a/modules/globebrowsing/globes/chunk.cpp b/modules/globebrowsing/globes/chunk.cpp index 1947224f38..7ddd0b694d 100644 --- a/modules/globebrowsing/globes/chunk.cpp +++ b/modules/globebrowsing/globes/chunk.cpp @@ -97,10 +97,11 @@ namespace openspace { return WANT_MERGE; } - auto center = _surfacePatch.center(); - Vec3 globePosition = data.position.dvec3(); - Vec3 patchPosition = globePosition + ellipsoid.geodetic2ToCartesian(center); Vec3 cameraPosition = data.camera.position().dvec3(); + Geodetic2 pointOnPatch = _surfacePatch.closestPoint( + ellipsoid.cartesianToGeodetic2(cameraPosition)); + Vec3 globePosition = data.position.dvec3(); + Vec3 patchPosition = globePosition + ellipsoid.geodetic2ToCartesian(pointOnPatch); Vec3 cameraToChunk = patchPosition - cameraPosition; Scalar minimumGlobeRadius = ellipsoid.minimumRadius(); diff --git a/modules/globebrowsing/other/gdaldataconverter.inl b/modules/globebrowsing/other/gdaldataconverter.inl index 5ce861c62d..372db231c5 100644 --- a/modules/globebrowsing/other/gdaldataconverter.inl +++ b/modules/globebrowsing/other/gdaldataconverter.inl @@ -100,15 +100,15 @@ namespace openspace { int xSize = rasterBand->GetXSize(); int ySize = rasterBand->GetYSize(); - CPLErr err = rasterBand->RasterIO( + CPLErr err = rasterBand->RasterIO( GF_Read, pixelStart.x, // Begin read x pixelStart.y, // Begin read y - numPixels.x, // width to read x - numPixels.y, // width to read y + numPixels.x, // width to read x + numPixels.y, // width to read y imageData + i, // Where to put data - numPixels.x, // width to write x in destination - numPixels.y, // width to write y in destination + numPixels.x, // width to write x in destination + numPixels.y, // width to write y in destination gdalType, // Type sizeof(T) * nRasters, // Pixel spacing 0); // Line spacing @@ -129,13 +129,13 @@ namespace openspace { delete[] imageData; glm::uvec3 dims(numPixels.x, numPixels.y, 1); - UninitializedTextureTile::TextureFormat textrureFormat = + UninitializedTextureTile::TextureFormat textureFormat = getTextureFormat(nRasters, gdalType); GLuint glType = getGlDataTypeFromGdalDataType(gdalType); UninitializedTextureTile* uninitedTexPtr = new UninitializedTextureTile( imageDataYflipped, dims, - textrureFormat, + textureFormat, glType, chunkIndex); std::shared_ptr uninitedTex = diff --git a/modules/globebrowsing/other/tileprovider.cpp b/modules/globebrowsing/other/tileprovider.cpp index ef5fe976c4..de20e63371 100644 --- a/modules/globebrowsing/other/tileprovider.cpp +++ b/modules/globebrowsing/other/tileprovider.cpp @@ -91,6 +91,13 @@ namespace openspace { int sizeLevel0 = firstBand->GetOverview(numOverviews - 1)->GetXSize(); _tileLevelDifference = log2(minimumPixelSize) - log2(sizeLevel0); + + GDALDataType gdalType = firstBand->GetRasterDataType(); + double maximumValue = (gdalType == GDT_Float32 || gdalType == GDT_Float64) ? + 1.0 : firstBand->GetMaximum(); + + _depthTransform.depthOffset = firstBand->GetOffset(); + _depthTransform.depthScale = firstBand->GetScale() * maximumValue; } TileProvider::~TileProvider(){ @@ -169,7 +176,7 @@ namespace openspace { } } - return { tex, uvOffset, uvScale }; + return{ tex, {uvOffset, uvScale } }; } std::shared_ptr TileProvider::getOrStartFetchingTile(ChunkIndex chunkIndex) { @@ -198,6 +205,11 @@ namespace openspace { return _defaultTexture; } + TileTextureDepthTransform TileProvider::depthTransform() { + return _depthTransform; + } + + std::shared_ptr TileProvider::getUninitializedTextureTile( const ChunkIndex& chunkIndex) { diff --git a/modules/globebrowsing/other/tileprovider.h b/modules/globebrowsing/other/tileprovider.h index 0ee00e5838..9fe3990498 100644 --- a/modules/globebrowsing/other/tileprovider.h +++ b/modules/globebrowsing/other/tileprovider.h @@ -46,7 +46,13 @@ namespace openspace { using namespace ghoul::opengl; - struct TileTextureTransform + struct TileTextureDepthTransform + { + float depthScale; + float depthOffset; + }; + + struct TileTextureUvTransform { glm::vec2 uvOffset; glm::vec2 uvScale; @@ -54,7 +60,7 @@ namespace openspace { struct Tile { std::shared_ptr texture; - TileTextureTransform transform; + TileTextureUvTransform uvTransform; }; /** @@ -69,8 +75,8 @@ namespace openspace { Tile getMostHiResTile(ChunkIndex chunkIndex); std::shared_ptr getOrStartFetchingTile(ChunkIndex chunkIndex); - std::shared_ptr getDefaultTexture(); + TileTextureDepthTransform depthTransform(); void prerender(); @@ -110,6 +116,7 @@ namespace openspace { std::shared_ptr _defaultTexture; int _tileLevelDifference; + TileTextureDepthTransform _depthTransform; }; } // namespace openspace diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index 1e5df03114..1d98498aa4 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -92,13 +92,13 @@ namespace openspace { , _grid(grid) { _programObjectGlobalRendering = OsEng.renderEngine().buildRenderProgram( - "GlobalClipMapPatch", + "GlobalChunkedLodPatch", "${MODULE_GLOBEBROWSING}/shaders/globalchunkedlodpatch_vs.glsl", "${MODULE_GLOBEBROWSING}/shaders/globalchunkedlodpatch_fs.glsl"); ghoul_assert(_programObjectGlobalRendering != nullptr, "Failed to initialize programObject!"); _programObjectLocalRendering = OsEng.renderEngine().buildRenderProgram( - "LocalClipMapPatch", + "LocalChunkedLodPatch", "${MODULE_GLOBEBROWSING}/shaders/localchunkedlodpatch_vs.glsl", "${MODULE_GLOBEBROWSING}/shaders/localchunkedlodpatch_fs.glsl"); ghoul_assert(_programObjectLocalRendering != nullptr, "Failed to initialize programObject!"); @@ -113,7 +113,7 @@ namespace openspace { const Ellipsoid& ellipsoid, const RenderData& data) { - if (chunk.index().level < 10) { + if (chunk.index().level < 9) { renderChunkGlobally(chunk, ellipsoid, data); } else { @@ -152,8 +152,8 @@ namespace openspace { heightTile.texture->bind(); _programObjectGlobalRendering->setUniform("textureSamplerHeight", texUnitHeight); - _programObjectGlobalRendering->setUniform("heightSamplingScale", heightTile.transform.uvScale); - _programObjectGlobalRendering->setUniform("heightSamplingOffset", heightTile.transform.uvOffset); + _programObjectGlobalRendering->setUniform("heightSamplingScale", heightTile.uvTransform.uvScale); + _programObjectGlobalRendering->setUniform("heightSamplingOffset", heightTile.uvTransform.uvOffset); @@ -168,9 +168,12 @@ namespace openspace { texUnitColor.activate(); colorTile.texture->bind(); _programObjectGlobalRendering->setUniform("textureSamplerColor", texUnitColor); - _programObjectGlobalRendering->setUniform("colorSamplingScale", colorTile.transform.uvScale); - _programObjectGlobalRendering->setUniform("colorSamplingOffset", colorTile.transform.uvOffset); + _programObjectGlobalRendering->setUniform("colorSamplingScale", colorTile.uvTransform.uvScale); + _programObjectGlobalRendering->setUniform("colorSamplingOffset", colorTile.uvTransform.uvOffset); + TileTextureDepthTransform depthTransformHeight = tileProviderHeight->depthTransform(); + _programObjectGlobalRendering->setUniform("heightSamplingDepthScale", depthTransformHeight.depthScale); + _programObjectGlobalRendering->setUniform("heightSamplingDepthOffset", depthTransformHeight.depthOffset); Geodetic2 swCorner = chunk.surfacePatch().southWestCorner(); auto patchSize = chunk.surfacePatch().size(); @@ -219,8 +222,12 @@ namespace openspace { texUnitHeight.activate(); heightTile.texture->bind(); _programObjectLocalRendering->setUniform("textureSamplerHeight", texUnitHeight); - _programObjectLocalRendering->setUniform("heightSamplingScale", heightTile.transform.uvScale); - _programObjectLocalRendering->setUniform("heightSamplingOffset", heightTile.transform.uvOffset); + _programObjectLocalRendering->setUniform("heightSamplingScale", heightTile.uvTransform.uvScale); + _programObjectLocalRendering->setUniform("heightSamplingOffset", heightTile.uvTransform.uvOffset); + + TileTextureDepthTransform depthTransformHeight = tileProviderHeight->depthTransform(); + _programObjectLocalRendering->setUniform("heightSamplingDepthScale", depthTransformHeight.depthScale); + _programObjectLocalRendering->setUniform("heightSamplingDepthOffset", depthTransformHeight.depthOffset); // Pick the first color texture auto colorTextureProviders = _tileProviderManager->colorTextureProviders(); @@ -233,8 +240,8 @@ namespace openspace { texUnitColor.activate(); colorTile.texture->bind(); _programObjectLocalRendering->setUniform("textureSamplerColor", texUnitColor); - _programObjectLocalRendering->setUniform("colorSamplingScale", colorTile.transform.uvScale); - _programObjectLocalRendering->setUniform("colorSamplingOffset", colorTile.transform.uvOffset); + _programObjectLocalRendering->setUniform("colorSamplingScale", colorTile.uvTransform.uvScale); + _programObjectLocalRendering->setUniform("colorSamplingOffset", colorTile.uvTransform.uvOffset); Geodetic2 sw = chunk.surfacePatch().southWestCorner(); @@ -263,6 +270,7 @@ namespace openspace { vec3 patchNormalCameraSpace = normalize( cross(patchSeCameraSpace - patchSwCameraSpace, patchNwCameraSpace - patchSwCameraSpace)); + _programObjectLocalRendering->setUniform( "patchNormalCameraSpace", patchNormalCameraSpace); diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl index 414ff6135a..d8d4a7ce9d 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl @@ -50,6 +50,7 @@ Fragment getFragment() { vec2 samplePos = colorSamplingScale*fs_uv + colorSamplingOffset; frag.color = texture(textureSamplerColor, samplePos); + //frag.color.rgb *= 10; // Sample position overlay //frag.color = frag.color * 0.9 + 0.2*vec4(samplePos, 0, 1); diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl index b712c77ba7..20d81dd409 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl @@ -34,6 +34,9 @@ uniform sampler2D textureSamplerHeight; uniform vec2 heightSamplingScale; uniform vec2 heightSamplingOffset; +uniform float heightSamplingDepthScale; +uniform float heightSamplingDepthOffset; + layout(location = 1) in vec2 in_UV; out vec4 vs_position; @@ -57,7 +60,9 @@ void main() vec2 samplePos = heightSamplingScale*in_UV + heightSamplingOffset; float sampledHeight = texture(textureSamplerHeight, samplePos).r; - pair.position += pair.normal * sampledHeight * pow(2,15); + if (sampledHeight < 0) + sampledHeight *= -10000000; + pair.position += pair.normal * (sampledHeight * heightSamplingDepthScale + heightSamplingDepthOffset); vec4 position = modelViewProjectionTransform * vec4(pair.position, 1); diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl index 54fccc6a16..4ab9921ebb 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl @@ -46,6 +46,8 @@ Fragment getFragment() { vec2 samplePos = colorSamplingScale * fs_uv + colorSamplingOffset; frag.color = texture(textureSamplerColor, samplePos);// + vec4(0.5,0,0,0); + //frag.color.rgb *= 10; + // Sample position overlay //frag.color = frag.color * 0.9 + 0.2*vec4(samplePos, 0, 1); diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl index c1ba33dfc0..560cba3c59 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl @@ -39,6 +39,9 @@ uniform sampler2D textureSamplerHeight; uniform vec2 heightSamplingScale; uniform vec2 heightSamplingOffset; +uniform float heightSamplingDepthScale; +uniform float heightSamplingDepthOffset; + layout(location = 1) in vec2 in_uv; out vec2 fs_uv; @@ -66,9 +69,9 @@ void main() vec2 samplePos = heightSamplingScale * in_uv + heightSamplingOffset; float sampledHeight = texture(textureSamplerHeight, samplePos).r; - + // Translate the point along normal - p += patchNormalCameraSpace * sampledHeight * pow(2,15); + p += patchNormalCameraSpace * (sampledHeight * heightSamplingDepthScale + heightSamplingDepthOffset); vec4 positionClippingSpace = projectionTransform * vec4(p, 1); From edf08d2b3d4d494bd8a7820fbfcefb148cea8a5e Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 12 May 2016 13:15:50 -0400 Subject: [PATCH 2/2] Use datastructure TextureTile for tiles in shader. --- modules/globebrowsing/CMakeLists.txt | 1 + modules/globebrowsing/other/tileprovider.cpp | 2 +- modules/globebrowsing/other/tileprovider.h | 10 ++-- .../globebrowsing/rendering/patchrenderer.cpp | 37 +++++++------ .../shaders/globalchunkedlodpatch_fs.glsl | 33 ++++-------- .../shaders/globalchunkedlodpatch_vs.glsl | 45 ++++++++-------- .../shaders/localchunkedlodpatch_fs.glsl | 30 ++++------- .../shaders/localchunkedlodpatch_vs.glsl | 38 ++++++------- .../globebrowsing/shaders/texturetile.hglsl | 53 +++++++++++++++++++ 9 files changed, 143 insertions(+), 106 deletions(-) create mode 100644 modules/globebrowsing/shaders/texturetile.hglsl diff --git a/modules/globebrowsing/CMakeLists.txt b/modules/globebrowsing/CMakeLists.txt index 789cee3e42..4995b25560 100644 --- a/modules/globebrowsing/CMakeLists.txt +++ b/modules/globebrowsing/CMakeLists.txt @@ -96,6 +96,7 @@ source_group("Source Files" FILES ${SOURCE_FILES}) set(SHADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/shaders/ellipsoid.hglsl + ${CMAKE_CURRENT_SOURCE_DIR}/shaders/texturetile.hglsl ${CMAKE_CURRENT_SOURCE_DIR}/shaders/globalchunkedlodpatch_vs.glsl ${CMAKE_CURRENT_SOURCE_DIR}/shaders/globalchunkedlodpatch_fs.glsl diff --git a/modules/globebrowsing/other/tileprovider.cpp b/modules/globebrowsing/other/tileprovider.cpp index de20e63371..a8ef467eb4 100644 --- a/modules/globebrowsing/other/tileprovider.cpp +++ b/modules/globebrowsing/other/tileprovider.cpp @@ -205,7 +205,7 @@ namespace openspace { return _defaultTexture; } - TileTextureDepthTransform TileProvider::depthTransform() { + TileDepthTransform TileProvider::depthTransform() { return _depthTransform; } diff --git a/modules/globebrowsing/other/tileprovider.h b/modules/globebrowsing/other/tileprovider.h index 64abd71395..4117b5898d 100644 --- a/modules/globebrowsing/other/tileprovider.h +++ b/modules/globebrowsing/other/tileprovider.h @@ -46,13 +46,13 @@ namespace openspace { using namespace ghoul::opengl; - struct TileTextureDepthTransform + struct TileDepthTransform { float depthScale; float depthOffset; }; - struct TileTextureUvTransform + struct TileUvTransform { glm::vec2 uvOffset; glm::vec2 uvScale; @@ -60,7 +60,7 @@ namespace openspace { struct Tile { std::shared_ptr texture; - TileTextureUvTransform uvTransform; + TileUvTransform uvTransform; }; /** @@ -76,7 +76,7 @@ namespace openspace { std::shared_ptr getOrStartFetchingTile(ChunkIndex chunkIndex); std::shared_ptr getDefaultTexture(); - TileTextureDepthTransform depthTransform(); + TileDepthTransform depthTransform(); void prerender(); @@ -116,7 +116,7 @@ namespace openspace { std::shared_ptr _defaultTexture; int _tileLevelDifference; - TileTextureDepthTransform _depthTransform; + TileDepthTransform _depthTransform; }; } // namespace openspace diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index 1d98498aa4..6e9fbb7770 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -150,11 +150,14 @@ namespace openspace { ghoul::opengl::TextureUnit texUnitHeight; texUnitHeight.activate(); heightTile.texture->bind(); - _programObjectGlobalRendering->setUniform("textureSamplerHeight", texUnitHeight); + _programObjectGlobalRendering->setUniform("heightTile.textureSampler", texUnitHeight); - _programObjectGlobalRendering->setUniform("heightSamplingScale", heightTile.uvTransform.uvScale); - _programObjectGlobalRendering->setUniform("heightSamplingOffset", heightTile.uvTransform.uvOffset); + _programObjectGlobalRendering->setUniform("heightTile.uvTransform.uvScale", heightTile.uvTransform.uvScale); + _programObjectGlobalRendering->setUniform("heightTile.uvTransform.uvOffset", heightTile.uvTransform.uvOffset); + TileDepthTransform depthTransformHeight = tileProviderHeight->depthTransform(); + _programObjectGlobalRendering->setUniform("heightTile.depthTransform.depthScale", depthTransformHeight.depthScale); + _programObjectGlobalRendering->setUniform("heightTile.depthTransform.depthOffset", depthTransformHeight.depthOffset); // Pick the first color texture @@ -167,13 +170,9 @@ namespace openspace { ghoul::opengl::TextureUnit texUnitColor; texUnitColor.activate(); colorTile.texture->bind(); - _programObjectGlobalRendering->setUniform("textureSamplerColor", texUnitColor); - _programObjectGlobalRendering->setUniform("colorSamplingScale", colorTile.uvTransform.uvScale); - _programObjectGlobalRendering->setUniform("colorSamplingOffset", colorTile.uvTransform.uvOffset); - - TileTextureDepthTransform depthTransformHeight = tileProviderHeight->depthTransform(); - _programObjectGlobalRendering->setUniform("heightSamplingDepthScale", depthTransformHeight.depthScale); - _programObjectGlobalRendering->setUniform("heightSamplingDepthOffset", depthTransformHeight.depthOffset); + _programObjectGlobalRendering->setUniform("colorTile.textureSampler", texUnitColor); + _programObjectGlobalRendering->setUniform("colorTile.uvTransform.uvScale", colorTile.uvTransform.uvScale); + _programObjectGlobalRendering->setUniform("colorTile.uvTransform.uvOffset", colorTile.uvTransform.uvOffset); Geodetic2 swCorner = chunk.surfacePatch().southWestCorner(); auto patchSize = chunk.surfacePatch().size(); @@ -221,13 +220,13 @@ namespace openspace { ghoul::opengl::TextureUnit texUnitHeight; texUnitHeight.activate(); heightTile.texture->bind(); - _programObjectLocalRendering->setUniform("textureSamplerHeight", texUnitHeight); - _programObjectLocalRendering->setUniform("heightSamplingScale", heightTile.uvTransform.uvScale); - _programObjectLocalRendering->setUniform("heightSamplingOffset", heightTile.uvTransform.uvOffset); + _programObjectLocalRendering->setUniform("heightTile.textureSampler", texUnitHeight); + _programObjectLocalRendering->setUniform("heightTile.uvTransform.uvScale", heightTile.uvTransform.uvScale); + _programObjectLocalRendering->setUniform("heightTile.uvTransform.uvOffset", heightTile.uvTransform.uvOffset); - TileTextureDepthTransform depthTransformHeight = tileProviderHeight->depthTransform(); - _programObjectLocalRendering->setUniform("heightSamplingDepthScale", depthTransformHeight.depthScale); - _programObjectLocalRendering->setUniform("heightSamplingDepthOffset", depthTransformHeight.depthOffset); + TileDepthTransform depthTransformHeight = tileProviderHeight->depthTransform(); + _programObjectLocalRendering->setUniform("heightTile.depthTransform.depthScale", depthTransformHeight.depthScale); + _programObjectLocalRendering->setUniform("heightTile.depthTransform.depthOffset", depthTransformHeight.depthOffset); // Pick the first color texture auto colorTextureProviders = _tileProviderManager->colorTextureProviders(); @@ -239,9 +238,9 @@ namespace openspace { ghoul::opengl::TextureUnit texUnitColor; texUnitColor.activate(); colorTile.texture->bind(); - _programObjectLocalRendering->setUniform("textureSamplerColor", texUnitColor); - _programObjectLocalRendering->setUniform("colorSamplingScale", colorTile.uvTransform.uvScale); - _programObjectLocalRendering->setUniform("colorSamplingOffset", colorTile.uvTransform.uvOffset); + _programObjectLocalRendering->setUniform("colorTile.textureSampler", texUnitColor); + _programObjectLocalRendering->setUniform("colorTile.uvTransform.uvScale", colorTile.uvTransform.uvScale); + _programObjectLocalRendering->setUniform("colorTile.uvTransform.uvOffset", colorTile.uvTransform.uvOffset); Geodetic2 sw = chunk.surfacePatch().southWestCorner(); diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl index d8d4a7ce9d..488cc35fa3 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl @@ -22,43 +22,32 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -// Colortexture coverage -uniform sampler2D textureSamplerColor; -uniform vec2 colorSamplingScale; -uniform vec2 colorSamplingOffset; - -in vec4 vs_position; -in vec3 fs_position; -in vec2 fs_uv; - +#include <${MODULE_GLOBEBROWSING}/shaders/texturetile.hglsl> #include "PowerScaling/powerScaling_fs.hglsl" #include "fragment.glsl" -vec4 borderOverlay(vec2 uv, vec3 borderColor, float borderSize){ - - vec2 uvOffset = uv - vec2(0.5); - float thres = 0.5 - borderSize/2; - bool isBorder = abs(uvOffset.x) > thres || abs(uvOffset.y) > thres; - vec3 color = isBorder ? borderColor : vec3(0); - return vec4(color, 0); -} - +uniform TextureTile colorTile; +in vec4 fs_position; +in vec2 fs_uv; Fragment getFragment() { Fragment frag; - vec2 samplePos = colorSamplingScale*fs_uv + colorSamplingOffset; - frag.color = texture(textureSamplerColor, samplePos); + vec2 samplePos = + colorTile.uvTransform.uvScale * fs_uv + + colorTile.uvTransform.uvOffset; + frag.color = texture(colorTile.textureSampler, samplePos); + //frag.color.rgb *= 10; // Sample position overlay //frag.color = frag.color * 0.9 + 0.2*vec4(samplePos, 0, 1); // Border overlay - //frag.color = frag.color + borderOverlay(fs_uv, vec3(0.5, 0.5, 0.5), 0.02); + //frag.color = frag.color + patchBorderOverlay(fs_uv, vec3(0.5, 0.5, 0.5), 0.02); - frag.depth = vs_position.w; + frag.depth = fs_position.w; return frag; } diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl index 20d81dd409..bf4f36ea2e 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl @@ -24,48 +24,51 @@ #version __CONTEXT__ +#include "PowerScaling/powerScaling_vs.hglsl" +#include <${MODULE_GLOBEBROWSING}/shaders/ellipsoid.hglsl> +#include <${MODULE_GLOBEBROWSING}/shaders/texturetile.hglsl> + uniform mat4 modelViewProjectionTransform; uniform vec3 radiiSquared; uniform vec2 minLatLon; uniform vec2 lonLatScalingFactor; -uniform sampler2D textureSamplerHeight; -uniform vec2 heightSamplingScale; -uniform vec2 heightSamplingOffset; +uniform TextureTile heightTile; -uniform float heightSamplingDepthScale; -uniform float heightSamplingDepthOffset; +layout(location = 1) in vec2 in_uv; -layout(location = 1) in vec2 in_UV; - -out vec4 vs_position; out vec2 fs_uv; - -#include "PowerScaling/powerScaling_vs.hglsl" -#include <${MODULE_GLOBEBROWSING}/shaders/ellipsoid.hglsl> +out vec4 fs_position; PositionNormalPair globalInterpolation() { vec2 lonLatInput; - lonLatInput.y = minLatLon.y + lonLatScalingFactor.y * in_UV.y; // Lat - lonLatInput.x = minLatLon.x + lonLatScalingFactor.x * in_UV.x; // Lon + lonLatInput.y = minLatLon.y + lonLatScalingFactor.y * in_uv.y; // Lat + lonLatInput.x = minLatLon.x + lonLatScalingFactor.x * in_uv.x; // Lon PositionNormalPair positionPairModelSpace = geodetic2ToCartesian(lonLatInput.y, lonLatInput.x, radiiSquared); return positionPairModelSpace; } void main() { - fs_uv = in_UV; PositionNormalPair pair = globalInterpolation(); - vec2 samplePos = heightSamplingScale*in_UV + heightSamplingOffset; - float sampledHeight = texture(textureSamplerHeight, samplePos).r; - if (sampledHeight < 0) - sampledHeight *= -10000000; - pair.position += pair.normal * (sampledHeight * heightSamplingDepthScale + heightSamplingDepthOffset); + vec2 samplePos = + heightTile.uvTransform.uvScale * in_uv + + heightTile.uvTransform.uvOffset; + + float sampledHeight = texture(heightTile.textureSampler, samplePos).r; + + pair.position += + pair.normal * + (sampledHeight * + heightTile.depthTransform.depthScale + + heightTile.depthTransform.depthOffset); vec4 position = modelViewProjectionTransform * vec4(pair.position, 1); - vs_position = z_normalization(position); - gl_Position = vs_position; + // Write output + fs_uv = in_uv; + fs_position = z_normalization(position); + gl_Position = fs_position; } \ No newline at end of file diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl index 4ab9921ebb..8f935d9225 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl @@ -22,38 +22,30 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -// Colortexture coverage -uniform sampler2D textureSamplerColor; -uniform vec2 colorSamplingScale; -uniform vec2 colorSamplingOffset; +#include <${MODULE_GLOBEBROWSING}/shaders/texturetile.hglsl> +#include "PowerScaling/powerScaling_fs.hglsl" +#include "fragment.glsl" + +uniform TextureTile colorTile; in vec4 fs_position; in vec2 fs_uv; -#include "PowerScaling/powerScaling_fs.hglsl" -#include "fragment.glsl" - -vec4 borderOverlay(vec2 uv, vec3 borderColor, float borderSize){ - vec2 uvOffset = uv - vec2(0.5); - float thres = 0.5 - borderSize/2; - bool isBorder = abs(uvOffset.x) > thres || abs(uvOffset.y) > thres; - vec3 color = isBorder ? borderColor : vec3(0); - return vec4(color, 0); -} - Fragment getFragment() { Fragment frag; - vec2 samplePos = colorSamplingScale * fs_uv + colorSamplingOffset; - frag.color = texture(textureSamplerColor, samplePos);// + vec4(0.5,0,0,0); - //frag.color.rgb *= 10; + vec2 samplePos = + colorTile.uvTransform.uvScale * fs_uv + + colorTile.uvTransform.uvOffset; + frag.color = texture(colorTile.textureSampler, samplePos); + //frag.color.rgb *= 10; // Sample position overlay //frag.color = frag.color * 0.9 + 0.2*vec4(samplePos, 0, 1); // Border overlay - //frag.color = frag.color + borderOverlay(fs_uv, vec3(0.5, 0.5, 0.5), 0.02); + //frag.color = frag.color + patchBorderOverlay(fs_uv, vec3(0.5, 0.5, 0.5), 0.02); frag.depth = fs_position.w; diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl index 560cba3c59..152acaa92b 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl @@ -25,6 +25,10 @@ #version __CONTEXT__ +#include "PowerScaling/powerScaling_vs.hglsl" +#include <${MODULE_GLOBEBROWSING}/shaders/ellipsoid.hglsl> +#include <${MODULE_GLOBEBROWSING}/shaders/texturetile.hglsl> + uniform mat4 projectionTransform; // Input points in camera space @@ -32,24 +36,15 @@ uniform vec3 p00; uniform vec3 p10; uniform vec3 p01; uniform vec3 p11; - uniform vec3 patchNormalCameraSpace; -uniform sampler2D textureSamplerHeight; -uniform vec2 heightSamplingScale; -uniform vec2 heightSamplingOffset; - -uniform float heightSamplingDepthScale; -uniform float heightSamplingDepthOffset; +uniform TextureTile heightTile; layout(location = 1) in vec2 in_uv; out vec2 fs_uv; out vec4 fs_position; -#include "PowerScaling/powerScaling_vs.hglsl" -#include <${MODULE_GLOBEBROWSING}/shaders/ellipsoid.hglsl> - vec3 bilinearInterpolation(vec2 uv) { // Bilinear interpolation vec3 p0 = (1 - uv.x) * p00 + uv.x * p10; @@ -60,21 +55,26 @@ vec3 bilinearInterpolation(vec2 uv) { void main() { - fs_uv = in_uv; - // Position in cameraspace - vec3 p = bilinearInterpolation(fs_uv); + vec3 p = bilinearInterpolation(in_uv); // Transform uv coordinates to texture space - vec2 samplePos = heightSamplingScale * in_uv + heightSamplingOffset; + vec2 samplePos = + heightTile.uvTransform.uvScale * in_uv + + heightTile.uvTransform.uvOffset; - float sampledHeight = texture(textureSamplerHeight, samplePos).r; + float sampledHeight = texture(heightTile.textureSampler, samplePos).r; // Translate the point along normal - p += patchNormalCameraSpace * (sampledHeight * heightSamplingDepthScale + heightSamplingDepthOffset); + p += patchNormalCameraSpace * + (sampledHeight * + heightTile.depthTransform.depthScale + + heightTile.depthTransform.depthOffset); vec4 positionClippingSpace = projectionTransform * vec4(p, 1); - - gl_Position = z_normalization(positionClippingSpace); - fs_position = gl_Position; + + // Write output + fs_uv = in_uv; + fs_position = z_normalization(positionClippingSpace); + gl_Position = fs_position; } \ No newline at end of file diff --git a/modules/globebrowsing/shaders/texturetile.hglsl b/modules/globebrowsing/shaders/texturetile.hglsl new file mode 100644 index 0000000000..38ef1b8eb4 --- /dev/null +++ b/modules/globebrowsing/shaders/texturetile.hglsl @@ -0,0 +1,53 @@ +/***************************************************************************************** + * * + * 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 TEXTURETILE_HGLSL +#define TEXTURETILE_HGLSL + +struct TileDepthTransform { + float depthScale; + float depthOffset; +}; + +struct TileUvTransform { + vec2 uvOffset; + vec2 uvScale; +}; + +struct TextureTile { + sampler2D textureSampler; + + TileDepthTransform depthTransform; + TileUvTransform uvTransform; +}; + +vec4 patchBorderOverlay(vec2 uv, vec3 borderColor, float borderSize) { + vec2 uvOffset = uv - vec2(0.5); + float thres = 0.5 - borderSize/2; + bool isBorder = abs(uvOffset.x) > thres || abs(uvOffset.y) > thres; + vec3 color = isBorder ? borderColor : vec3(0); + return vec4(color, 0); +} + +#endif // TEXTURETILE_HGLSL \ No newline at end of file