From 4e95aaab8250a0736e605b2ef61fa0ba0e521eff Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 12 May 2016 11:06:49 -0400 Subject: [PATCH] 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);