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: 236b5cae6fb240661204c63a1a44408ff96964b9 (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
#include "drape/mesh_object.hpp"

#include "drape/glconstants.hpp"
#include "drape/glextensions_list.hpp"
#include "drape/glfunctions.hpp"
#include "drape/glsl_func.hpp"
#include "drape/glsl_types.hpp"
#include "drape/gpu_program.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
{
MeshObject::MeshObject(DrawPrimitive drawPrimitive)
  : m_drawPrimitive(drawPrimitive)
{}

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

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()
{
  for (auto & buffer : 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;
  m_initialized = false;
}

void MeshObject::UpdateBuffer(uint32_t bufferInd, std::vector<float> && vertices)
{
  CHECK(m_initialized, ());
  CHECK_LESS(bufferInd, static_cast<uint32_t>(m_buffers.size()), ());
  CHECK(m_buffers[bufferInd].m_bufferId != 0, ());
  CHECK(!vertices.empty(), ());

  auto & buffer = m_buffers[bufferInd];
  buffer.m_data = std::move(vertices);
  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 MeshObject::Build(ref_ptr<dp::GpuProgram> program)
{
  Reset();

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

  for (auto & buffer : 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)
    {
      for (auto const & attribute : buffer.m_attributes)
      {
        int8_t const attributePosition = program->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);

  m_initialized = true;
}

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

  if (!m_initialized)
    Build(program);

  if (dp::GLExtensionsList::Instance().IsSupported(dp::GLExtensionsList::VertexArrayObject))
  {
    GLFunctions::glBindVertexArray(m_VAO);
  }
  else
  {
    for (auto const & buffer : m_buffers)
    {
      GLFunctions::glBindBuffer(buffer.m_bufferId, gl_const::GLArrayBuffer);
      for (auto const & attribute : buffer.m_attributes)
      {
        int8_t const attributePosition = program->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 MeshObject::DrawPrimitives()
{
  if (m_buffers.empty())
    return;

  auto const & buffer = m_buffers[0];
  auto const verticesCount = static_cast<uint32_t>(buffer.m_data.size() * sizeof(buffer.m_data[0]) / buffer.m_stride);

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

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

  if (dp::GLExtensionsList::Instance().IsSupported(dp::GLExtensionsList::VertexArrayObject))
    GLFunctions::glBindVertexArray(0);
  GLFunctions::glBindBuffer(0, gl_const::GLArrayBuffer);
}

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