Browse Source

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.
main
cinnaboot 5 years ago
parent
commit
dd64cae398
  1. 4
      src/main.cpp
  2. 16
      src/shader.cpp
  3. 10
      src/shader.h

4
src/main.cpp

@ -62,8 +62,8 @@ loadScene(RenderState* rs)
assert(df_pos_attrib && df_uv_attrib); assert(df_pos_attrib && df_uv_attrib);
GLBufferToAttribMapping default_mappings[2] = { GLBufferToAttribMapping default_mappings[2] = {
{ "position", df_pos_attrib, texmesh.vertices }, { "position", df_pos_attrib, VERTEX},
{ "uvs", df_uv_attrib, texmesh.uvs } { "uvs", df_uv_attrib, UV }
}; };
// NOTE: init new render group // NOTE: init new render group

16
src/shader.cpp

@ -251,6 +251,18 @@ initGLAttribBuffer(gl_buffer* buf, GLenum target, GLVertexAttrib* attrib)
buf->name = utilAllocateCStr(attrib->name); 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 // 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 // going to use this, can void copying GLmesh twice that way
GLmesh GLmesh
@ -273,11 +285,13 @@ loadGLMesh(const mesh& m,
u32 type_size = getGLTypeSize(attrib->data_type); u32 type_size = getGLTypeSize(attrib->data_type);
assert(type_size > 0); assert(type_size > 0);
void* mesh_buf_data = getMeshData(m, mappings[i]);
assert(mesh_buf_data);
initGLAttribBuffer(&buf, GL_ARRAY_BUFFER, attrib); initGLAttribBuffer(&buf, GL_ARRAY_BUFFER, attrib);
glBindBuffer(buf.target, buf.id); glBindBuffer(buf.target, buf.id);
glBufferData(buf.target, glBufferData(buf.target,
m.num_vertices * type_size, m.num_vertices * type_size,
mappings[i].mesh_buf, mesh_buf_data,
glm.usage); glm.usage);
glVertexAttribPointer(attrib->location, attrib->num_components, glVertexAttribPointer(attrib->location, attrib->num_components,
attrib->component_type, GL_FALSE, 0, 0); attrib->component_type, GL_FALSE, 0, 0);

10
src/shader.h

@ -106,11 +106,19 @@ struct GLContext
GLTexture* textures; GLTexture* textures;
}; };
enum MeshBufferType
{
VERTEX,
NORMAL,
UV,
COLOR
};
struct GLBufferToAttribMapping struct GLBufferToAttribMapping
{ {
const char* buf_name; const char* buf_name;
GLVertexAttrib* attrib; GLVertexAttrib* attrib;
void* mesh_buf; MeshBufferType buf_type;
}; };
// TODO: rename to GLMesh // TODO: rename to GLMesh

Loading…
Cancel
Save