Browse Source

use uint16_t for element array buffer data

this matches the use in the tinygltf library
main
cinnaboot 5 years ago
parent
commit
d6373b1b41
  1. 6
      src/asset.h
  2. 6
      src/main.cpp
  3. 4
      src/shader.cpp
  4. 1
      src/types.h

6
src/asset.h

@ -58,13 +58,13 @@ struct mesh
{ {
GLenum draw_mode; // NOTE: GL_LINES, GL_TRIANGLES GLenum draw_mode; // NOTE: GL_LINES, GL_TRIANGLES
GLenum usage; // NOTE: GL_STATIC_DRAW, GL_DYNAMIC_DRAW GLenum usage; // NOTE: GL_STATIC_DRAW, GL_DYNAMIC_DRAW
uint num_vertices; u32 num_vertices;
uint num_indices; u32 num_indices;
glm::vec3* vertices; glm::vec3* vertices;
glm::vec3* normals; glm::vec3* normals;
glm::vec2* uvs; glm::vec2* uvs;
glm::vec3* colors; glm::vec3* colors;
uint* indices; u16* indices;
glm::mat4* xform; glm::mat4* xform;
}; };

6
src/main.cpp

@ -55,8 +55,8 @@ initCubeMesh()
m.vertices[7] = { 1, 1, 1 }; m.vertices[7] = { 1, 1, 1 };
m.num_indices = 36; // 6 sides, 2 tris per side, 3 verts per tri m.num_indices = 36; // 6 sides, 2 tris per side, 3 verts per tri
m.indices = (u32*) std::calloc(m.num_indices, sizeof(u32)); m.indices = (u16*) std::calloc(m.num_indices, sizeof(u16));
u32 indices[36] = { u16 indices[36] = {
0, 1, 2, 0, 2, 3, 0, 1, 2, 0, 2, 3,
3, 2, 6, 3, 6, 7, 3, 2, 6, 3, 6, 7,
7, 6, 5, 7, 5, 4, 7, 6, 5, 7, 5, 4,
@ -64,7 +64,7 @@ initCubeMesh()
0, 3, 4, 0, 3, 7, 0, 3, 4, 0, 3, 7,
1, 2, 5, 2, 6, 5 1, 2, 5, 2, 6, 5
}; };
std::memcpy(m.indices, indices, m.num_indices * sizeof(u32)); std::memcpy(m.indices, indices, m.num_indices * sizeof(u16));
return m; return m;
} }

4
src/shader.cpp

@ -117,7 +117,7 @@ renderVAO(GLmesh* glmesh)
(float*) glmesh->model_xform); (float*) glmesh->model_xform);
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_INT, 0); glmesh->draw_mode, glmesh->num_indices, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0); glBindVertexArray(0);
} }
@ -379,7 +379,7 @@ loadGLMesh(shader_program* s,
glGenBuffers(1, &gm.idx_buf_id); glGenBuffers(1, &gm.idx_buf_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gm.idx_buf_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gm.idx_buf_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, glBufferData(GL_ELEMENT_ARRAY_BUFFER,
m.num_indices * sizeof(GLuint), m.num_indices * sizeof(u16),
m.indices, m.indices,
GL_STATIC_DRAW); GL_STATIC_DRAW);

1
src/types.h

@ -3,6 +3,7 @@
typedef uint8_t u8; typedef uint8_t u8;
typedef int32_t i32; typedef int32_t i32;
typedef uint16_t u16;
typedef uint32_t u32; typedef uint32_t u32;
typedef uint64_t u64; typedef uint64_t u64;

Loading…
Cancel
Save