|
|
|
@ -131,13 +131,19 @@ getShaderByID(GLContext* gl_ctx, GLuint prog_id) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
renderVAO(GLmesh* glmesh) |
|
|
|
renderVAO(GLmesh* glmesh, shader_program* shader) |
|
|
|
{ |
|
|
|
{ |
|
|
|
glUseProgram(glmesh->shader->prog_id); |
|
|
|
glUseProgram(shader->prog_id); |
|
|
|
glBindVertexArray(glmesh->vao_id); |
|
|
|
glBindVertexArray(glmesh->vao_id); |
|
|
|
glUniformMatrix4fv(glmesh->shader->model_xform_id, 1, GL_FALSE, |
|
|
|
glUniformMatrix4fv(shader->model_xform_id, 1, GL_FALSE, |
|
|
|
(float*) glmesh->model_xform); |
|
|
|
(float*) glmesh->model_xform); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: remove conditional when removing glmesh IDs
|
|
|
|
|
|
|
|
if (glmesh->element_buf) |
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glmesh->element_buf->id); |
|
|
|
|
|
|
|
else |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glmesh->idx_buf_id); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glmesh->idx_buf_id); |
|
|
|
|
|
|
|
|
|
|
|
glDrawElements( |
|
|
|
glDrawElements( |
|
|
|
glmesh->draw_mode, glmesh->num_indices, GL_UNSIGNED_SHORT, 0); |
|
|
|
glmesh->draw_mode, glmesh->num_indices, GL_UNSIGNED_SHORT, 0); |
|
|
|
glBindVertexArray(0); |
|
|
|
glBindVertexArray(0); |
|
|
|
@ -241,25 +247,92 @@ getVertexAttribByName(shader_program* shader, const char* name) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GLmesh |
|
|
|
GLmesh |
|
|
|
loadGLMesh(shader_program* s, |
|
|
|
initGLmesh(const mesh& m, u32 num_mappings, GLenum draw_mode, glm::vec3 pos) |
|
|
|
const mesh& m, |
|
|
|
{ |
|
|
|
|
|
|
|
GLmesh glm = {0}; |
|
|
|
|
|
|
|
glm.num_indices = m.num_indices; |
|
|
|
|
|
|
|
glm.draw_mode = draw_mode; |
|
|
|
|
|
|
|
glm.usage = GL_STATIC_DRAW; // TODO: logic for updating dynamic meshes
|
|
|
|
|
|
|
|
glm.num_vertex_attrib_buffers = num_mappings; |
|
|
|
|
|
|
|
glm.vertex_attrib_buffers = UTIL_ALLOC(num_mappings, gl_buffer); |
|
|
|
|
|
|
|
glm.model_xform = UTIL_ALLOC(1, glm::mat4); |
|
|
|
|
|
|
|
glm.element_buf = UTIL_ALLOC(1, gl_buffer); |
|
|
|
|
|
|
|
*glm.model_xform = glm::translate(glm::mat4(1), pos); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return glm; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
initGLAttribBuffer(gl_buffer* buf, GLenum target, GLVertexAttrib* attrib) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
glGenBuffers(1, &buf->id); |
|
|
|
|
|
|
|
buf->target = target; |
|
|
|
|
|
|
|
buf->data_type = attrib->data_type; |
|
|
|
|
|
|
|
buf->location = attrib->location; |
|
|
|
|
|
|
|
buf->name = utilAllocateCStr(attrib->name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: might as well pass in pointer to GLmesh, since that's how we're
|
|
|
|
|
|
|
|
// going to use this, can void copying GLmesh twich that way
|
|
|
|
|
|
|
|
GLmesh |
|
|
|
|
|
|
|
loadGLMesh(const mesh& m, |
|
|
|
GLenum draw_mode, |
|
|
|
GLenum draw_mode, |
|
|
|
const glm::vec3& pos, |
|
|
|
const glm::vec3& pos, |
|
|
|
u32 num_mappings, |
|
|
|
u32 num_mappings, |
|
|
|
GLBufferToAttribMapping mappings[]) |
|
|
|
GLBufferToAttribMapping mappings[]) |
|
|
|
{ |
|
|
|
{ |
|
|
|
GLmesh glm = {0}; |
|
|
|
// NOTE: need to use freeGLMesh() when freeing because we're not storing
|
|
|
|
#if 0 |
|
|
|
// the gl_buffers on a memory arean (yet)
|
|
|
|
// TODO: store gl_buffers on gl_ctx
|
|
|
|
GLmesh glm = initGLmesh(m, num_mappings, draw_mode, pos); |
|
|
|
// TODO: loop over mapping array to get buffer attrib info
|
|
|
|
glGenVertexArrays(1, &glm.vao_id); |
|
|
|
WRONG! gl_ctx->num_vertex_buffers = num_mappings; |
|
|
|
glBindVertexArray(glm.vao_id); |
|
|
|
gl_ctx->vertex_buffers = ... |
|
|
|
|
|
|
|
for (u32 i = 0; i < num_mappings; i++) { |
|
|
|
for (u32 i = 0; i < num_mappings; i++) { |
|
|
|
|
|
|
|
// TODO: make a helper function here
|
|
|
|
|
|
|
|
gl_buffer& buf = glm.vertex_attrib_buffers[i]; |
|
|
|
|
|
|
|
GLVertexAttrib* attrib = mappings[i].attrib; |
|
|
|
|
|
|
|
u32 type_size = getGLTypeSize(attrib->data_type); |
|
|
|
|
|
|
|
assert(type_size > 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initGLAttribBuffer(&buf, GL_ARRAY_BUFFER, attrib); |
|
|
|
|
|
|
|
glBindBuffer(buf.target, buf.id); |
|
|
|
|
|
|
|
glBufferData(buf.target, |
|
|
|
|
|
|
|
m.num_vertices * type_size, |
|
|
|
|
|
|
|
mappings[i].mesh_buf, |
|
|
|
|
|
|
|
glm.usage); |
|
|
|
|
|
|
|
glVertexAttribPointer(attrib->location, attrib->num_components, |
|
|
|
|
|
|
|
attrib->component_type, GL_FALSE, 0, 0); |
|
|
|
|
|
|
|
glEnableVertexAttribArray(attrib->location); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
glGenBuffers(1, &glm.element_buf->id); |
|
|
|
|
|
|
|
glm.element_buf->target = GL_ELEMENT_ARRAY_BUFFER; |
|
|
|
|
|
|
|
glm.element_buf->data_type = GL_UNSIGNED_SHORT; |
|
|
|
|
|
|
|
glBindBuffer(glm.element_buf->target, glm.element_buf->id); |
|
|
|
|
|
|
|
glBufferData(glm.element_buf->target, |
|
|
|
|
|
|
|
m.num_indices * sizeof(u16), |
|
|
|
|
|
|
|
m.indices, |
|
|
|
|
|
|
|
glm.usage); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(0); |
|
|
|
|
|
|
|
|
|
|
|
return glm; |
|
|
|
return glm; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
freeGLMesh(GLmesh* glm) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (glm) { |
|
|
|
|
|
|
|
for (u32 i = 0; i < glm->num_vertex_attrib_buffers; i++) |
|
|
|
|
|
|
|
utilSafeFree(glm->vertex_attrib_buffers[i].name); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utilSafeFree(glm->vertex_attrib_buffers); |
|
|
|
|
|
|
|
utilSafeFree(glm->element_buf); |
|
|
|
|
|
|
|
// NOTE: don't free the GLmesh because it is part of a memory arena
|
|
|
|
|
|
|
|
//utilSafeFree(glm);
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NOTE: internal
|
|
|
|
// NOTE: internal
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
bool |
|
|
|
|