Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mesh_object.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8440542b8618ae9efd9ea371ceee3601ef0da7a (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "drape/mesh_object.hpp"

#include "drape/gl_constants.hpp"
#include "drape/gl_gpu_program.hpp"
#include "drape/gl_functions.hpp"
#include "drape/glsl_func.hpp"
#include "drape/glsl_types.hpp"
#include "drape/texture_manager.hpp"

namespace
{
glConst GetGLDrawPrimitive(dp::MeshObject::DrawPrimitive drawPrimitive)
{
  switch (drawPrimitive)
  {
  case dp::MeshObject::DrawPrimitive::Triangles: return gl_const::GLTriangles;
  case dp::MeshObject::DrawPrimitive::TriangleStrip: return gl_const::GLTriangleStrip;
  case dp::MeshObject::DrawPrimitive::LineStrip: return gl_const::GLLineStrip;
  }
}
}  // namespace

namespace dp
{
class GLMeshObjectImpl : public MeshObjectImpl
{
public:
  explicit GLMeshObjectImpl(ref_ptr<dp::MeshObject> mesh)
    : m_mesh(mesh)
  {}

  void Build(ref_ptr<dp::GraphicsContext> context, ref_ptr<dp::GpuProgram> program) override
  {
    UNUSED_VALUE(context);

    bool const isVAOSupported =
      GLFunctions::ExtensionsList.IsSupported(dp::GLExtensionsList::VertexArrayObject);
    if (isVAOSupported)
    {
      m_VAO = GLFunctions::glGenVertexArray();
      GLFunctions::glBindVertexArray(m_VAO);
    }

    for (auto & buffer : m_mesh->m_buffers)
    {
      buffer.m_bufferId = GLFunctions::glGenBuffer();
      GLFunctions::glBindBuffer(buffer.m_bufferId, gl_const::GLArrayBuffer);

      if (!buffer.m_data.empty())
      {
        GLFunctions::glBufferData(gl_const::GLArrayBuffer,
                                  static_cast<uint32_t>(buffer.m_data.size()) * sizeof(buffer.m_data[0]),
                                  buffer.m_data.data(), gl_const::GLStaticDraw);
      }

      if (isVAOSupported)
      {
        ref_ptr<dp::GLGpuProgram> p = program;
        for (auto const & attribute : buffer.m_attributes)
        {
          int8_t const attributePosition = p->GetAttributeLocation(attribute.m_attributeName);
          ASSERT_NOT_EQUAL(attributePosition, -1, ());
          GLFunctions::glEnableVertexAttribute(attributePosition);
          GLFunctions::glVertexAttributePointer(attributePosition, attribute.m_componentsCount,
                                                gl_const::GLFloatType, false,
                                                buffer.m_stride, attribute.m_offset);
        }
      }
    }

    if (isVAOSupported)
      GLFunctions::glBindVertexArray(0);
    GLFunctions::glBindBuffer(0, gl_const::GLArrayBuffer);
  }

  void Reset() override
  {
    for (auto & buffer : m_mesh->m_buffers)
    {
      if (buffer.m_bufferId != 0)
      {
        GLFunctions::glDeleteBuffer(buffer.m_bufferId);
        buffer.m_bufferId = 0;
      }
    }

    if (m_VAO != 0)
      GLFunctions::glDeleteVertexArray(m_VAO);

    m_VAO = 0;
  }

  void UpdateBuffer(ref_ptr<dp::GraphicsContext> context, uint32_t bufferInd) override
  {
    UNUSED_VALUE(context);
    auto & buffer = m_mesh->m_buffers[bufferInd];
    GLFunctions::glBindBuffer(buffer.m_bufferId, gl_const::GLArrayBuffer);
    GLFunctions::glBufferData(gl_const::GLArrayBuffer,
                              static_cast<uint32_t>(buffer.m_data.size()) * sizeof(buffer.m_data[0]),
                              buffer.m_data.data(), gl_const::GLStaticDraw);
    GLFunctions::glBindBuffer(0, gl_const::GLArrayBuffer);
  }

  void Bind(ref_ptr<dp::GpuProgram> program) override
  {
    if (GLFunctions::ExtensionsList.IsSupported(dp::GLExtensionsList::VertexArrayObject))
    {
      GLFunctions::glBindVertexArray(m_VAO);
      return;
    }

    ref_ptr<dp::GLGpuProgram> p = program;
    for (auto const & buffer : m_mesh->m_buffers)
    {
      GLFunctions::glBindBuffer(buffer.m_bufferId, gl_const::GLArrayBuffer);
      for (auto const & attribute : buffer.m_attributes)
      {
        int8_t const attributePosition = p->GetAttributeLocation(attribute.m_attributeName);
        ASSERT_NOT_EQUAL(attributePosition, -1, ());
        GLFunctions::glEnableVertexAttribute(attributePosition);
        GLFunctions::glVertexAttributePointer(attributePosition, attribute.m_componentsCount,
                                              gl_const::GLFloatType, false,
                                              buffer.m_stride, attribute.m_offset);
      }
    }
  }

  void Unbind() override
  {
    if (GLFunctions::ExtensionsList.IsSupported(dp::GLExtensionsList::VertexArrayObject))
      GLFunctions::glBindVertexArray(0);
    GLFunctions::glBindBuffer(0, gl_const::GLArrayBuffer);
  }

  void DrawPrimitives(ref_ptr<dp::GraphicsContext> context, uint32_t verticesCount) override
  {
    UNUSED_VALUE(context);

    GLFunctions::glDrawArrays(GetGLDrawPrimitive(m_mesh->m_drawPrimitive), 0, verticesCount);
  }

private:
  ref_ptr<dp::MeshObject> m_mesh;
  uint32_t m_VAO = 0;
};

MeshObject::MeshObject(ref_ptr<dp::GraphicsContext> context, DrawPrimitive drawPrimitive)
  : m_drawPrimitive(drawPrimitive)
{
  auto const apiVersion = context->GetApiVersion();
  if (apiVersion == dp::ApiVersion::OpenGLES2 || apiVersion == dp::ApiVersion::OpenGLES3)
  {
    InitForOpenGL();
  }
  else if (apiVersion == dp::ApiVersion::Metal)
  {
#if defined(OMIM_METAL_AVAILABLE)
    InitForMetal();
#endif
  }
  else if (apiVersion == dp::ApiVersion::Vulkan)
  {
    InitForVulkan(context);
  }
  CHECK(m_impl != nullptr, ());
}

MeshObject::~MeshObject()
{
  Reset();
}

void MeshObject::InitForOpenGL()
{
  m_impl = make_unique_dp<GLMeshObjectImpl>(make_ref(this));
}

void MeshObject::SetBuffer(uint32_t bufferInd, std::vector<float> && vertices, uint32_t stride)
{
  CHECK_LESS_OR_EQUAL(bufferInd, GetNextBufferIndex(), ());

  if (bufferInd == GetNextBufferIndex())
    m_buffers.emplace_back(std::move(vertices), stride);
  else
    m_buffers[bufferInd] = VertexBuffer(std::move(vertices), stride);

  Reset();
}

void MeshObject::SetAttribute(std::string const & attributeName, uint32_t bufferInd, uint32_t offset,
                              uint32_t componentsCount)
{
  CHECK_LESS(bufferInd, m_buffers.size(), ());
  m_buffers[bufferInd].m_attributes.emplace_back(attributeName, offset, componentsCount);

  Reset();
}

void MeshObject::Reset()
{
  CHECK(m_impl != nullptr, ());
  m_impl->Reset();

  m_initialized = false;
}

void MeshObject::UpdateBuffer(ref_ptr<dp::GraphicsContext> context, uint32_t bufferInd,
                              std::vector<float> && vertices)
{
  CHECK(m_initialized, ());
  CHECK_LESS(bufferInd, static_cast<uint32_t>(m_buffers.size()), ());
  CHECK(!vertices.empty(), ());

  auto & buffer = m_buffers[bufferInd];
  buffer.m_data = std::move(vertices);

  CHECK(m_impl != nullptr, ());
  m_impl->UpdateBuffer(context, bufferInd);
}

void MeshObject::Build(ref_ptr<dp::GraphicsContext> context, ref_ptr<dp::GpuProgram> program)
{
  Reset();

  CHECK(m_impl != nullptr, ());
  m_impl->Build(context, program);

  m_initialized = true;
}

void MeshObject::Bind(ref_ptr<dp::GraphicsContext> context, ref_ptr<dp::GpuProgram> program)
{
  program->Bind();

  if (!m_initialized)
    Build(context, program);

  CHECK(m_impl != nullptr, ());
  m_impl->Bind(program);
}

void MeshObject::DrawPrimitives(ref_ptr<dp::GraphicsContext> context)
{
  if (m_buffers.empty())
    return;

  auto const & buffer = m_buffers[0];
#ifdef DEBUG
  for (size_t i = 1; i < m_buffers.size(); i++)
  {
    ASSERT_EQUAL(m_buffers[i].m_data.size() / m_buffers[i].m_stride,
                 buffer.m_data.size() / buffer.m_stride, ());
  }
#endif
  auto const verticesCount =
      static_cast<uint32_t>(buffer.m_data.size() * sizeof(buffer.m_data[0]) / buffer.m_stride);

  CHECK(m_impl != nullptr, ());
  m_impl->DrawPrimitives(context, verticesCount);
}

void MeshObject::Unbind(ref_ptr<dp::GpuProgram> program)
{
  program->Unbind();

  CHECK(m_impl != nullptr, ());
  m_impl->Unbind();
}

// static
std::vector<float> MeshObject::GenerateNormalsForTriangles(std::vector<float> const & vertices,
                                                           size_t componentsCount)
{
  auto const trianglesCount = vertices.size() / (3 * componentsCount);
  std::vector<float> normals;
  normals.reserve(trianglesCount * 9);
  for (size_t triangle = 0; triangle < trianglesCount; ++triangle)
  {
    glsl::vec3 v[3];
    for (size_t vertex = 0; vertex < 3; ++vertex)
    {
      size_t const offset = triangle * componentsCount * 3 + vertex * componentsCount;
      v[vertex] = glsl::vec3(vertices[offset], vertices[offset + 1], vertices[offset + 2]);
    }

    glsl::vec3 normal = glsl::cross(glsl::vec3(v[1].x - v[0].x, v[1].y - v[0].y, v[1].z - v[0].z),
                                    glsl::vec3(v[2].x - v[0].x, v[2].y - v[0].y, v[2].z - v[0].z));
    normal = glsl::normalize(normal);

    for (size_t vertex = 0; vertex < 3; ++vertex)
    {
      normals.push_back(normal.x);
      normals.push_back(normal.y);
      normals.push_back(normal.z);
    }
  }
  return normals;
}
}  // namespace dp