From d6373b1b412668bdee17675a67c7e56c93c870e3 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 10 Dec 2021 14:00:59 -0500 Subject: [PATCH] use uint16_t for element array buffer data this matches the use in the tinygltf library --- src/asset.h | 6 +++--- src/main.cpp | 6 +++--- src/shader.cpp | 4 ++-- src/types.h | 1 + 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/asset.h b/src/asset.h index 891b055..34a2ebe 100644 --- a/src/asset.h +++ b/src/asset.h @@ -58,13 +58,13 @@ struct mesh { GLenum draw_mode; // NOTE: GL_LINES, GL_TRIANGLES GLenum usage; // NOTE: GL_STATIC_DRAW, GL_DYNAMIC_DRAW - uint num_vertices; - uint num_indices; + u32 num_vertices; + u32 num_indices; glm::vec3* vertices; glm::vec3* normals; glm::vec2* uvs; glm::vec3* colors; - uint* indices; + u16* indices; glm::mat4* xform; }; diff --git a/src/main.cpp b/src/main.cpp index 34a86cc..5a82918 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,8 +55,8 @@ initCubeMesh() m.vertices[7] = { 1, 1, 1 }; m.num_indices = 36; // 6 sides, 2 tris per side, 3 verts per tri - m.indices = (u32*) std::calloc(m.num_indices, sizeof(u32)); - u32 indices[36] = { + m.indices = (u16*) std::calloc(m.num_indices, sizeof(u16)); + u16 indices[36] = { 0, 1, 2, 0, 2, 3, 3, 2, 6, 3, 6, 7, 7, 6, 5, 7, 5, 4, @@ -64,7 +64,7 @@ initCubeMesh() 0, 3, 4, 0, 3, 7, 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; } diff --git a/src/shader.cpp b/src/shader.cpp index 707b0a0..72579fa 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -117,7 +117,7 @@ renderVAO(GLmesh* glmesh) (float*) glmesh->model_xform); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glmesh->idx_buf_id); glDrawElements( - glmesh->draw_mode, glmesh->num_indices, GL_UNSIGNED_INT, 0); + glmesh->draw_mode, glmesh->num_indices, GL_UNSIGNED_SHORT, 0); glBindVertexArray(0); } @@ -379,7 +379,7 @@ loadGLMesh(shader_program* s, glGenBuffers(1, &gm.idx_buf_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gm.idx_buf_id); glBufferData(GL_ELEMENT_ARRAY_BUFFER, - m.num_indices * sizeof(GLuint), + m.num_indices * sizeof(u16), m.indices, GL_STATIC_DRAW); diff --git a/src/types.h b/src/types.h index 647483f..4b893a7 100644 --- a/src/types.h +++ b/src/types.h @@ -3,6 +3,7 @@ typedef uint8_t u8; typedef int32_t i32; +typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64;