|
|
|
@ -70,24 +70,8 @@ rgInitShaderProgram(gl_render_group* rg, const char * vertex_code, const char * |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NOTE: this will only work after a call to glBindBuffer
|
|
|
|
void |
|
|
|
bool |
|
|
|
rgInitGLBufferObject(gl_buffer* buf_obj, int len, GLenum usage, GLenum target, GLfloat data[]) |
|
|
|
checkGLBufferSize(GLenum buf_type, int expected_size, int line_num) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
GLint gl_size; |
|
|
|
|
|
|
|
glGetBufferParameteriv(buf_type, GL_BUFFER_SIZE ,&gl_size); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (expected_size != gl_size) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LOG(ERROR) << "line: " << line_num << "gl buffer size mismatch\n"; |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
rgInitGLBufferObject(gl_buffer* buf_obj, int len, GLenum usage, GLfloat data[]) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
buf_obj->buffer_len = (size_t) len; |
|
|
|
buf_obj->buffer_len = (size_t) len; |
|
|
|
buf_obj->buffer = (GLfloat*) std::calloc(len, sizeof(GLfloat)); |
|
|
|
buf_obj->buffer = (GLfloat*) std::calloc(len, sizeof(GLfloat)); |
|
|
|
@ -99,13 +83,8 @@ rgInitGLBufferObject(gl_buffer* buf_obj, int len, GLenum usage, GLfloat data[]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
glGenBuffers(1, &buf_obj->buffer_id); |
|
|
|
glGenBuffers(1, &buf_obj->buffer_id); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buf_obj->buffer_id); |
|
|
|
glBindBuffer(target, buf_obj->buffer_id); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, len * sizeof(GLfloat), buf_obj->buffer, usage); |
|
|
|
glBufferData(target, len * sizeof(GLfloat), buf_obj->buffer, usage); |
|
|
|
|
|
|
|
|
|
|
|
if (!checkGLBufferSize(GL_ARRAY_BUFFER, len * sizeof(GLfloat), __LINE__)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NOTE: helper for fillColorBuffer() to convert uint32 color to GLfloat triplet
|
|
|
|
// NOTE: helper for fillColorBuffer() to convert uint32 color to GLfloat triplet
|
|
|
|
@ -176,20 +155,17 @@ rgInitEntity(gl_render_group* rg, Entity* e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if 1 |
|
|
|
#if 1 |
|
|
|
if (!rgInitGLBufferObject(&rg->vertex_buffer, entity_buf_len, |
|
|
|
rgInitGLBufferObject(&rg->vertex_buffer, entity_buf_len, |
|
|
|
GL_DYNAMIC_DRAW, entity_buf)) |
|
|
|
GL_DYNAMIC_DRAW, GL_ARRAY_BUFFER, entity_buf); |
|
|
|
retVal = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!rgInitGLBufferObject(&rg->color_buffer, entity_buf_len, |
|
|
|
rgInitGLBufferObject(&rg->color_buffer, entity_buf_len, |
|
|
|
GL_STATIC_DRAW, entity_color_buf)) |
|
|
|
GL_STATIC_DRAW, GL_ARRAY_BUFFER, entity_color_buf); |
|
|
|
retVal = false; |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
// TODO: testing indexed draw
|
|
|
|
// TODO: testing indexed draw
|
|
|
|
|
|
|
|
|
|
|
|
gl_buffer index_buffer; |
|
|
|
gl_index_buffer index_buffer; |
|
|
|
index_buffer.num_indices = e->num_indices; |
|
|
|
index_buffer.buffer_len = e->num_indices; |
|
|
|
// TODO: there's no associated call to free this alloc until we remake
|
|
|
|
|
|
|
|
index_buffer.indices = (uint *) std::calloc(e->num_indices, sizeof(uint)); |
|
|
|
index_buffer.indices = (uint *) std::calloc(e->num_indices, sizeof(uint)); |
|
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < e->num_indices; i++) |
|
|
|
for (uint i = 0; i < e->num_indices; i++) |
|
|
|
@ -197,6 +173,13 @@ rgInitEntity(gl_render_group* rg, Entity* e) |
|
|
|
|
|
|
|
|
|
|
|
rg->index_buffer = index_buffer; |
|
|
|
rg->index_buffer = index_buffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glGenBuffers(1, &rg->index_buffer.buffer_id); |
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rg->index_buffer.buffer_id); |
|
|
|
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->num_indices * sizeof(uint), |
|
|
|
|
|
|
|
rg->index_buffer.indices, GL_STATIC_DRAW); |
|
|
|
|
|
|
|
//rgInitGLBufferObject(&rg->index_buffer, rg->index_buffer.num_indices,
|
|
|
|
|
|
|
|
// GL_STATIC_DRAW, GL_ELEMENT_ARRAY_BUFFER, rg->index_buffer.indices);
|
|
|
|
|
|
|
|
|
|
|
|
//////////
|
|
|
|
//////////
|
|
|
|
|
|
|
|
|
|
|
|
std::free(entity_buf); |
|
|
|
std::free(entity_buf); |
|
|
|
@ -246,28 +229,57 @@ rgDraw(gl_render_group* rg, GLenum draw_mode, glm::mat4 model_matrix, |
|
|
|
glDisableVertexAttribArray(1); |
|
|
|
glDisableVertexAttribArray(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: testing indexed drawing
|
|
|
|
void |
|
|
|
void |
|
|
|
rgDrawIndexed(gl_render_group* rg, GLenum draw_mode, glm::mat4 model_matrix, |
|
|
|
rgDrawIndexed(gl_render_group* rg, GLenum draw_mode, glm::mat4 model_matrix, |
|
|
|
glm::mat4 view_matrix, glm::mat4 projection_matrix, bool update_vertex_data) |
|
|
|
glm::mat4 view_matrix, glm::mat4 projection_matrix, bool update_vertex_data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
//
|
|
|
|
glUseProgram(rg->program_id); |
|
|
|
|
|
|
|
glUniformMatrix4fv(rg->model_matrix_id, 1, GL_FALSE, &model_matrix[0][0]); |
|
|
|
|
|
|
|
glUniformMatrix4fv(rg->view_matrix_id, 1, GL_FALSE, &view_matrix[0][0]); |
|
|
|
|
|
|
|
glUniformMatrix4fv(rg->projection_matrix_id, 1, GL_FALSE, &projection_matrix[0][0]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1st attribute buffer : vertices
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, rg->vertex_buffer.buffer_id); |
|
|
|
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2nd attribute buffer : colors
|
|
|
|
|
|
|
|
if (rg->color_buffer.buffer) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, rg->color_buffer.buffer_id); |
|
|
|
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rg->index_buffer.buffer_id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// draw
|
|
|
|
|
|
|
|
//glDrawArrays(draw_mode, 0, rg->vertex_buffer.buffer_len / 3);
|
|
|
|
|
|
|
|
glDrawElements(draw_mode, rg->index_buffer.buffer_len, GL_UNSIGNED_INT, 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(0); |
|
|
|
|
|
|
|
if (rg->color_buffer.buffer) |
|
|
|
|
|
|
|
glDisableVertexAttribArray(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
rgFree(gl_render_group* rg) |
|
|
|
rgFree(gl_render_group* rg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
assert(rg != nullptr); |
|
|
|
assert(rg != nullptr); |
|
|
|
gl_buffer bufs[] = {rg->vertex_buffer, rg->color_buffer, rg->index_buffer}; |
|
|
|
gl_buffer bufs[] = {rg->vertex_buffer, rg->color_buffer}; |
|
|
|
for (uint i = 0; i < 3; i++) { |
|
|
|
for (uint i = 0; i < 2; i++) { |
|
|
|
gl_buffer& buf = bufs[i]; |
|
|
|
gl_buffer& buf = bufs[i]; |
|
|
|
if (buf.buffer != nullptr) { |
|
|
|
if (buf.buffer != nullptr) { |
|
|
|
std::free(buf.buffer); |
|
|
|
std::free(buf.buffer); |
|
|
|
buf.buffer = nullptr; |
|
|
|
buf.buffer = nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
if (buf.indices != nullptr) { |
|
|
|
|
|
|
|
std::free(buf.indices); |
|
|
|
|
|
|
|
buf.indices = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rg->index_buffer.indices != nullptr) { |
|
|
|
|
|
|
|
std::free(rg->index_buffer.indices); |
|
|
|
|
|
|
|
rg->index_buffer.indices = nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (rg->entities != nullptr) { |
|
|
|
if (rg->entities != nullptr) { |
|
|
|
|