From 061b9ba6c2d861a2e8f310d5f65da7ffe45d3bfc Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 17 Jul 2021 21:44:59 -0400 Subject: [PATCH] [client] egl: use vertex array objects in model.c This eliminates the need to bind the buffers and set up the vertices on every frame. --- client/renderers/EGL/model.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/client/renderers/EGL/model.c b/client/renderers/EGL/model.c index 4d2b2292..0f58c621 100644 --- a/client/renderers/EGL/model.c +++ b/client/renderers/EGL/model.c @@ -38,6 +38,7 @@ struct EGL_Model bool finish; GLuint buffer; + GLuint vao; EGL_Shader * shader; EGL_Texture * texture; @@ -85,6 +86,9 @@ void egl_model_free(EGL_Model ** model) if ((*model)->buffer) glDeleteBuffers(1, &(*model)->buffer); + if ((*model)->vao) + glDeleteVertexArrays(1, &(*model)->vao); + free(*model); *model = NULL; } @@ -139,6 +143,11 @@ void egl_model_render(EGL_Model * model) if (model->buffer) glDeleteBuffers(1, &model->buffer); + if (!model->vao) + glGenVertexArrays(1, &model->vao); + + glBindVertexArray(model->vao); + /* create a buffer large enough */ glGenBuffers(1, &model->buffer); glBindBuffer(GL_ARRAY_BUFFER, model->buffer); @@ -161,16 +170,18 @@ void egl_model_render(EGL_Model * model) offset += sizeof(GLfloat) * fl->count * 2; } + /* set up vertex arrays in the VAO */ + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(GLfloat) * model->vertexCount * 3)); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); model->rebuild = false; } - /* bind the model buffer and setup the pointers */ - glBindBuffer(GL_ARRAY_BUFFER, model->buffer); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(GLfloat) * model->vertexCount * 3)); + glBindVertexArray(model->vao); if (model->shader) egl_shader_use(model->shader); @@ -189,8 +200,7 @@ void egl_model_render(EGL_Model * model) /* unbind and cleanup */ glBindTexture(GL_TEXTURE_2D, 0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); + glBindVertexArray(0); glUseProgram(0); }