From 5476dd7d21875d68a4d957f441d34eee2a9e2b37 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 29 Oct 2021 13:58:14 +0200 Subject: Fix multiple vertex buffers using the same data Need to call glVertexPointer/glTexCoordPointer/glColorPointer every time we render. --- include/mgl/gl.h | 2 ++ src/gl.c | 2 ++ src/graphics/vertex_buffer.c | 1 + src/window/window.c | 2 +- tests/main.c | 56 +++++++++++++++++++++++++++++++++++--------- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/include/mgl/gl.h b/include/mgl/gl.h index a13406e..c9a709d 100644 --- a/include/mgl/gl.h +++ b/include/mgl/gl.h @@ -67,6 +67,8 @@ typedef struct { void (*glAttachShader)(unsigned int program, unsigned int shader); int (*glGetUniformLocation)(unsigned int program, const char *name); void (*glUniform2fv)(int location, int count, const float *value); + unsigned int (*glGetError)(void); + const unsigned char* (*glGetString)(unsigned int name); /* Optional*/ void (*glXSwapIntervalEXT)(Display * dpy, GLXDrawable drawable, int interval); diff --git a/src/gl.c b/src/gl.c index 4227d9e..c54bbbf 100644 --- a/src/gl.c +++ b/src/gl.c @@ -82,6 +82,8 @@ int mgl_gl_load(mgl_gl *self) { { &self->glAttachShader, "glAttachShader" }, { &self->glGetUniformLocation, "glGetUniformLocation" }, { &self->glUniform2fv, "glUniform2fv" }, + { &self->glGetError, "glGetError" }, + { &self->glGetString, "glGetString" }, { NULL, NULL } }; diff --git a/src/graphics/vertex_buffer.c b/src/graphics/vertex_buffer.c index aa4fdbb..9203d63 100644 --- a/src/graphics/vertex_buffer.c +++ b/src/graphics/vertex_buffer.c @@ -104,6 +104,7 @@ void mgl_vertex_buffer_draw(mgl_context *context, mgl_vertex_buffer *vertex_buff context->gl.glBindTexture(GL_TEXTURE_2D, texture ? texture->id : 0); context->gl.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer->id); + mgl_vertex_buffer_set_gl_buffer_pointers(context); context->gl.glDrawArrays(mgl_primitive_type_to_gl_mode(vertex_buffer->primitive_type), 0, vertex_buffer->vertex_count); context->gl.glBindBuffer(GL_ARRAY_BUFFER, 0); context->gl.glBindTexture(GL_TEXTURE_2D, 0); diff --git a/src/window/window.c b/src/window/window.c index f29b920..94756cd 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -103,8 +103,8 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h context->gl.glEnable(GL_BLEND); context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); context->gl.glEnableClientState(GL_VERTEX_ARRAY); - context->gl.glEnableClientState(GL_COLOR_ARRAY); context->gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY); + context->gl.glEnableClientState(GL_COLOR_ARRAY); XWindowAttributes gwa; gwa.width = 0; diff --git a/tests/main.c b/tests/main.c index 53134b9..98c68ad 100644 --- a/tests/main.c +++ b/tests/main.c @@ -14,7 +14,8 @@ typedef struct { mgl_texture *texture; mgl_font *font; - mgl_vertex_buffer *vertex_buffer; + mgl_vertex_buffer *vertex_buffer1; + mgl_vertex_buffer *vertex_buffer2; mgl_shader_program *shader_program; mgl_clock clock; } Userdata; @@ -47,8 +48,11 @@ static void draw(mgl_window *window, void *userdata) { mgl_text_draw(context, &text); mgl_text_deinit(&text); - mgl_vertex_buffer_set_position(u->vertex_buffer, (mgl_vec2f){ window->cursor_position.x, window->cursor_position.y }); - mgl_vertex_buffer_draw(context, u->vertex_buffer, &u->font->texture); + mgl_vertex_buffer_set_position(u->vertex_buffer1, (mgl_vec2f){ window->cursor_position.x, window->cursor_position.y }); + mgl_vertex_buffer_draw(context, u->vertex_buffer1, &u->font->texture); + + 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); } int main(int argc, char **argv) { @@ -57,13 +61,15 @@ int main(int argc, char **argv) { mgl_texture texture; mgl_font font; - mgl_vertex_buffer vertex_buffer; + mgl_vertex_buffer vertex_buffer1; + mgl_vertex_buffer vertex_buffer2; mgl_shader_program shader_program; Userdata userdata; userdata.texture = &texture; userdata.font = &font; - userdata.vertex_buffer = &vertex_buffer; + userdata.vertex_buffer1 = &vertex_buffer1; + userdata.vertex_buffer2 = &vertex_buffer2; userdata.shader_program = &shader_program; mgl_clock_init(&userdata.clock); @@ -86,9 +92,10 @@ int main(int argc, char **argv) { if(mgl_shader_program_finalize(&shader_program) != 0) return 1; - mgl_vertex_buffer_init(&vertex_buffer); + mgl_vertex_buffer_init(&vertex_buffer1); + mgl_vertex_buffer_init(&vertex_buffer2); - mgl_vertex vertices[4] = { + mgl_vertex vertices1[4] = { (mgl_vertex){ .position = {0.0f, 0.0f}, .texcoords = {0.0f, 0.0f}, @@ -100,18 +107,44 @@ int main(int argc, char **argv) { .color = {0, 255, 0, 100}, }, (mgl_vertex){ - .position = {font.texture.width, font.texture.height}, + .position = {font.texture.width, font.texture.height/2}, + .texcoords = {1.0f, 0.5f}, + .color = {0, 0, 255, 100}, + }, + (mgl_vertex){ + .position = {0.0f, font.texture.height/2}, + .texcoords = {0.0f, 0.5f}, + .color = {255, 0, 255, 100} + } + }; + + mgl_vertex vertices2[4] = { + (mgl_vertex){ + .position = {0.0f, 0.0f}, + .texcoords = {0.0f, 0.5f}, + .color = {255, 0, 0, 100} + }, + (mgl_vertex){ + .position = {font.texture.width, 0.0f}, + .texcoords = {1.0f, 0.5f}, + .color = {0, 255, 0, 100}, + }, + (mgl_vertex){ + .position = {font.texture.width, font.texture.height/2}, .texcoords = {1.0f, 1.0f}, .color = {0, 0, 255, 100}, }, (mgl_vertex){ - .position = {0.0f, font.texture.height}, + .position = {0.0f, font.texture.height/2}, .texcoords = {0.0f, 1.0f}, .color = {255, 0, 255, 100} } }; - if(mgl_vertex_buffer_update(&vertex_buffer, vertices, 4, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0) + if(mgl_vertex_buffer_update(&vertex_buffer1, vertices1, 4, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0) + return 1; + + if(mgl_vertex_buffer_update(&vertex_buffer2, vertices2, 4, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0) return 1; fprintf(stderr, "Font texture width: %d, texture height: %d\n", font.texture.width, font.texture.height); @@ -127,7 +160,8 @@ int main(int argc, char **argv) { mgl_window_display(&window); } - mgl_vertex_buffer_deinit(&vertex_buffer); + mgl_vertex_buffer_deinit(&vertex_buffer2); + mgl_vertex_buffer_deinit(&vertex_buffer1); mgl_shader_program_deinit(&shader_program); mgl_font_unload(&font); mgl_texture_unload(&texture); -- cgit v1.2.3