Unheavly _GLBuffer and create new fanction initVertexArray

This commit is contained in:
CRy386i 2025-02-23 12:10:07 +02:00
parent 5460918bcc
commit b6ae3369a1
2 changed files with 24 additions and 9 deletions

View file

@ -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);
}

View file

@ -8,6 +8,7 @@ typedef struct {
typedef struct {
GLuint program;
GLuint vao;
GLuint vbo;
GLuint ibo;
GLuint projMatrLoc;