1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include "drape_frontend/area_shape.hpp"
#include "drape_frontend/shader_def.hpp"
#include "drape/attribute_provider.hpp"
#include "drape/batcher.hpp"
#include "drape/glstate.hpp"
#include "drape/texture_manager.hpp"
#include "drape/utils/vertex_decl.hpp"
#include "indexer/map_style_reader.hpp"
#include "base/buffer_vector.hpp"
#include "base/logging.hpp"
#include "std/algorithm.hpp"
namespace df
{
AreaShape::AreaShape(vector<m2::PointD> && triangleList, BuildingOutline && buildingOutline,
AreaViewParams const & params)
: m_vertexes(move(triangleList))
, m_buildingOutline(move(buildingOutline))
, m_params(params)
{}
void AreaShape::Draw(ref_ptr<dp::Batcher> batcher, ref_ptr<dp::TextureManager> textures) const
{
dp::TextureManager::ColorRegion region;
textures->GetColorRegion(m_params.m_color, region);
m2::PointD const colorUv = region.GetTexRect().Center();
m2::PointD outlineUv(0.0, 0.0);
if (m_buildingOutline.m_generateOutline)
{
dp::TextureManager::ColorRegion outlineRegion;
textures->GetColorRegion(m_params.m_outlineColor, outlineRegion);
ASSERT_EQUAL(region.GetTexture(), outlineRegion.GetTexture(), ());
outlineUv = outlineRegion.GetTexRect().Center();
}
if (m_params.m_is3D)
DrawArea3D(batcher, colorUv, outlineUv, region.GetTexture());
else if (m_params.m_hatching)
DrawHatchingArea(batcher, colorUv, region.GetTexture(), textures->GetHatchingTexture());
else
DrawArea(batcher, colorUv, outlineUv, region.GetTexture());
}
void AreaShape::DrawArea(ref_ptr<dp::Batcher> batcher, m2::PointD const & colorUv, m2::PointD const & outlineUv,
ref_ptr<dp::Texture> texture) const
{
glsl::vec2 const uv = glsl::ToVec2(colorUv);
buffer_vector<gpu::AreaVertex, 128> vertexes;
vertexes.resize(m_vertexes.size());
transform(m_vertexes.begin(), m_vertexes.end(), vertexes.begin(), [&uv, this](m2::PointF const & vertex)
{
return gpu::AreaVertex(glsl::vec3(glsl::ToVec2(ConvertToLocal(vertex, m_params.m_tileCenter, kShapeCoordScalar)),
m_params.m_depth), uv);
});
dp::GLState state(gpu::AREA_PROGRAM, dp::GLState::GeometryLayer);
state.SetColorTexture(texture);
dp::AttributeProvider provider(1, static_cast<uint32_t>(vertexes.size()));
provider.InitStream(0, gpu::AreaVertex::GetBindingInfo(), make_ref(vertexes.data()));
batcher->InsertTriangleList(state, make_ref(&provider));
// Generate outline.
if (m_buildingOutline.m_generateOutline && !m_buildingOutline.m_indices.empty())
{
glsl::vec2 const ouv = glsl::ToVec2(outlineUv);
vector<gpu::AreaVertex> vertices;
vertices.reserve(m_buildingOutline.m_vertices.size());
for (size_t i = 0; i < m_buildingOutline.m_vertices.size(); i++)
{
glsl::vec2 const pos = glsl::ToVec2(ConvertToLocal(m_buildingOutline.m_vertices[i],
m_params.m_tileCenter, kShapeCoordScalar));
vertices.emplace_back(gpu::AreaVertex(glsl::vec3(pos, m_params.m_depth), ouv));
}
dp::GLState outlineState(gpu::AREA_OUTLINE_PROGRAM, dp::GLState::GeometryLayer);
outlineState.SetColorTexture(texture);
outlineState.SetDrawAsLine(true);
dp::AttributeProvider outlineProvider(1, static_cast<uint32_t>(vertices.size()));
outlineProvider.InitStream(0, gpu::AreaVertex::GetBindingInfo(), make_ref(vertices.data()));
batcher->InsertLineRaw(outlineState, make_ref(&outlineProvider), m_buildingOutline.m_indices);
}
}
void AreaShape::DrawHatchingArea(ref_ptr<dp::Batcher> batcher, m2::PointD const & colorUv,
ref_ptr<dp::Texture> texture, ref_ptr<dp::Texture> hatchingTexture) const
{
glsl::vec2 const uv = glsl::ToVec2(colorUv);
m2::RectD bbox;
for (auto const & v : m_vertexes)
bbox.Add(v);
double const maxU = bbox.SizeX() * m_params.m_baseGtoPScale / hatchingTexture->GetWidth();
double const maxV = bbox.SizeY() * m_params.m_baseGtoPScale / hatchingTexture->GetHeight();
buffer_vector<gpu::HatchingAreaVertex, 128> vertexes;
vertexes.resize(m_vertexes.size());
for (size_t i = 0; i < m_vertexes.size(); ++i)
{
vertexes[i].m_position = glsl::vec3(glsl::ToVec2(ConvertToLocal(m_vertexes[i], m_params.m_tileCenter,
kShapeCoordScalar)), m_params.m_depth);
vertexes[i].m_colorTexCoord = uv;
vertexes[i].m_maskTexCoord.x = static_cast<float>(maxU * (m_vertexes[i].x - bbox.minX()) / bbox.SizeX());
vertexes[i].m_maskTexCoord.y = static_cast<float>(maxV * (m_vertexes[i].y - bbox.minY()) / bbox.SizeY());
}
dp::GLState state(gpu::HATCHING_AREA_PROGRAM, dp::GLState::GeometryLayer);
state.SetColorTexture(texture);
state.SetMaskTexture(hatchingTexture);
state.SetTextureFilter(gl_const::GLLinear);
dp::AttributeProvider provider(1, static_cast<uint32_t>(vertexes.size()));
provider.InitStream(0, gpu::HatchingAreaVertex::GetBindingInfo(), make_ref(vertexes.data()));
batcher->InsertTriangleList(state, make_ref(&provider));
}
void AreaShape::DrawArea3D(ref_ptr<dp::Batcher> batcher, m2::PointD const & colorUv, m2::PointD const & outlineUv,
ref_ptr<dp::Texture> texture) const
{
ASSERT(!m_buildingOutline.m_indices.empty(), ());
ASSERT(!m_buildingOutline.m_normals.empty(), ());
glsl::vec2 const uv = glsl::ToVec2(colorUv);
vector<gpu::Area3dVertex> vertexes;
vertexes.reserve(m_vertexes.size() + m_buildingOutline.m_normals.size() * 6);
for (size_t i = 0; i < m_buildingOutline.m_normals.size(); i++)
{
int const startIndex = m_buildingOutline.m_indices[i * 2];
int const endIndex = m_buildingOutline.m_indices[i * 2 + 1];
glsl::vec2 const startPt = glsl::ToVec2(ConvertToLocal(m_buildingOutline.m_vertices[startIndex],
m_params.m_tileCenter, kShapeCoordScalar));
glsl::vec2 const endPt = glsl::ToVec2(ConvertToLocal(m_buildingOutline.m_vertices[endIndex],
m_params.m_tileCenter, kShapeCoordScalar));
glsl::vec3 normal(glsl::ToVec2(m_buildingOutline.m_normals[i]), 0.0f);
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(startPt, -m_params.m_minPosZ), normal, uv));
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(endPt, -m_params.m_minPosZ), normal, uv));
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(startPt, -m_params.m_posZ), normal, uv));
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(startPt, -m_params.m_posZ), normal, uv));
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(endPt, -m_params.m_minPosZ), normal, uv));
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(endPt, -m_params.m_posZ), normal, uv));
}
glsl::vec3 const normal(0.0f, 0.0f, -1.0f);
for (auto const & vertex : m_vertexes)
{
glsl::vec2 const pt = glsl::ToVec2(ConvertToLocal(vertex, m_params.m_tileCenter, kShapeCoordScalar));
vertexes.emplace_back(gpu::Area3dVertex(glsl::vec3(pt, -m_params.m_posZ), normal, uv));
}
dp::GLState state(gpu::AREA_3D_PROGRAM, dp::GLState::GeometryLayer);
state.SetColorTexture(texture);
state.SetBlending(dp::Blending(false /* isEnabled */));
dp::AttributeProvider provider(1, static_cast<uint32_t>(vertexes.size()));
provider.InitStream(0, gpu::Area3dVertex::GetBindingInfo(), make_ref(vertexes.data()));
batcher->InsertTriangleList(state, make_ref(&provider));
// Generate outline.
if (m_buildingOutline.m_generateOutline)
{
glsl::vec2 const ouv = glsl::ToVec2(outlineUv);
dp::GLState outlineState(gpu::AREA_3D_OUTLINE_PROGRAM, dp::GLState::GeometryLayer);
outlineState.SetColorTexture(texture);
outlineState.SetBlending(dp::Blending(false /* isEnabled */));
outlineState.SetDrawAsLine(true);
vector<gpu::AreaVertex> vertices;
vertices.reserve(m_buildingOutline.m_vertices.size());
for (size_t i = 0; i < m_buildingOutline.m_vertices.size(); i++)
{
glsl::vec2 const pos = glsl::ToVec2(ConvertToLocal(m_buildingOutline.m_vertices[i],
m_params.m_tileCenter, kShapeCoordScalar));
vertices.emplace_back(gpu::AreaVertex(glsl::vec3(pos, -m_params.m_posZ), ouv));
}
dp::AttributeProvider outlineProvider(1, static_cast<uint32_t>(vertices.size()));
outlineProvider.InitStream(0, gpu::AreaVertex::GetBindingInfo(), make_ref(vertices.data()));
batcher->InsertLineRaw(outlineState, make_ref(&outlineProvider), m_buildingOutline.m_indices);
}
}
} // namespace df
|