diff options
author | r.kuznetsov <r.kuznetsov@corp.mail.ru> | 2016-08-16 14:28:05 +0300 |
---|---|---|
committer | r.kuznetsov <r.kuznetsov@corp.mail.ru> | 2016-10-05 13:52:06 +0300 |
commit | 6dc03b1114362f2f06244422c88a1d159d827f5b (patch) | |
tree | 92a829f91254b87fb0e56204ccf7a49138f834d0 /drape | |
parent | ea20412b1da6b31a2a69181ccecb2d7b9006b6a2 (diff) |
Added traffic renderer
Diffstat (limited to 'drape')
-rw-r--r-- | drape/drape.pro | 2 | ||||
-rw-r--r-- | drape/drape_common.pri | 2 | ||||
-rw-r--r-- | drape/shaders/shader_index.txt | 1 | ||||
-rw-r--r-- | drape/shaders/traffic_fragment_shader.fsh | 19 | ||||
-rw-r--r-- | drape/shaders/traffic_vertex_shader.vsh | 42 | ||||
-rw-r--r-- | drape/static_texture.cpp | 120 | ||||
-rw-r--r-- | drape/static_texture.hpp | 34 | ||||
-rw-r--r-- | drape/texture.hpp | 2 | ||||
-rw-r--r-- | drape/texture_manager.cpp | 15 | ||||
-rw-r--r-- | drape/texture_manager.hpp | 3 |
10 files changed, 239 insertions, 1 deletions
diff --git a/drape/drape.pro b/drape/drape.pro index 0253766c1f..e13cad0f68 100644 --- a/drape/drape.pro +++ b/drape/drape.pro @@ -54,6 +54,8 @@ OTHER_FILES += \ shaders/texturing_vertex_shader.vsh \ shaders/trackpoint_vertex_shader.vsh \ shaders/trackpoint_fragment_shader.fsh \ + shaders/traffic_fragment_shader.fsh \ + shaders/traffic_vertex_shader.vsh \ shaders/transparent_layer_fragment_shader.fsh \ shaders/transparent_layer_vertex_shader.vsh \ shaders/user_mark.vsh \ diff --git a/drape/drape_common.pri b/drape/drape_common.pri index f8cc8fa8ff..132d156d30 100644 --- a/drape/drape_common.pri +++ b/drape/drape_common.pri @@ -36,6 +36,7 @@ SOURCES += \ $$DRAPE_DIR/render_bucket.cpp \ $$DRAPE_DIR/shader.cpp \ $$DRAPE_DIR/shader_def.cpp \ + $$DRAPE_DIR/static_texture.cpp \ $$DRAPE_DIR/stipple_pen_resource.cpp \ $$DRAPE_DIR/support_manager.cpp \ $$DRAPE_DIR/symbols_texture.cpp \ @@ -93,6 +94,7 @@ HEADERS += \ $$DRAPE_DIR/render_bucket.hpp \ $$DRAPE_DIR/shader.hpp \ $$DRAPE_DIR/shader_def.hpp \ + $$DRAPE_DIR/static_texture.hpp \ $$DRAPE_DIR/stipple_pen_resource.hpp \ $$DRAPE_DIR/support_manager.hpp \ $$DRAPE_DIR/symbols_texture.hpp \ diff --git a/drape/shaders/shader_index.txt b/drape/shaders/shader_index.txt index 62b1ad476e..73fc39a3ce 100644 --- a/drape/shaders/shader_index.txt +++ b/drape/shaders/shader_index.txt @@ -29,3 +29,4 @@ MASKED_TEXTURING_BILLBOARD_PROGRAM masked_texturing_billboard_vertex_shader.vsh TEXT_OUTLINED_BILLBOARD_PROGRAM text_outlined_billboard_vertex_shader.vsh text_fragment_shader.fsh TEXT_BILLBOARD_PROGRAM text_billboard_vertex_shader.vsh text_fragment_shader.fsh BOOKMARK_BILLBOARD_PROGRAM user_mark_billboard.vsh texturing_fragment_shader.fsh +TRAFFIC_PROGRAM traffic_vertex_shader.vsh traffic_fragment_shader.fsh diff --git a/drape/shaders/traffic_fragment_shader.fsh b/drape/shaders/traffic_fragment_shader.fsh new file mode 100644 index 0000000000..287ea0b076 --- /dev/null +++ b/drape/shaders/traffic_fragment_shader.fsh @@ -0,0 +1,19 @@ +varying vec2 v_colorTexCoord; +varying vec2 v_maskTexCoord; +varying float v_halfLength; + +uniform sampler2D u_colorTex; +uniform sampler2D u_maskTex; +uniform float u_opacity; + +const float kAntialiasingThreshold = 0.92; + +void main(void) +{ + vec4 color = texture2D(u_colorTex, v_colorTexCoord); + vec4 mask = texture2D(u_maskTex, v_maskTexCoord); + color.a = color.a * u_opacity * (1.0 - smoothstep(kAntialiasingThreshold, 1.0, abs(v_halfLength))); + color.rgb = mix(color.rgb, mask.rgb, mask.a); + + gl_FragColor = color; +} diff --git a/drape/shaders/traffic_vertex_shader.vsh b/drape/shaders/traffic_vertex_shader.vsh new file mode 100644 index 0000000000..e1c735b39c --- /dev/null +++ b/drape/shaders/traffic_vertex_shader.vsh @@ -0,0 +1,42 @@ +attribute vec3 a_position; +attribute vec4 a_normal; +attribute vec2 a_colorTexCoord; + +uniform mat4 modelView; +uniform mat4 projection; +uniform mat4 pivotTransform; + +uniform vec4 u_trafficParams; + +varying vec2 v_colorTexCoord; +varying vec2 v_maskTexCoord; +varying float v_halfLength; + +const float kShapeCoordScalar = 1000.0; + +void main(void) +{ + vec2 normal = a_normal.xy; + float halfWidth = length(normal); + vec2 transformedAxisPos = (vec4(a_position.xy, 0.0, 1.0) * modelView).xy; + if (halfWidth != 0.0) + { + vec2 norm = normal * u_trafficParams.x; + if (a_normal.z < 0.0) norm = normal * u_trafficParams.y; + halfWidth = length(norm); + + vec4 glbShiftPos = vec4(a_position.xy + norm, 0.0, 1.0); + vec2 shiftPos = (glbShiftPos * modelView).xy; + transformedAxisPos = transformedAxisPos + normalize(shiftPos - transformedAxisPos) * halfWidth; + } + + float uOffset = length(vec4(kShapeCoordScalar, 0, 0, 0) * modelView) * a_normal.w; + v_colorTexCoord = a_colorTexCoord; + v_maskTexCoord = vec2(uOffset * u_trafficParams.z, 0.5 * a_normal.z + 0.5) * u_trafficParams.w; + v_halfLength = a_normal.z; + vec4 pos = vec4(transformedAxisPos, a_position.z, 1.0) * projection; + float w = pos.w; + pos.xyw = (pivotTransform * vec4(pos.xy, 0.0, w)).xyw; + pos.z *= pos.w / w; + gl_Position = pos; +} diff --git a/drape/static_texture.cpp b/drape/static_texture.cpp new file mode 100644 index 0000000000..782cbf7649 --- /dev/null +++ b/drape/static_texture.cpp @@ -0,0 +1,120 @@ +#include "drape/static_texture.hpp" +#include "3party/stb_image/stb_image.h" + +#include "indexer/map_style_reader.hpp" + +#include "platform/platform.hpp" + +#include "coding/reader.hpp" +#include "coding/parse_xml.hpp" + +#include "base/string_utils.hpp" + +namespace dp +{ + +namespace +{ + +using TLoadingCompletion = function<void(unsigned char *, uint32_t, uint32_t)>; +using TLoadingFailure = function<void(string const &)>; + +void LoadData(string const & textureName, string const & skinPathName, + TLoadingCompletion const & completionHandler, + TLoadingFailure const & failureHandler) +{ + ASSERT(completionHandler != nullptr, ()); + ASSERT(failureHandler != nullptr, ()); + + vector<unsigned char> rawData; + try + { + ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(textureName + ".png", skinPathName); + size_t const size = reader.Size(); + rawData.resize(size); + reader.Read(0, &rawData[0], size); + } + catch (RootException & e) + { + failureHandler(e.what()); + return; + } + + int w, h, bpp; + unsigned char * data = stbi_png_load_from_memory(&rawData[0], rawData.size(), &w, &h, &bpp, 0); + ASSERT_EQUAL(bpp, 4, ("Incorrect texture format")); + completionHandler(data, w, h); + + stbi_image_free(data); +} + +class StaticResourceInfo : public Texture::ResourceInfo +{ +public: + StaticResourceInfo() : Texture::ResourceInfo(m2::RectF(0.0f, 0.0f, 1.0f, 1.0f)) {} + virtual ~StaticResourceInfo(){} + + Texture::ResourceType GetType() const override { return Texture::Static; } +}; + +} // namespace + +StaticTexture::StaticTexture(string const & textureName, string const & skinPathName, + ref_ptr<HWTextureAllocator> allocator) + : m_textureName(textureName) + , m_info(make_unique_dp<StaticResourceInfo>()) +{ + Load(skinPathName, allocator); +} + +void StaticTexture::Load(string const & skinPathName, ref_ptr<HWTextureAllocator> allocator) +{ + auto completionHandler = [this, &allocator](unsigned char * data, uint32_t width, uint32_t height) + { + Texture::Params p; + p.m_allocator = allocator; + p.m_format = dp::RGBA8; + p.m_width = width; + p.m_height = height; + p.m_wrapSMode = gl_const::GLRepeate; + p.m_wrapTMode = gl_const::GLRepeate; + + Create(p, make_ref(data)); + }; + + auto failureHandler = [this](string const & reason) + { + LOG(LERROR, (reason)); + Fail(); + }; + + LoadData(m_textureName, skinPathName, completionHandler, failureHandler); +} + +void StaticTexture::Invalidate(string const & skinPathName, ref_ptr<HWTextureAllocator> allocator) +{ + Destroy(); + Load(skinPathName, allocator); +} + +ref_ptr<Texture::ResourceInfo> StaticTexture::FindResource(Texture::Key const & key, bool & newResource) +{ + newResource = false; + if (key.GetType() != Texture::Static) + return nullptr; + return make_ref(m_info); +} + +void StaticTexture::Fail() +{ + int32_t alfaTexture = 0; + Texture::Params p; + p.m_allocator = GetDefaultAllocator(); + p.m_format = dp::RGBA8; + p.m_width = 1; + p.m_height = 1; + + Create(p, make_ref(&alfaTexture)); +} + +} // namespace dp diff --git a/drape/static_texture.hpp b/drape/static_texture.hpp new file mode 100644 index 0000000000..9385faf917 --- /dev/null +++ b/drape/static_texture.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "drape/texture.hpp" + +#include "std/string.hpp" + +namespace dp +{ + +class StaticTexture : public Texture +{ +public: + class StaticKey : public Key + { + public: + ResourceType GetType() const override { return ResourceType::Static; } + }; + + StaticTexture(string const & textureName, string const & skinPathName, + ref_ptr<HWTextureAllocator> allocator); + + ref_ptr<ResourceInfo> FindResource(Key const & key, bool & newResource) override; + + void Invalidate(string const & skinPathName, ref_ptr<HWTextureAllocator> allocator); + +private: + void Fail(); + void Load(string const & skinPathName, ref_ptr<HWTextureAllocator> allocator); + + string m_textureName; + drape_ptr<Texture::ResourceInfo> m_info; +}; + +} // namespace dp diff --git a/drape/texture.hpp b/drape/texture.hpp index 83d9e26c1f..b102f94598 100644 --- a/drape/texture.hpp +++ b/drape/texture.hpp @@ -22,7 +22,7 @@ public: Glyph, StipplePen, Color, - UniformValue + Static }; class Key diff --git a/drape/texture_manager.cpp b/drape/texture_manager.cpp index 9fd92dd856..e1cfb9948f 100644 --- a/drape/texture_manager.cpp +++ b/drape/texture_manager.cpp @@ -1,6 +1,7 @@ #include "drape/texture_manager.hpp" #include "drape/symbols_texture.hpp" #include "drape/font_texture.hpp" +#include "drape/static_texture.hpp" #include "drape/stipple_pen_resource.hpp" #include "drape/texture_of_colors.hpp" #include "drape/glfunctions.hpp" @@ -210,6 +211,8 @@ void TextureManager::Release() m_stipplePenTexture.reset(); m_colorTexture.reset(); + m_trafficArrowTexture.reset(); + m_glyphTextures.clear(); m_glyphManager.reset(); @@ -379,6 +382,9 @@ void TextureManager::Init(Params const & params) m_symbolTexture = make_unique_dp<SymbolsTexture>(params.m_resPostfix, make_ref(m_textureAllocator)); + m_trafficArrowTexture = make_unique_dp<StaticTexture>("traffic-arrow", params.m_resPostfix, + make_ref(m_textureAllocator)); + // initialize patterns buffer_vector<buffer_vector<uint8_t, 8>, 64> patterns; double const visualScale = params.m_visualScale; @@ -450,6 +456,10 @@ void TextureManager::Invalidate(string const & resPostfix) ASSERT(m_symbolTexture != nullptr, ()); ref_ptr<SymbolsTexture> symbolsTexture = make_ref(m_symbolTexture); symbolsTexture->Invalidate(resPostfix, make_ref(m_textureAllocator)); + + ASSERT(m_trafficArrowTexture != nullptr, ()); + ref_ptr<StaticTexture> staticTexture = make_ref(m_trafficArrowTexture); + staticTexture->Invalidate(resPostfix, make_ref(m_textureAllocator)); } void TextureManager::GetSymbolRegion(string const & symbolName, SymbolRegion & region) @@ -507,6 +517,11 @@ ref_ptr<Texture> TextureManager::GetSymbolsTexture() const return make_ref(m_symbolTexture); } +ref_ptr<Texture> TextureManager::GetTrafficArrowTexture() const +{ + return make_ref(m_trafficArrowTexture); +} + constexpr size_t TextureManager::GetInvalidGlyphGroup() { return kInvalidGlyphGroup; diff --git a/drape/texture_manager.hpp b/drape/texture_manager.hpp index f25a9bbb5d..f820e6729f 100644 --- a/drape/texture_manager.hpp +++ b/drape/texture_manager.hpp @@ -102,6 +102,7 @@ public: bool AreGlyphsReady(strings::UniString const & str) const; ref_ptr<Texture> GetSymbolsTexture() const; + ref_ptr<Texture> GetTrafficArrowTexture() const; private: struct GlyphGroup @@ -239,6 +240,8 @@ private: drape_ptr<Texture> m_colorTexture; list<drape_ptr<Texture>> m_glyphTextures; + drape_ptr<Texture> m_trafficArrowTexture; + drape_ptr<GlyphManager> m_glyphManager; drape_ptr<HWTextureAllocator> m_textureAllocator; |