Skip to content

Commit

Permalink
Move from glDrawArrays to glDrawElements
Browse files Browse the repository at this point in the history
  • Loading branch information
rtoumazet committed Dec 15, 2023
1 parent 256accf commit 7b9ecf0
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions saturnin/src/video/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ void Opengl::render() {

const auto texture_used_loc = glGetUniformLocation(program_shader_, "is_texture_used");

auto elements_buffer = u32{};
glGenBuffers(1, &elements_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements_buffer);
std::array<GLuint, 6> indices = {0, 1, 2, 0, 2, 3};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices.data(), GL_STATIC_DRAW);

for (const auto& part : parts_list) {
if (part->vertexes_.empty()) { continue; }

Expand Down Expand Up @@ -388,23 +394,32 @@ void Opengl::render() {

// Quad is tessellated into 2 triangles, using a texture
//******
auto vertexes = std::vector<Vertex>{};

vertexes.reserve(vertexes_per_tessellated_quad);
// Transforming one quad in 2 triangles
vertexes.emplace_back(part->vertexes_[0]);
vertexes.emplace_back(part->vertexes_[1]);
vertexes.emplace_back(part->vertexes_[2]);
vertexes.emplace_back(part->vertexes_[0]);
vertexes.emplace_back(part->vertexes_[2]);
vertexes.emplace_back(part->vertexes_[3]);

// Sending vertex buffer data to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertexes.size(), vertexes.data(), GL_STATIC_DRAW);
// glActiveTexture(GLenum::GL_TEXTURE0);
//******
if constexpr (constexpr bool usesDrawElements = true) {
glBufferData(GL_ARRAY_BUFFER,
sizeof(Vertex) * part->vertexes_.size(),
part->vertexes_.data(),
GL_STATIC_DRAW);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
} else {
auto vertexes = std::vector<Vertex>{};

vertexes.reserve(vertexes_per_tessellated_quad);
// Transforming one quad in 2 triangles
vertexes.emplace_back(part->vertexes_[0]);
vertexes.emplace_back(part->vertexes_[1]);
vertexes.emplace_back(part->vertexes_[2]);
vertexes.emplace_back(part->vertexes_[0]);
vertexes.emplace_back(part->vertexes_[2]);
vertexes.emplace_back(part->vertexes_[3]);

//// Sending vertex buffer data to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertexes.size(), vertexes.data(), GL_STATIC_DRAW);

glDrawArrays(GL_TRIANGLES, 0, vertexes_per_tessellated_quad);
}

glDrawArrays(GL_TRIANGLES, 0, vertexes_per_tessellated_quad);
break;
}
case non_textured_polygon: {
Expand Down Expand Up @@ -485,7 +500,8 @@ void Opengl::render() {
// glDeleteVertexArrays(elements_nb, vao_ids_array.data());
gl::glDeleteBuffers(1, &vertex_buffer);
gl::glDeleteVertexArrays(1, &vao);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
gl::glDeleteBuffers(1, &elements_buffer);
// Texture::cleanCache(this);
}

Expand Down

0 comments on commit 7b9ecf0

Please sign in to comment.