Проблема в том, что мне приходится заново создавать буфер вершин в каждом кадре, чтобы квадраты появлялись. Это также означает, что массив вершин должен быть связан с макетом в каждом кадре. Я новичок в OpenGL, но, насколько я могу судить, в этом нет необходимости. Итак, часть моего вопроса заключается в том, почему воссоздание буфера вершин для каждого кадра работает как исправление и почему квадраты не появляются только из кода, показанного ниже? Код находится в классе с конструктором и методом onUpdate.
Конструктор:
Код: Выделить всё
m_positions = {
0.1f, 0.1f,
0.2f, 0.1f,
0.2f, 0.2f,
0.1f, 0.2f,
0.3f, 0.3f,
0.4f, 0.3f,
0.4f, 0.4f,
0.3f, 0.4f,
};
m_vb = VertexBuffer{ m_positions.data(), static_cast(m_positions.size() * sizeof(float)) };
size_t vertex_count = m_positions.size() / 2; // 2 coordinates per vertex
size_t quad_count = vertex_count / 4; // 4 vertices per quad
size_t count = quad_count * 6; // 6 indices per quad
std::vector indices(count);
for(size_t i = 0, vertex_index = 0; i < count; i += 6, vertex_index += 4)
{
indices[i + 0] = vertex_index + 0;
indices[i + 1] = vertex_index + 1;
indices[i + 2] = vertex_index + 2;
indices[i + 3] = vertex_index + 2;
indices[i + 4] = vertex_index + 3;
indices[i + 5] = vertex_index + 0;
}
m_ib = IndexBuffer{ indices.data(), static_cast(count) };
m_layout.push(2);
m_va.addBuffer(m_vb, m_layout);
Код: Выделить всё
auto vb = VertexBuffer{ m_positions.data(), static_cast(m_positions.size() *
sizeof(float)) };
vb.bind();
// m_vb.bind(); // Should work instead of the two lines above
m_va.bind();
// This should only include one of the squares
GLCall( glEnableVertexAttribArray(0) );
GLCall( glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 8, (const void*)0) );
m_shader.bind();
m_va.bind();
m_ib.bind();
GLCall(glDrawElements(GL_TRIANGLES, m_ib.getCount(), GL_UNSIGNED_INT, nullptr));
m_shader.unbind();
m_va.unbind();
m_ib.unbind();
Подробнее здесь: https://stackoverflow.com/questions/788 ... -to-appear