Move shape buffer mapping to renderer gl
This commit is contained in:
130
src/shape.cpp
130
src/shape.cpp
@@ -6,6 +6,40 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] GLenum array_buffer_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::array_buffer_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum element_array_buffer_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::element_array_buffer_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum static_draw_buffer_usage() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::static_draw_buffer_usage());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum vertex_attribute_float_component_type() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::vertex_attribute_float_component_type());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLboolean vertex_attribute_not_normalized() noexcept
|
||||
{
|
||||
return static_cast<GLboolean>(pp::renderer::gl::vertex_attribute_not_normalized());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum uint16_index_type() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::index_type_for_index_size(sizeof(GLushort)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool Shape::create_buffers(GLushort * idx, GLvoid * vertices, int isize, int vsize)
|
||||
{
|
||||
index_type = static_cast<GLenum>(pp::renderer::gl::index_type_for_index_size(sizeof(GLushort)));
|
||||
@@ -36,12 +70,12 @@ bool Shape::create_buffers_imp(GLvoid* idx, GLvoid* vertices, int isize, int vsi
|
||||
return;
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, isize, idx, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vsize, vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(element_array_buffer_target(), buffers[1]);
|
||||
glBufferData(element_array_buffer_target(), isize, idx, static_draw_buffer_usage());
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glBufferData(array_buffer_target(), vsize, vertices, static_draw_buffer_usage());
|
||||
glBindBuffer(element_array_buffer_target(), 0);
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
|
||||
#if USE_VBO
|
||||
glGenVertexArrays(2, arrays);
|
||||
@@ -57,12 +91,12 @@ bool Shape::create_buffers_imp(GLvoid* idx, GLvoid* vertices, int isize, int vsi
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs2));
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, nor));
|
||||
glBindBuffer(element_array_buffer_target(), buffers[1]);
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
glVertexAttribPointer(2, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs2));
|
||||
glVertexAttribPointer(3, 3, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, nor));
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
#endif
|
||||
@@ -87,10 +121,10 @@ bool Shape::create_buffers(GLvoid* vertices, int vsize)
|
||||
|
||||
if (vsize)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vsize, vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glBufferData(array_buffer_target(), vsize, vertices, static_draw_buffer_usage());
|
||||
glBindBuffer(element_array_buffer_target(), 0);
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
}
|
||||
|
||||
#if USE_VBO
|
||||
@@ -107,11 +141,11 @@ bool Shape::create_buffers(GLvoid* vertices, int vsize)
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs2));
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, nor));
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
glVertexAttribPointer(2, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs2));
|
||||
glVertexAttribPointer(3, 3, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, nor));
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
#endif
|
||||
@@ -135,20 +169,20 @@ void Shape::draw_fill() const
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
if (use_idx)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glDrawElements(type, count[0], GL_UNSIGNED_SHORT, ioff[0]);
|
||||
glBindBuffer(element_array_buffer_target(), buffers[1]);
|
||||
glDrawElements(type, count[0], uint16_index_type(), ioff[0]);
|
||||
}
|
||||
else
|
||||
glDrawArrays(type, 0, count[0]);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
glBindBuffer(element_array_buffer_target(), 0);
|
||||
#endif // USE_VBO
|
||||
});
|
||||
}
|
||||
@@ -169,21 +203,21 @@ void Shape::draw_stroke() const
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
glBindBuffer(element_array_buffer_target(), buffers[1]);
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
|
||||
if (use_idx)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glDrawElements(type, count[1], GL_UNSIGNED_SHORT, ioff[1]);
|
||||
glBindBuffer(element_array_buffer_target(), buffers[1]);
|
||||
glDrawElements(type, count[1], uint16_index_type(), ioff[1]);
|
||||
}
|
||||
else
|
||||
glDrawArrays(type, 0, count[1]);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
glBindBuffer(element_array_buffer_target(), 0);
|
||||
#endif // USE_VBO
|
||||
});
|
||||
}
|
||||
@@ -489,8 +523,8 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g
|
||||
|
||||
App::I->render_task([this]
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glBufferData(array_buffer_target(), sizeof(vertices), vertices, static_draw_buffer_usage());
|
||||
static GLushort idx[6 + 8]{
|
||||
0, 1, 2,
|
||||
0, 2, 3,
|
||||
@@ -499,11 +533,11 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g
|
||||
2, 3,
|
||||
3, 0,
|
||||
};
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
|
||||
glBindBuffer(element_array_buffer_target(), buffers[1]);
|
||||
glBufferData(element_array_buffer_target(), sizeof(idx), idx, static_draw_buffer_usage());
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
glBindBuffer(element_array_buffer_target(), 0);
|
||||
});
|
||||
}
|
||||
void Circle::create_impl(float radius, int div, GLushort* idx, vertex_t* vertices)
|
||||
@@ -762,9 +796,9 @@ void LineSegment::update_vertices(const glm::vec4 data[2])
|
||||
static vertex_t vertices[2];
|
||||
vertices[0] = { data[0], { 0, 0 } }; // A
|
||||
vertices[1] = { data[1], { 0, 1 } }; // B
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glBufferData(array_buffer_target(), sizeof(vertices), vertices, static_draw_buffer_usage());
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
});
|
||||
}
|
||||
void DynamicShape::update_vertices(vertex_t* vertices, int vcount)
|
||||
@@ -773,8 +807,8 @@ void DynamicShape::update_vertices(vertex_t* vertices, int vcount)
|
||||
{
|
||||
count[0] = vcount;
|
||||
count[1] = vcount;
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_t) * vcount, vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(array_buffer_target(), buffers[0]);
|
||||
glBufferData(array_buffer_target(), sizeof(vertex_t) * vcount, vertices, static_draw_buffer_usage());
|
||||
glBindBuffer(array_buffer_target(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user