From 89d7b3ca8bcb6f4020fee72cb36c77a4b2ceda14 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Fri, 15 Apr 2016 17:17:31 -0400 Subject: [PATCH 1/2] Reading textures that are used for patch rendering. --- .../globebrowsing/rendering/patchrenderer.cpp | 14 ++++-- .../rendering/renderableglobe.cpp | 4 +- .../globebrowsing/rendering/renderableglobe.h | 6 +-- modules/globebrowsing/rendering/texturetile.h | 4 +- .../rendering/texturetileset.cpp | 44 ++++++++++++++----- .../globebrowsing/rendering/texturetileset.h | 15 ++++--- modules/globebrowsing/shaders/simple_fs.glsl | 4 +- 7 files changed, 62 insertions(+), 29 deletions(-) diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index 3f7aba2706..c539f953f3 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -31,9 +31,11 @@ #include #include - // ghoul includes #include +#include +#include + #define _USE_MATH_DEFINES #include @@ -113,13 +115,17 @@ namespace openspace { return; } } - + // Get the textures that should be used for rendering glm::ivec3 tileIndex = tileSet.getTileIndex(patch); LatLonPatch tilePatch = tileSet.getTilePositionAndScale(tileIndex); - TextureTile tile00 = tileSet.getTile(tileIndex); + std::shared_ptr tile00 = tileSet.getTile(tileIndex); - // Upload the tile to the GPU (a lot of caching things should be done) + // Bind and use the texture + ghoul::opengl::TextureUnit texUnit; + texUnit.activate(); + tile00->bind(); + _programObject->setUniform("textureSampler", texUnit); LatLon swCorner = patch.southWestCorner(); _programObject->setUniform("modelViewProjectionTransform", modelViewProjectionTransform); diff --git a/modules/globebrowsing/rendering/renderableglobe.cpp b/modules/globebrowsing/rendering/renderableglobe.cpp index c22f8d8490..2a7ab9a3f1 100644 --- a/modules/globebrowsing/rendering/renderableglobe.cpp +++ b/modules/globebrowsing/rendering/renderableglobe.cpp @@ -71,8 +71,8 @@ namespace openspace { // Mainly for debugging purposes @AA addProperty(_rotation); - addSwitchValue(std::shared_ptr(new ClipMapGlobe(dictionary)), 1e9); - //addSwitchValue(std::shared_ptr(new ChunkLodGlobe(dictionary)), 1e9); + //addSwitchValue(std::shared_ptr(new ClipMapGlobe(dictionary)), 1e9); + addSwitchValue(std::shared_ptr(new ChunkLodGlobe(dictionary)), 1e9); addSwitchValue(std::shared_ptr(new GlobeMesh(dictionary)), 1e10); } diff --git a/modules/globebrowsing/rendering/renderableglobe.h b/modules/globebrowsing/rendering/renderableglobe.h index 4be3a04846..26bce946d8 100644 --- a/modules/globebrowsing/rendering/renderableglobe.h +++ b/modules/globebrowsing/rendering/renderableglobe.h @@ -22,8 +22,8 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#ifndef __PLANET_H__ -#define __PLANET_H__ +#ifndef __RENDERABLEGLOBE_H__ +#define __RENDERABLEGLOBE_H__ #include @@ -66,4 +66,4 @@ private: } // namespace openspace -#endif // __PLANET_H__ \ No newline at end of file +#endif // __RENDERABLEGLOBE_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/texturetile.h b/modules/globebrowsing/rendering/texturetile.h index ab39afb409..05fa81a2fc 100644 --- a/modules/globebrowsing/rendering/texturetile.h +++ b/modules/globebrowsing/rendering/texturetile.h @@ -26,8 +26,10 @@ #define __TEXTURETILE_H__ #include +#include #include +#include namespace openspace { @@ -38,8 +40,6 @@ namespace openspace { ~TextureTile(); private: - glm::ivec2 positionIndex; - int level; }; } // namespace openspace diff --git a/modules/globebrowsing/rendering/texturetileset.cpp b/modules/globebrowsing/rendering/texturetileset.cpp index a3119c8570..c37876f07b 100644 --- a/modules/globebrowsing/rendering/texturetileset.cpp +++ b/modules/globebrowsing/rendering/texturetileset.cpp @@ -23,12 +23,32 @@ ****************************************************************************************/ #include +#include +#include +#include + +#include #include +namespace { + const std::string _loggerCat = "TextureTileSet"; +} + namespace openspace { TextureTileSet::TextureTileSet() - { + { + _testTexture = std::move(ghoul::io::TextureReader::ref().loadTexture(absPath("textures/earth_bluemarble.jpg"))); + if (_testTexture) { + LDEBUG("Loaded texture from '" << "textures/earth_bluemarble.jpg" << "'"); + _testTexture->uploadTexture(); + + // Textures of planets looks much smoother with AnisotropicMipMap rather than linear + // TODO: AnisotropicMipMap crashes on ATI cards ---abock + //_testTexture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); + _testTexture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); + } + } TextureTileSet::~TextureTileSet() @@ -38,31 +58,31 @@ namespace openspace { glm::ivec3 TextureTileSet::getTileIndex(LatLonPatch patch) { int level = log2(static_cast(glm::max( - sizeLevel0.lat / patch.halfSize.lat * 2, - sizeLevel0.lon / patch.halfSize.lon * 2))); - Vec2 TileSize = sizeLevel0.toLonLatVec2() / pow(2, level); - glm::ivec2 tileIndex = -(patch.northWestCorner().toLonLatVec2() + offsetLevel0.toLonLatVec2()) / TileSize; + _sizeLevel0.lat / patch.halfSize.lat * 2, + _sizeLevel0.lon / patch.halfSize.lon * 2))); + Vec2 TileSize = _sizeLevel0.toLonLatVec2() / pow(2, level); + glm::ivec2 tileIndex = -(patch.northWestCorner().toLonLatVec2() + _offsetLevel0.toLonLatVec2()) / TileSize; return glm::ivec3(tileIndex, level); } - TextureTile TextureTileSet::getTile(LatLonPatch patch) + std::shared_ptr TextureTileSet::getTile(LatLonPatch patch) { return getTile(getTileIndex(patch)); } - TextureTile TextureTileSet::getTile(glm::ivec3 tileIndex) + std::shared_ptr TextureTileSet::getTile(glm::ivec3 tileIndex) { - return TextureTile(); + return _testTexture; } LatLonPatch TextureTileSet::getTilePositionAndScale(glm::ivec3 tileIndex) { LatLon tileSize = LatLon( - sizeLevel0.lat / pow(2, tileIndex.z), - sizeLevel0.lon / pow(2, tileIndex.z)); + _sizeLevel0.lat / pow(2, tileIndex.z), + _sizeLevel0.lon / pow(2, tileIndex.z)); LatLon northWest = LatLon( - offsetLevel0.lat + tileIndex.y * tileSize.lat, - offsetLevel0.lon + tileIndex.x * tileSize.lon); + _offsetLevel0.lat + tileIndex.y * tileSize.lat, + _offsetLevel0.lon + tileIndex.x * tileSize.lon); return LatLonPatch( LatLon(northWest.lat + tileSize.lat / 2, northWest.lon + tileSize.lon / 2), diff --git a/modules/globebrowsing/rendering/texturetileset.h b/modules/globebrowsing/rendering/texturetileset.h index 2549a6fcf4..4774ae09de 100644 --- a/modules/globebrowsing/rendering/texturetileset.h +++ b/modules/globebrowsing/rendering/texturetileset.h @@ -26,12 +26,15 @@ #define __TEXTURETILESET_H__ #include +#include #include #include namespace openspace { + using namespace ghoul::opengl; + class TextureTileSet { public: @@ -41,14 +44,16 @@ namespace openspace { /// Returns the index of the tile at an appropriate level. /// Appropriate meaning that the tile should be at as high level as possible /// Without the tile being smaller than the patch in lat-lon space. - /// The tile needs to be at least as big as the patch. + /// The tile is at least as big as the patch. glm::ivec3 getTileIndex(LatLonPatch patch); - TextureTile getTile(LatLonPatch patch); - TextureTile getTile(glm::ivec3 tileIndex); + std::shared_ptr getTile(LatLonPatch patch); + std::shared_ptr getTile(glm::ivec3 tileIndex); LatLonPatch getTilePositionAndScale(glm::ivec3 tileIndex); private: - LatLon sizeLevel0; - LatLon offsetLevel0; + LatLon _sizeLevel0; + LatLon _offsetLevel0; + + std::shared_ptr _testTexture; }; } // namespace openspace diff --git a/modules/globebrowsing/shaders/simple_fs.glsl b/modules/globebrowsing/shaders/simple_fs.glsl index 13cb1c2e5b..503510e77d 100644 --- a/modules/globebrowsing/shaders/simple_fs.glsl +++ b/modules/globebrowsing/shaders/simple_fs.glsl @@ -35,6 +35,8 @@ uniform float time; uniform sampler2D texture1; uniform sampler2D nightTex; +uniform sampler2D textureSampler; + in vec4 vs_position; in vec2 vs_uv; @@ -45,7 +47,7 @@ in vec2 vs_uv; Fragment getFragment() { Fragment frag; - frag.color = vec4(fract(vs_uv * 1), 0.4,1); + frag.color = texture2D(textureSampler, vs_uv);// vec4(fract(vs_uv * 1), 0.4,1); frag.depth = pscDepth(vs_position); return frag; From 7cdf991e765bfdbe5785624d114412f12ac1d8bd Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Fri, 15 Apr 2016 17:19:35 -0400 Subject: [PATCH 2/2] Add use of textures to clipmap rendering. --- modules/globebrowsing/rendering/patchrenderer.cpp | 12 ++++++++++++ modules/globebrowsing/rendering/renderableglobe.cpp | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index c539f953f3..4b55f7bd3a 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -193,6 +193,18 @@ namespace openspace { //LDEBUG("intSnapCoord = [ " << intSnapCoord.x << " , " << intSnapCoord.y << " ]"); //LDEBUG("contraction = [ " << contraction.x << " , " << contraction.y << " ]"); + + // Get the textures that should be used for rendering + glm::ivec3 tileIndex = tileSet.getTileIndex(patch); + LatLonPatch tilePatch = tileSet.getTilePositionAndScale(tileIndex); + std::shared_ptr tile00 = tileSet.getTile(tileIndex); + + // Bind and use the texture + ghoul::opengl::TextureUnit texUnit; + texUnit.activate(); + tile00->bind(); + _programObject->setUniform("textureSampler", texUnit); + _programObject->setUniform("modelViewProjectionTransform", data.camera.projectionMatrix() * viewTransform * modelTransform); _programObject->setUniform("minLatLon", vec2(swCorner.toLonLatVec2())); _programObject->setUniform("lonLatScalingFactor", 2.0f * vec2(patch.halfSize.toLonLatVec2())); diff --git a/modules/globebrowsing/rendering/renderableglobe.cpp b/modules/globebrowsing/rendering/renderableglobe.cpp index 2a7ab9a3f1..c22f8d8490 100644 --- a/modules/globebrowsing/rendering/renderableglobe.cpp +++ b/modules/globebrowsing/rendering/renderableglobe.cpp @@ -71,8 +71,8 @@ namespace openspace { // Mainly for debugging purposes @AA addProperty(_rotation); - //addSwitchValue(std::shared_ptr(new ClipMapGlobe(dictionary)), 1e9); - addSwitchValue(std::shared_ptr(new ChunkLodGlobe(dictionary)), 1e9); + addSwitchValue(std::shared_ptr(new ClipMapGlobe(dictionary)), 1e9); + //addSwitchValue(std::shared_ptr(new ChunkLodGlobe(dictionary)), 1e9); addSwitchValue(std::shared_ptr(new GlobeMesh(dictionary)), 1e10); }