From 992c828cce2fab7835671008a116d6cb5acb8278 Mon Sep 17 00:00:00 2001 From: Erik Broberg Date: Wed, 29 Jun 2016 00:32:25 -0400 Subject: [PATCH] Render vertex and heightmap resolution. Fix offsetted pixel read bug in TileDataset --- modules/globebrowsing/chunk/chunkrenderer.cpp | 4 +++ .../shaders/globalchunkedlodpatch_fs.glsl | 17 +++++++++++-- .../shaders/localchunkedlodpatch_fs.glsl | 15 +++++++++-- .../shaders/texturetilemapping.hglsl | 4 +-- modules/globebrowsing/shaders/tile.hglsl | 25 +++++++++++++------ modules/globebrowsing/tile/tiledataset.cpp | 23 +++++++++-------- modules/globebrowsing/tile/tiledataset.h | 4 ++- 7 files changed, 68 insertions(+), 24 deletions(-) diff --git a/modules/globebrowsing/chunk/chunkrenderer.cpp b/modules/globebrowsing/chunk/chunkrenderer.cpp index 0081659cd6..6f0093a317 100644 --- a/modules/globebrowsing/chunk/chunkrenderer.cpp +++ b/modules/globebrowsing/chunk/chunkrenderer.cpp @@ -282,6 +282,10 @@ namespace openspace { programObject->setUniform("skirtLength", min(static_cast(chunk.surfacePatch().halfSize().lat * 1000000), 8700.0f)); programObject->setUniform("xSegments", _grid->xSegments()); + if (tileProviders[LayeredTextures::ColorTextures].size() == 0) { + programObject->setUniform("vertexResolution", glm::vec2(_grid->xSegments(), _grid->ySegments())); + } + return programObject; } diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl index b3f6ea5893..63949b4d9c 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl @@ -28,6 +28,12 @@ #include "fragment.glsl" +#if USE_HEIGHTMAP +uniform Tile HeightMaps[NUMLAYERS_HEIGHTMAP]; +uniform Tile HeightMapsParent1[NUMLAYERS_HEIGHTMAP]; +uniform Tile HeightMapsParent2[NUMLAYERS_HEIGHTMAP]; +#endif // USE_HEIGHTMAP + #if USE_COLORTEXTURE uniform Tile ColorTextures[NUMLAYERS_COLORTEXTURE]; uniform Tile ColorTexturesParent1[NUMLAYERS_COLORTEXTURE]; @@ -58,6 +64,9 @@ uniform Tile WaterMasksParent1[NUMLAYERS_WATERMASK]; uniform Tile WaterMasksParent2[NUMLAYERS_WATERMASK]; #endif // USE_WATERMASK +uniform vec2 vertexResolution; + + #if USE_ATMOSPHERE // TODO atmosphere uniforms here #endif // USE_ATMOSPHERE @@ -83,8 +92,12 @@ Fragment getFragment() { ColorTexturesParent2); #else - vec2 vertexResolution = vec2(64.0); - frag.color = calculateDebugColor(fs_uv, fs_position, vertexResolution); + vec2 heightResolution = vec2(1,1); + #if USE_HEIGHTMAP + heightResolution = vec2(textureSize(HeightMaps[0].textureSampler,0)); + #endif + + frag.color = calculateDebugColor(fs_uv, fs_position, vertexResolution, heightResolution); #endif // USE_COLORTEXTURE #if USE_GRAYSCALE_OVERLAY diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl index 33af4fff32..ed5aab2668 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl @@ -27,6 +27,12 @@ #include "PowerScaling/powerScaling_fs.hglsl" #include "fragment.glsl" +#if USE_HEIGHTMAP +uniform Tile HeightMaps[NUMLAYERS_HEIGHTMAP]; +uniform Tile HeightMapsParent1[NUMLAYERS_HEIGHTMAP]; +uniform Tile HeightMapsParent2[NUMLAYERS_HEIGHTMAP]; +#endif // USE_HEIGHTMAP + #if USE_COLORTEXTURE uniform Tile ColorTextures[NUMLAYERS_COLORTEXTURE]; uniform Tile ColorTexturesParent1[NUMLAYERS_COLORTEXTURE]; @@ -57,6 +63,8 @@ uniform Tile WaterMasksParent1[NUMLAYERS_WATERMASK]; uniform Tile WaterMasksParent2[NUMLAYERS_WATERMASK]; #endif // USE_WATERMASK +uniform vec2 vertexResolution; + // levelInterpolationParameter is used to interpolate between a tile and its parent tiles // The value increases with the distance from the vertex (or fragment) to the camera in LevelWeights levelWeights; @@ -79,8 +87,11 @@ Fragment getFragment() { ColorTexturesParent1, ColorTexturesParent2); #else - vec2 vertexResolution = vec2(64.0); - frag.color = calculateDebugColor(fs_uv, fs_position, vertexResolution); + vec2 heightResolution = vec2(1,1); + #if USE_HEIGHTMAP + heightResolution = vec2(textureSize(HeightMaps[0].textureSampler,0)); + #endif + frag.color = calculateDebugColor(fs_uv, fs_position, vertexResolution, heightResolution); #endif // USE_COLORTEXTURE diff --git a/modules/globebrowsing/shaders/texturetilemapping.hglsl b/modules/globebrowsing/shaders/texturetilemapping.hglsl index 4e66272818..874f953afe 100644 --- a/modules/globebrowsing/shaders/texturetilemapping.hglsl +++ b/modules/globebrowsing/shaders/texturetilemapping.hglsl @@ -129,12 +129,12 @@ float gridDots(vec2 uv, vec2 gridResolution){ return 1-length(1-uvDotSpace); } -vec4 calculateDebugColor(vec2 uv, vec4 fragPos, vec2 vertexResolution){ +vec4 calculateDebugColor(vec2 uv, vec4 fragPos, vec2 vertexResolution, vec2 heightResolution){ vec2 uvVertexSpace = fract(vertexResolution * uv); vec3 colorUv = vec3(0.3*uv.x, 0.3*uv.y, 0); vec3 colorDistance = vec3(0, 0, min( 0.4*log(fragPos.w) - 3.9, 1)); vec3 colorVertex = (1.0-length(uvVertexSpace)) * vec3(0.5); - vec3 colorHeightSample = (gridDots(uv, vertexResolution) > 0.9 ? 1 : 0) * vec3(1,0,0); + vec3 colorHeightSample = (gridDots(uv, heightResolution) > 0.9 ? 1 : 0) * vec3(1,0,0); vec3 colorSum = colorUv + colorDistance + colorVertex + colorHeightSample; return vec4(0.5 * colorSum, 1); } diff --git a/modules/globebrowsing/shaders/tile.hglsl b/modules/globebrowsing/shaders/tile.hglsl index 8b6f7f703d..96c3a5703f 100644 --- a/modules/globebrowsing/shaders/tile.hglsl +++ b/modules/globebrowsing/shaders/tile.hglsl @@ -71,20 +71,31 @@ struct Tile { TileUvTransform uvTransform; }; -vec2 compensateSourceTextureSampling(vec2 startOffset, vec2 endOffset, const Tile tile, vec2 tileUV){ - vec2 offset = endOffset - startOffset; +vec2 compensateSourceTextureSampling(vec2 startOffset, vec2 sizeDiff, const Tile tile, vec2 tileUV){ ivec2 resolution = textureSize(tile.textureSampler, 0); - vec2 sourceSize = vec2(resolution) + offset; + vec2 sourceSize = vec2(resolution) + sizeDiff; vec2 currentSize = vec2(resolution); - vec2 oldToNewScale = currentSize / sourceSize; - tileUV = oldToNewScale * tileUV + startOffset / sourceSize; + vec2 sourceToCurrentSize = currentSize / sourceSize; + tileUV = sourceToCurrentSize * (tileUV - startOffset / sourceSize); + return tileUV; +} + + +vec2 compensateSourceTextureSampling2(vec2 startOffset, vec2 sizeDiff, const Tile tile, vec2 tileUV){ + vec2 pixelSizeUV = 1 / (textureSize(tile.textureSampler, 0) + sizeDiff); + + tileUV *= (1 - pixelSizeUV* 2); + tileUV += pixelSizeUV; + return tileUV; } vec4 getTexVal(const Tile tile, vec2 tileUV){ - vec2 samplePos = tile.uvTransform.uvOffset + tile.uvTransform.uvScale * tileUV; - vec4 texVal = texture(tile.textureSampler, samplePos); + tileUV = tile.uvTransform.uvOffset + tile.uvTransform.uvScale * tileUV; + tileUV = compensateSourceTextureSampling(vec2(-1), vec2(2), tile, tileUV); + //tileUV = compensateSourceTextureSampling2(vec2(-1), vec2(2), tile, tileUV); + vec4 texVal = texture(tile.textureSampler, tileUV); return texVal; } diff --git a/modules/globebrowsing/tile/tiledataset.cpp b/modules/globebrowsing/tile/tiledataset.cpp index 36231359b9..9ab981509b 100644 --- a/modules/globebrowsing/tile/tiledataset.cpp +++ b/modules/globebrowsing/tile/tiledataset.cpp @@ -182,12 +182,14 @@ namespace openspace { // Tile Dataset // ////////////////////////////////////////////////////////////////////////////////// + const glm::ivec2 TileDataset::tilePixelStartOffset = glm::ivec2(-1, -1); + const glm::ivec2 TileDataset::tilePixelSizeDifference = glm::ivec2(2, 2); + bool TileDataset::GdalHasBeenInitialized = false; TileDataset::TileDataset(const std::string& gdalDatasetDesc, int minimumPixelSize, bool doPreprocessing, GLuint dataType) - : _minimumPixelSize(minimumPixelSize) - , _doPreprocessing(doPreprocessing) + : _doPreprocessing(doPreprocessing) , _maxLevel(-1) { if (!GdalHasBeenInitialized) { @@ -284,16 +286,17 @@ namespace openspace { char* dataDestination = imageData + (i * _dataLayout.bytesPerDatum); - int pixelStartX = region.pixelStart.x * pixelSourceScale; - int pixelStartY = region.pixelStart.y * pixelSourceScale; - int pixelWidthX = region.numPixels.x * pixelSourceScale; - int pixelWidthY = region.numPixels.y * pixelSourceScale; + int pixelStartX = region.pixelStart.x * pixelSourceScale + tilePixelStartOffset.x; + int pixelStartY = region.pixelStart.y * pixelSourceScale + tilePixelStartOffset.y; + int pixelWidthX = region.numPixels.x * pixelSourceScale + tilePixelSizeDifference.x; + int pixelWidthY = region.numPixels.y * pixelSourceScale + tilePixelSizeDifference.y; + // Clamp to be inside dataset - pixelStartX = glm::max(pixelStartX, 0); - pixelStartY = glm::max(pixelStartY, 0); - pixelWidthX = glm::min(pixelStartX + pixelWidthX, rasterBand->GetXSize()) - pixelStartX; - pixelWidthY = glm::min(pixelStartY + pixelWidthY, rasterBand->GetYSize()) - pixelStartY; + //pixelStartX = glm::max(pixelStartX, 0); + //pixelStartY = glm::max(pixelStartY, 0); + //pixelWidthX = glm::min(pixelStartX + pixelWidthX, rasterBand->GetXSize()) - pixelStartX; + //pixelWidthY = glm::min(pixelStartY + pixelWidthY, rasterBand->GetYSize()) - pixelStartY; CPLErr err = rasterBand->RasterIO( GF_Read, diff --git a/modules/globebrowsing/tile/tiledataset.h b/modules/globebrowsing/tile/tiledataset.h index fcda6ff458..f8037ad6ee 100644 --- a/modules/globebrowsing/tile/tiledataset.h +++ b/modules/globebrowsing/tile/tiledataset.h @@ -102,6 +102,9 @@ namespace openspace { TileDepthTransform getDepthTransform() const; const TileDataLayout& getDataLayout() const; + const static glm::ivec2 tilePixelStartOffset; + const static glm::ivec2 tilePixelSizeDifference; + private: @@ -131,7 +134,6 @@ namespace openspace { static bool GdalHasBeenInitialized; - int _minimumPixelSize; int _maxLevel; double _tileLevelDifference;