From dd64cae39864b707f927fd3723a726832d2742e9 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 14 Jan 2022 08:59:41 -0500 Subject: [PATCH] add a function to get the correct mesh buffer automatically Previously we had to add the correct buffer to the GLBufferToAttribMapping struct. This doesn't work if we want to deal with entities at a high level. Now we can specify the buffer type as an enum, and have a helper function get a reference to the corrent mesh buffer when loading into GL. --- src/main.cpp | 4 ++-- src/shader.cpp | 16 +++++++++++++++- src/shader.h | 10 +++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5173cd7..6b7ee11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,8 +62,8 @@ loadScene(RenderState* rs) assert(df_pos_attrib && df_uv_attrib); GLBufferToAttribMapping default_mappings[2] = { - { "position", df_pos_attrib, texmesh.vertices }, - { "uvs", df_uv_attrib, texmesh.uvs } + { "position", df_pos_attrib, VERTEX}, + { "uvs", df_uv_attrib, UV } }; // NOTE: init new render group diff --git a/src/shader.cpp b/src/shader.cpp index a4309b5..a17f754 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -251,6 +251,18 @@ initGLAttribBuffer(gl_buffer* buf, GLenum target, GLVertexAttrib* attrib) buf->name = utilAllocateCStr(attrib->name); } +void* +getMeshData(const mesh& m, const GLBufferToAttribMapping& mapping) +{ + switch (mapping.buf_type) { + case VERTEX: return m.vertices; + case NORMAL: return m.normals; + case UV: return m.uvs; + case COLOR: return m.colors; + default: return nullptr; + } +} + // FIXME: might as well pass in pointer to GLmesh, since that's how we're // going to use this, can void copying GLmesh twice that way GLmesh @@ -273,11 +285,13 @@ loadGLMesh(const mesh& m, u32 type_size = getGLTypeSize(attrib->data_type); assert(type_size > 0); + void* mesh_buf_data = getMeshData(m, mappings[i]); + assert(mesh_buf_data); initGLAttribBuffer(&buf, GL_ARRAY_BUFFER, attrib); glBindBuffer(buf.target, buf.id); glBufferData(buf.target, m.num_vertices * type_size, - mappings[i].mesh_buf, + mesh_buf_data, glm.usage); glVertexAttribPointer(attrib->location, attrib->num_components, attrib->component_type, GL_FALSE, 0, 0); diff --git a/src/shader.h b/src/shader.h index 876242e..50fb860 100644 --- a/src/shader.h +++ b/src/shader.h @@ -106,11 +106,19 @@ struct GLContext GLTexture* textures; }; +enum MeshBufferType +{ + VERTEX, + NORMAL, + UV, + COLOR +}; + struct GLBufferToAttribMapping { const char* buf_name; GLVertexAttrib* attrib; - void* mesh_buf; + MeshBufferType buf_type; }; // TODO: rename to GLMesh