|
|
|
@ -122,25 +122,38 @@ bool |
|
|
|
rgInitEntity(gl_render_group* rg, Entity* e) |
|
|
|
rgInitEntity(gl_render_group* rg, Entity* e) |
|
|
|
{ |
|
|
|
{ |
|
|
|
bool retVal = true; |
|
|
|
bool retVal = true; |
|
|
|
|
|
|
|
bool useNormals = rg->use_normals = (e->mesh->normals != nullptr); |
|
|
|
|
|
|
|
|
|
|
|
uint entity_buf_len = e->mesh->num_vertices * 3; |
|
|
|
uint vertex_buf_len = e->mesh->num_vertices * 3; |
|
|
|
GLfloat* entity_buf = (GLfloat*) std::calloc(entity_buf_len, sizeof(GLfloat)); |
|
|
|
GLfloat* vertex_buf = (GLfloat*) std::calloc(vertex_buf_len, sizeof(GLfloat)); |
|
|
|
GLfloat* entity_color_buf = (GLfloat*) std::calloc(entity_buf_len, sizeof(GLfloat)); |
|
|
|
GLfloat* color_buf = (GLfloat*) std::calloc(vertex_buf_len, sizeof(GLfloat)); |
|
|
|
|
|
|
|
GLfloat* normal_buf; |
|
|
|
|
|
|
|
if (useNormals) |
|
|
|
|
|
|
|
normal_buf = (GLfloat*) std::calloc(vertex_buf_len, sizeof(GLfloat)); |
|
|
|
|
|
|
|
|
|
|
|
// dump vertices into temporary buffer
|
|
|
|
// dump vertices, colors, and normals into temporary buffers
|
|
|
|
uint vertex_index = 0; |
|
|
|
uint vertex_index = 0; |
|
|
|
uint vertex_prop_index = 0; |
|
|
|
uint vertex_prop_index = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < entity_buf_len; i++) { |
|
|
|
for (uint i = 0; i < vertex_buf_len; i++) { |
|
|
|
const glm::vec3& vertex = e->mesh->vertices[vertex_index]; |
|
|
|
const glm::vec3& vertex = e->mesh->vertices[vertex_index]; |
|
|
|
|
|
|
|
const glm::vec3& normal = e->mesh->normals[vertex_index]; |
|
|
|
|
|
|
|
|
|
|
|
switch (vertex_prop_index) { |
|
|
|
switch (vertex_prop_index) { |
|
|
|
case 0: entity_buf[i] = vertex.x; break; |
|
|
|
case 0: vertex_buf[i] = vertex.x; break; |
|
|
|
case 1: entity_buf[i] = vertex.y; break; |
|
|
|
case 1: vertex_buf[i] = vertex.y; break; |
|
|
|
case 2: entity_buf[i] = vertex.z; break; |
|
|
|
case 2: vertex_buf[i] = vertex.z; break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
entity_color_buf[i] = e->mesh->diffuse_color[vertex_prop_index]; |
|
|
|
if (useNormals) { |
|
|
|
|
|
|
|
switch (vertex_prop_index) { |
|
|
|
|
|
|
|
case 0: normal_buf[i] = normal.x; break; |
|
|
|
|
|
|
|
case 1: normal_buf[i] = normal.y; break; |
|
|
|
|
|
|
|
case 2: normal_buf[i] = normal.z; break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
color_buf[i] = e->mesh->diffuse_color[vertex_prop_index]; |
|
|
|
vertex_prop_index++; |
|
|
|
vertex_prop_index++; |
|
|
|
|
|
|
|
|
|
|
|
if (vertex_prop_index == 3) { |
|
|
|
if (vertex_prop_index == 3) { |
|
|
|
@ -149,12 +162,19 @@ rgInitEntity(gl_render_group* rg, Entity* e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rgInitGLBufferObject(&rg->vertex_buffer, entity_buf_len, |
|
|
|
// send vertex and color data to OpenGL
|
|
|
|
GL_DYNAMIC_DRAW, GL_ARRAY_BUFFER, entity_buf); |
|
|
|
rgInitGLBufferObject(&rg->vertex_buffer, vertex_buf_len, |
|
|
|
|
|
|
|
GL_DYNAMIC_DRAW, GL_ARRAY_BUFFER, vertex_buf); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rgInitGLBufferObject(&rg->color_buffer, vertex_buf_len, |
|
|
|
|
|
|
|
GL_STATIC_DRAW, GL_ARRAY_BUFFER, color_buf); |
|
|
|
|
|
|
|
|
|
|
|
rgInitGLBufferObject(&rg->color_buffer, entity_buf_len, |
|
|
|
if (useNormals) { |
|
|
|
GL_STATIC_DRAW, GL_ARRAY_BUFFER, entity_color_buf); |
|
|
|
rgInitGLBufferObject(&rg->vertex_normals, vertex_buf_len, |
|
|
|
|
|
|
|
GL_STATIC_DRAW, GL_ARRAY_BUFFER, normal_buf); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: combine this somehow with rgInitGLBufferObject()
|
|
|
|
// dump indices into temp buffer
|
|
|
|
// dump indices into temp buffer
|
|
|
|
gl_index_buffer index_buffer; |
|
|
|
gl_index_buffer index_buffer; |
|
|
|
index_buffer.buffer_len = e->mesh->num_indices; |
|
|
|
index_buffer.buffer_len = e->mesh->num_indices; |
|
|
|
@ -170,9 +190,11 @@ rgInitEntity(gl_render_group* rg, Entity* e) |
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->mesh->num_indices * sizeof(uint), |
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->mesh->num_indices * sizeof(uint), |
|
|
|
rg->index_buffer.indices, GL_STATIC_DRAW); |
|
|
|
rg->index_buffer.indices, GL_STATIC_DRAW); |
|
|
|
|
|
|
|
|
|
|
|
std::free(entity_buf); |
|
|
|
// free temp buffers
|
|
|
|
std::free(entity_color_buf); |
|
|
|
std::free(vertex_buf); |
|
|
|
entity_buf = entity_color_buf = nullptr; |
|
|
|
if (useNormals) std::free(normal_buf); |
|
|
|
|
|
|
|
std::free(color_buf); |
|
|
|
|
|
|
|
vertex_buf = normal_buf = color_buf = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
return retVal; |
|
|
|
return retVal; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -240,6 +262,13 @@ rgDrawIndexed(gl_render_group* rg, GLenum draw_mode, glm::mat4 model_matrix, |
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); |
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3rd attribe buffer : normals
|
|
|
|
|
|
|
|
if (rg->use_normals) { |
|
|
|
|
|
|
|
glEnableVertexAttribArray(2); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, rg->vertex_normals.buffer_id); |
|
|
|
|
|
|
|
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rg->index_buffer.buffer_id); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rg->index_buffer.buffer_id); |
|
|
|
|
|
|
|
|
|
|
|
// draw
|
|
|
|
// draw
|
|
|
|
@ -250,6 +279,8 @@ rgDrawIndexed(gl_render_group* rg, GLenum draw_mode, glm::mat4 model_matrix, |
|
|
|
glDisableVertexAttribArray(0); |
|
|
|
glDisableVertexAttribArray(0); |
|
|
|
if (rg->color_buffer.buffer) |
|
|
|
if (rg->color_buffer.buffer) |
|
|
|
glDisableVertexAttribArray(1); |
|
|
|
glDisableVertexAttribArray(1); |
|
|
|
|
|
|
|
if (rg->use_normals) |
|
|
|
|
|
|
|
glDisableVertexAttribArray(2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
|