From 3cfe2117952983f5663fe06291d343b94f9b5672 Mon Sep 17 00:00:00 2001 From: 0scar Date: Sat, 2 Mar 2024 22:49:29 +0100 Subject: Use the vertex array object --- src/rendering/include/engine/rendering/rendering.h | 33 ++++++----- src/rendering/src/gl.c | 27 ++++----- src/rendering/src/rendering.c | 64 ++++++++-------------- 3 files changed, 56 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h index 3acaedb..d6311de 100644 --- a/src/rendering/include/engine/rendering/rendering.h +++ b/src/rendering/include/engine/rendering/rendering.h @@ -58,24 +58,28 @@ typedef struct { u32 program; } Shader; +typedef struct { + // Maybe also define type? so texture normals can be mapped to a texture + // atlas? + u32 buffername; + isize sz; + isize n; + isize m; + void* data; +} ShaderBuffer; + typedef struct { /* Shader proram */ Shader shader; /* Vertex Array Object */ u32 vao; - /* Vertex Buffer Object */ - u32 vbo; - /* Index Buffer Object */ - u32 ibo; - /* Color (?) */ - u32 col; - - /* norm */ - u32 normal; + /* MVP (a uniform from the shader) */ u32 mvp; u32 texture; + usize buffer_len; + ShaderBuffer* buffer; } RenderObject; typedef enum { @@ -150,11 +154,12 @@ typedef struct { } data; } RenderDrawCall; -RenderObject RenderObject_new(float* model, - Shader* shader, usize sz, - float* uv, usize uv_sz, - float* normal, usize normal_sz, - u32 texture); +// TODO make all the shader buffers a list + +RenderObject RenderObject_new( + Shader* shader, + u32 texture, + ShaderBuffer *restrict buffers, usize num_buffers); Shader compile_shader(const char* file_path, const ShaderType shader_type); Shader compose_shader(Shader *shaders, usize shaders_len); diff --git a/src/rendering/src/gl.c b/src/rendering/src/gl.c index 72596df..6894b1d 100644 --- a/src/rendering/src/gl.c +++ b/src/rendering/src/gl.c @@ -186,7 +186,10 @@ void shaders_delete(Shader* shader, isize shader_len) { } } -RenderObject RenderObject_new(float* model, Shader* shader, usize sz, float* uv, usize uv_sz, float* normal, usize normal_sz, u32 texture) { +RenderObject RenderObject_new( + Shader* shader, + u32 texture, + ShaderBuffer *restrict buffers, usize num_buffers) { GladGLContext *gl = GLOBAL_PLATFORM->window->context; RenderObject o; @@ -195,21 +198,19 @@ RenderObject RenderObject_new(float* model, Shader* shader, usize sz, float* uv, /* For each buffer in the shader, */ /* The shader should be generalied, */ - gl->GenBuffers(1, &(o.vbo)); - gl->BindBuffer(GL_ARRAY_BUFFER, o.vbo); - gl->BufferData(GL_ARRAY_BUFFER, sz, model, GL_STATIC_DRAW); - - gl->GenBuffers(1, &(o.col)); - gl->BindBuffer(GL_ARRAY_BUFFER, o.col); - gl->BufferData(GL_ARRAY_BUFFER, uv_sz, uv, GL_STATIC_DRAW); - - gl->GenBuffers(1, &(o.normal)); - gl->BindBuffer(GL_ARRAY_BUFFER, o.normal); - gl->BufferData(GL_ARRAY_BUFFER, normal_sz, normal, GL_STATIC_DRAW); + for (usize i = 0; i < num_buffers; i++) { + gl->GenBuffers(1, &(buffers[i].buffername)); + gl->BindBuffer(GL_ARRAY_BUFFER, buffers[i].buffername); + gl->BufferData(GL_ARRAY_BUFFER, buffers[i].sz * buffers[i].n, buffers[i].data, GL_STATIC_DRAW); + } o.shader = *shader; - o.texture = texture; + o.buffer = buffers; + o.buffer_len = num_buffers; + o.mvp = gl->GetUniformLocation(o.shader.program, "MVP"); + + gl->BindVertexArray(0); return o; } diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index c00af68..611f997 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -73,6 +73,7 @@ void render_present(Window* w) { glm_vec3_copy(dc.data.model.pos, pos); gl->UseProgram(o->shader.program); + // TODO: Use texture atlas gl->ActiveTexture(GL_TEXTURE0); gl->BindTexture(GL_TEXTURE_2D, o->texture); @@ -91,54 +92,35 @@ void render_present(Window* w) { } // TODO: Do this only once during initialization - u32 matrix = gl->GetUniformLocation(o->shader.program, "MVP"); + u32 matrix = o->mvp; gl->UniformMatrix4fv(matrix, 1, GL_FALSE, &modelviewprojection[0][0]); } // TODO the buffers need to be abstracted a bit more - gl->EnableVertexAttribArray(0); - gl->BindBuffer(GL_ARRAY_BUFFER, o->vbo); - gl->VertexAttribPointer( - 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); - - //// Do the uv buffer (?) - gl->EnableVertexAttribArray(1); - gl->BindBuffer(GL_ARRAY_BUFFER, o->col); - gl->VertexAttribPointer( - 1, // attribute. No particular reason for 1, but must match the layout in the shader. - 2, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); - - //// Do the normal buffer (?) - gl->EnableVertexAttribArray(2); - gl->BindBuffer(GL_ARRAY_BUFFER, o->normal); - gl->VertexAttribPointer( - 2, // attribute. No particular reason for 1, but must match the layout in the shader. - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); - - - - //// Draw the model ! + gl->BindVertexArray(o->vao); + + for (usize i = 0; i < o->buffer_len; i++) { + gl->EnableVertexAttribArray(i); + gl->BindBuffer(GL_ARRAY_BUFFER, o->buffer[i].buffername); + gl->VertexAttribPointer( + i, // ... + o->buffer[i].m, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + (void*)0 // array buffer offset + ); + } + + // Draw the model ! gl->DrawArrays(GL_TRIANGLES, 0, 3*12); // Starting from vertex 0; 3 vertices total -> 1 triangle - gl->DisableVertexAttribArray(0); - gl->DisableVertexAttribArray(1); + for (usize i = 0; i < o->buffer_len; i++) { + gl->DisableVertexAttribArray(i); + } + //gl->DisableVertexAttribArray(1); + gl->BindVertexArray(0); -- cgit v1.3