From 850ccb6d3c79f27fe3879d77f14f220df87c0814 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 25 Aug 2016 00:00:22 -0400 Subject: [PATCH] Update rendering of shadow texture and water mask in RenderableGlobe. --- modules/globebrowsing/chunk/chunkrenderer.cpp | 22 ++++++++++++--- .../shaders/globalchunkedlodpatch_vs.glsl | 2 ++ .../shaders/localchunkedlodpatch_vs.glsl | 3 +- .../shaders/texturetilemapping.hglsl | 28 ++++++++++++++----- .../globebrowsing/shaders/tilefragcolor.hglsl | 11 ++++++-- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/modules/globebrowsing/chunk/chunkrenderer.cpp b/modules/globebrowsing/chunk/chunkrenderer.cpp index 5ff49de520..4dedcf3aaa 100644 --- a/modules/globebrowsing/chunk/chunkrenderer.cpp +++ b/modules/globebrowsing/chunk/chunkrenderer.cpp @@ -381,11 +381,17 @@ namespace openspace { programObject->setUniform("lonLatScalingFactor", vec2(patchSize.toLonLatVec2())); programObject->setUniform("radiiSquared", vec3(ellipsoid.radiiSquared())); - if (_tileProviderManager->getTileProviderGroup(LayeredTextures::NightTextures).getActiveTileProviders().size() > 0) { + if (_tileProviderManager->getTileProviderGroup( + LayeredTextures::NightTextures).getActiveTileProviders().size() > 0) { + glm::vec3 directionToSunWorldSpace = + glm::normalize(-data.modelTransform.translation); + glm::vec3 directionToSunCameraSpace = + (viewTransform * glm::dvec4(directionToSunWorldSpace, 0)).xyz(); + data.modelTransform.translation; programObject->setUniform("modelViewTransform", modelViewTransform); + programObject->setUniform("lightDirectionCameraSpace", -directionToSunCameraSpace); } - // OpenGL rendering settings glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); @@ -430,8 +436,6 @@ namespace openspace { } // Calculate other uniform variables needed for rendering - - // TODO : Model transform should be fetched as a matrix directly. dmat4 modelTransform = chunk.owner()->modelTransform(); dmat4 viewTransform = data.camera.combinedViewMatrix(); dmat4 modelViewTransform = viewTransform * modelTransform; @@ -454,6 +458,16 @@ namespace openspace { programObject->setUniform("patchNormalCameraSpace", patchNormalCameraSpace); programObject->setUniform("projectionTransform", data.camera.projectionMatrix()); + if (_tileProviderManager->getTileProviderGroup( + LayeredTextures::NightTextures).getActiveTileProviders().size() > 0) { + glm::vec3 directionToSunWorldSpace = + glm::normalize(-data.modelTransform.translation); + glm::vec3 directionToSunCameraSpace = + (viewTransform * glm::dvec4(directionToSunWorldSpace, 0)).xyz(); + data.modelTransform.translation; + programObject->setUniform("lightDirectionCameraSpace", -directionToSunCameraSpace); + } + // OpenGL rendering settings glEnable(GL_DEPTH_TEST); diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl index 7e63750f93..e30636cd03 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl @@ -44,6 +44,7 @@ out vec2 fs_uv; out vec4 fs_position; out vec3 ellipsoidNormalCameraSpace; out LevelWeights levelWeights; +out vec3 positionCameraSpace; PositionNormalPair globalInterpolation() { vec2 lonLatInput; @@ -77,4 +78,5 @@ void main() { fs_position = z_normalization(positionClippingSpace); gl_Position = fs_position; ellipsoidNormalCameraSpace = mat3(modelViewTransform) * pair.normal; + positionCameraSpace = vec3(modelViewTransform * vec4(pair.position, 1)); } \ No newline at end of file diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl index 748f60ae2b..14e26c22a2 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl @@ -46,7 +46,7 @@ out vec2 fs_uv; out vec4 fs_position; out vec3 ellipsoidNormalCameraSpace; out LevelWeights levelWeights; - +out vec3 positionCameraSpace; vec3 bilinearInterpolation(vec2 uv) { vec3 p0 = (1 - uv.x) * p00 + uv.x * p10; @@ -84,4 +84,5 @@ void main() { fs_position = z_normalization(positionClippingSpace); gl_Position = fs_position; ellipsoidNormalCameraSpace = patchNormalCameraSpace; + positionCameraSpace = p; } \ No newline at end of file diff --git a/modules/globebrowsing/shaders/texturetilemapping.hglsl b/modules/globebrowsing/shaders/texturetilemapping.hglsl index a7436970fb..06dd98309a 100644 --- a/modules/globebrowsing/shaders/texturetilemapping.hglsl +++ b/modules/globebrowsing/shaders/texturetilemapping.hglsl @@ -259,10 +259,8 @@ vec4 calculateNight( 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); + const vec3 ellipsoidNormalCameraSpace, + const vec3 lightDirectionCameraSpace) { vec4 nightColor = vec4(0,0,0,0); @@ -283,8 +281,10 @@ vec4 calculateNight( } #endfor + float cosineFactor = clamp(dot(-lightDirectionCameraSpace, ellipsoidNormalCameraSpace) * 3, 0, 1); + // Blend night color with base color - vec4 color = vec4(cosineFactor * vec3(currentColor) + (1 - cosineFactor) * vec3(nightColor), currentColor.a); + vec4 color = vec4(cosineFactor * currentColor.xyz + (1 - cosineFactor) * vec3(nightColor * nightColor * 0.7), currentColor.a); return color; } @@ -371,7 +371,10 @@ vec4 calculateWater( LevelWeights levelWeights, const Tile waterTiles[NUMLAYERS_WATERMASK], const Tile waterTilesParent1[NUMLAYERS_WATERMASK], - const Tile waterTilesParent2[NUMLAYERS_WATERMASK]) { + const Tile waterTilesParent2[NUMLAYERS_WATERMASK], + const vec3 ellipsoidNormalCameraSpace, + const vec3 lightDirectionCameraSpace, + const vec3 positionCameraSpace) { vec4 waterColor = vec4(0,0,0,0); @@ -392,7 +395,18 @@ vec4 calculateWater( } #endfor - return blendOver(currentColor, waterColor); + vec3 directionToFragmentCameraSpace = normalize(positionCameraSpace - vec3(0, 0, 0)); + vec3 reflectionDirectionCameraSpace = reflect(lightDirectionCameraSpace, ellipsoidNormalCameraSpace); + float cosineFactor = clamp(dot(-reflectionDirectionCameraSpace, directionToFragmentCameraSpace), 0, 1); + cosineFactor = pow(cosineFactor, 30); + + vec3 specularColor = vec3(1, 1, 1); + float specularIntensity = 0.4; + + vec3 specularTotal = specularColor * cosineFactor * specularIntensity * waterColor.a; + + //return blendOver(currentColor, waterColor); + return currentColor + vec4(specularTotal, 1); } #endif // TEXTURETILEMAPPING_HGLSL \ No newline at end of file diff --git a/modules/globebrowsing/shaders/tilefragcolor.hglsl b/modules/globebrowsing/shaders/tilefragcolor.hglsl index c7dc7f90c1..884b7b9df0 100644 --- a/modules/globebrowsing/shaders/tilefragcolor.hglsl +++ b/modules/globebrowsing/shaders/tilefragcolor.hglsl @@ -43,6 +43,7 @@ uniform Tile ColorTexturesParent2[NUMLAYERS_COLORTEXTURE]; uniform Tile NightTextures[NUMLAYERS_NIGHTTEXTURE]; uniform Tile NightTexturesParent1[NUMLAYERS_NIGHTTEXTURE]; uniform Tile NightTexturesParent2[NUMLAYERS_NIGHTTEXTURE]; +uniform vec3 lightDirectionCameraSpace; #endif // USE_NIGHTTEXTURE #if USE_OVERLAY @@ -75,6 +76,8 @@ uniform vec2 vertexResolution; in vec4 fs_position; in vec2 fs_uv; in vec3 ellipsoidNormalCameraSpace; +in vec3 positionCameraSpace; + // 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 @@ -170,7 +173,10 @@ vec4 getTileFragColor(){ levelWeights, WaterMasks, WaterMasksParent1, - WaterMasksParent2); + WaterMasksParent2, + ellipsoidNormalCameraSpace, + lightDirectionCameraSpace, + positionCameraSpace); #endif // USE_WATERMASK @@ -184,7 +190,8 @@ vec4 getTileFragColor(){ NightTextures, NightTexturesParent1, NightTexturesParent2, - ellipsoidNormalCameraSpace); + ellipsoidNormalCameraSpace, + lightDirectionCameraSpace); #endif // USE_NIGHTTEXTURE