From 275e851e8a2b11c2efe7c39787bf331dd3b75272 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 29 Oct 2021 14:29:24 +0200 Subject: Use gl*Pointer and glDrawArrays to render vertices (in mgl_vertices_draw) instead of loop with glBegin/glEnd --- src/graphics/vertex.c | 15 +++++++-------- src/graphics/vertex_buffer.c | 2 -- tests/main.c | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/graphics/vertex.c b/src/graphics/vertex.c index 629b421..dff9661 100644 --- a/src/graphics/vertex.c +++ b/src/graphics/vertex.c @@ -2,12 +2,11 @@ #include "../../include/mgl/mgl.h" void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type) { - context->gl.glBegin(mgl_primitive_type_to_gl_mode(primitive_type)); - for(size_t i = 0; i < vertex_count; ++i) { - const mgl_vertex *vertex = &vertices[i]; - context->gl.glColor4ub(vertex->color.r, vertex->color.g, vertex->color.b, vertex->color.a); - context->gl.glTexCoord2f(vertex->texcoords.x, vertex->texcoords.y); - context->gl.glVertex3f(vertex->position.x, vertex->position.y, 0.0f); - } - context->gl.glEnd(); + if(vertex_count == 0) + return; + + context->gl.glVertexPointer(2, GL_FLOAT, sizeof(mgl_vertex), (void*)&vertices[0].position); + context->gl.glTexCoordPointer(2, GL_FLOAT, sizeof(mgl_vertex), (void*)&vertices[0].texcoords); + context->gl.glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(mgl_vertex), (void*)&vertices[0].color); + context->gl.glDrawArrays(mgl_primitive_type_to_gl_mode(primitive_type), 0, vertex_count); } diff --git a/src/graphics/vertex_buffer.c b/src/graphics/vertex_buffer.c index 9203d63..325665c 100644 --- a/src/graphics/vertex_buffer.c +++ b/src/graphics/vertex_buffer.c @@ -58,7 +58,6 @@ static int mgl_vertex_buffer_resize(mgl_vertex_buffer *self, const mgl_vertex *v context->gl.glBindBuffer(GL_ARRAY_BUFFER, self->id); /* TODO: Optimize by calling with NULL data first? or do that in |mgl_vertex_buffer_update| */ context->gl.glBufferData(GL_ARRAY_BUFFER, sizeof(mgl_vertex) * vertex_count, vertices, mgl_vertex_buffer_usage_to_gl_usage(self->usage)); - mgl_vertex_buffer_set_gl_buffer_pointers(context); context->gl.glBindBuffer(GL_ARRAY_BUFFER, 0); self->vertex_count = vertex_count; return 0; @@ -88,7 +87,6 @@ int mgl_vertex_buffer_update(mgl_vertex_buffer *self, const mgl_vertex *vertices context->gl.glBindBuffer(GL_ARRAY_BUFFER, self->id); context->gl.glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(mgl_vertex) * vertex_count, vertices); - mgl_vertex_buffer_set_gl_buffer_pointers(context); context->gl.glBindBuffer(GL_ARRAY_BUFFER, 0); return 0; } diff --git a/tests/main.c b/tests/main.c index 98c68ad..4f29a55 100644 --- a/tests/main.c +++ b/tests/main.c @@ -53,6 +53,31 @@ static void draw(mgl_window *window, void *userdata) { mgl_vertex_buffer_set_position(u->vertex_buffer2, (mgl_vec2f){ window->cursor_position.x, window->cursor_position.y + 500 }); mgl_vertex_buffer_draw(context, u->vertex_buffer2, &u->font->texture); + + mgl_vertex vertices[4] = { + (mgl_vertex){ + .position = {window->cursor_position.x, 0.0f}, + .texcoords = {0.0f, 0.0f}, + .color = {0, 255, 0, 255} + }, + (mgl_vertex){ + .position = {window->cursor_position.x + 100.0f, 0.0f}, + .texcoords = {0.0f, 0.0f}, + .color = {0, 255, 0, 255}, + }, + (mgl_vertex){ + .position = {window->cursor_position.x + 100.0f, 100.0f}, + .texcoords = {0.0f, 0.0f}, + .color = {0, 255, 0, 255}, + }, + (mgl_vertex){ + .position = {window->cursor_position.x, 100.0f}, + .texcoords = {0.0f, 0.0f}, + .color = {0, 255, 0, 255} + } + }; + + mgl_vertices_draw(context, vertices, 4, MGL_PRIMITIVE_QUADS); } int main(int argc, char **argv) { -- cgit v1.2.3