Use datastructure TextureTile for tiles in shader.

This commit is contained in:
Kalle Bladin
2016-05-12 13:15:50 -04:00
parent ec8299da69
commit edf08d2b3d
9 changed files with 143 additions and 106 deletions

View File

@@ -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

View File

@@ -205,7 +205,7 @@ namespace openspace {
return _defaultTexture;
}
TileTextureDepthTransform TileProvider::depthTransform() {
TileDepthTransform TileProvider::depthTransform() {
return _depthTransform;
}

View File

@@ -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> texture;
TileTextureUvTransform uvTransform;
TileUvTransform uvTransform;
};
/**
@@ -76,7 +76,7 @@ namespace openspace {
std::shared_ptr<Texture> getOrStartFetchingTile(ChunkIndex chunkIndex);
std::shared_ptr<Texture> getDefaultTexture();
TileTextureDepthTransform depthTransform();
TileDepthTransform depthTransform();
void prerender();
@@ -116,7 +116,7 @@ namespace openspace {
std::shared_ptr<Texture> _defaultTexture;
int _tileLevelDifference;
TileTextureDepthTransform _depthTransform;
TileDepthTransform _depthTransform;
};
} // namespace openspace

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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