From b6ae3369a1554c75edf24ddf632b6e3f77012a30 Mon Sep 17 00:00:00 2001 From: CRy386i Date: Sun, 23 Feb 2025 12:10:07 +0200 Subject: [PATCH] Unheavly _GLBuffer and create new fanction initVertexArray --- opengl.c | 30 ++++++++++++++++++++++-------- opengl.h | 3 ++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/opengl.c b/opengl.c index bce0a4a..0285c39 100644 --- a/opengl.c +++ b/opengl.c @@ -91,6 +91,24 @@ void createProgram ( GLuint *shaders, int len ) } } +void initVertexArray ( void ) +{ + glGenVertexArrays(1, &sp.vao); + glBindVertexArray(sp.vao); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, sp.vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sp.ibo); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (4*4*sizeof(float))); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sp.ibo); + + glBindVertexArray(0); +} + void createBuffer ( void ) { GLushort indices[] = { @@ -128,6 +146,7 @@ void setUniformLocation ( void ) void startShaderProg ( void ) { createBuffer(); + initVertexArray(); GLuint shaders[] = { createShader("vertex.glsl", GL_VERTEX_SHADER), @@ -152,16 +171,11 @@ void _GLBuffer( void ) glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(sp.program); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, sp.vbo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sp.ibo); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); - glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (4*4*sizeof(float))); + glBindVertexArray(sp.vao); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); + glBindVertexArray(0); glUseProgram(0); } diff --git a/opengl.h b/opengl.h index 8d0285d..4409338 100644 --- a/opengl.h +++ b/opengl.h @@ -5,9 +5,10 @@ typedef struct { SDL_Window* window; SDL_GLContext glcontext; } _glWindows_t; - + typedef struct { GLuint program; + GLuint vao; GLuint vbo; GLuint ibo; GLuint projMatrLoc;