diff --git a/src/raylib.h b/src/raylib.h index 206b26573..3113732be 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -347,8 +347,11 @@ typedef struct Mesh { float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) float *tangents; // Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) - unsigned short *indices; // Vertex indices (in case vertex data comes indexed) - +#if defined(GRAPHICS_API_OPENGL_ES2) + unsigned short *indices; // Vertex indices (in case vertex data comes indexed) +#else + unsigned int *indices; +#endif // Animation vertex data float *animVertices; // Animated vertex positions (after bones transformations) float *animNormals; // Animated normals (after bones transformations) diff --git a/src/rlgl.h b/src/rlgl.h index 4b3184986..d00edc68b 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -238,6 +238,13 @@ #define RL_CULL_DISTANCE_FAR 1000.0 // Default far cull distance #endif +// Index buffer params +#if defined(GRAPHICS_API_OPENGL_ES2) +#define RL_INDEX_BUFFER_ELEMENT_TYPE unsigned short +#else +#define RL_INDEX_BUFFER_ELEMENT_TYPE unsigned int +#endif + // Texture parameters (equivalent to OpenGL defines) #define RL_TEXTURE_WRAP_S 0x2802 // GL_TEXTURE_WRAP_S #define RL_TEXTURE_WRAP_T 0x2803 // GL_TEXTURE_WRAP_T @@ -3729,10 +3736,10 @@ void rlDrawVertexArray(int offset, int count) void rlDrawVertexArrayElements(int offset, int count, const void *buffer) { // NOTE: Added pointer math separately from function to avoid UBSAN complaining - unsigned short *bufferPtr = (unsigned short *)buffer; + RL_INDEX_BUFFER_ELEMENT_TYPE *bufferPtr = (RL_INDEX_BUFFER_ELEMENT_TYPE *)buffer; if (offset > 0) bufferPtr += offset; - glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const unsigned short *)bufferPtr); + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const RL_INDEX_BUFFER_ELEMENT_TYPE *)bufferPtr); } // Draw vertex array instanced @@ -3748,10 +3755,10 @@ void rlDrawVertexArrayElementsInstanced(int offset, int count, const void *buffe { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // NOTE: Added pointer math separately from function to avoid UBSAN complaining - unsigned short *bufferPtr = (unsigned short *)buffer; + RL_INDEX_BUFFER_ELEMENT_TYPE *bufferPtr = (RL_INDEX_BUFFER_ELEMENT_TYPE *)buffer; if (offset > 0) bufferPtr += offset; - glDrawElementsInstanced(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const unsigned short *)bufferPtr, instances); + glDrawElementsInstanced(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const RL_INDEX_BUFFER_ELEMENT_TYPE *)bufferPtr, instances); #endif } diff --git a/src/rmodels.c b/src/rmodels.c index 84c7d9cbe..b1fdc9983 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -1278,7 +1278,7 @@ void UploadMesh(Mesh *mesh, bool dynamic) if (mesh->indices != NULL) { - mesh->vboId[6] = rlLoadVertexBufferElement(mesh->indices, mesh->triangleCount*3*sizeof(unsigned short), dynamic); + mesh->vboId[6] = rlLoadVertexBufferElement(mesh->indices, mesh->triangleCount*3*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE), dynamic); } if (mesh->vaoId > 0) TRACELOG(LOG_INFO, "VAO: [ID %i] Mesh uploaded successfully to VRAM (GPU)", mesh->vaoId); @@ -1930,7 +1930,13 @@ bool ExportMeshAsCode(Mesh mesh, const char *fileName) if (mesh.indices != NULL) // Vertex indices (3 index per triangle - unsigned short) { - byteCount += sprintf(txtData + byteCount, "static unsigned short %s_INDEX_DATA[%i] = { ", varFileName, mesh.triangleCount*3); + byteCount += sprintf(txtData + byteCount, +#if defined(GRAPHICS_API_OPENGL_ES2) + "static unsigned short %s_INDEX_DATA[%i] = { ", +#else + "static unsigned int %s_INDEX_DATA[%i] = { ", +#endif + varFileName, mesh.triangleCount*3); for (int i = 0; i < mesh.triangleCount*3 - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "%i,\n" : "%i, "), mesh.indices[i]); byteCount += sprintf(txtData + byteCount, "%i };\n", mesh.indices[mesh.triangleCount*3 - 1]); } @@ -2359,7 +2365,7 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ) mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float)); mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float)); mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float)); - mesh.indices = (unsigned short *)RL_MALLOC(mesh.triangleCount*3*sizeof(unsigned short)); + mesh.indices = (RL_INDEX_BUFFER_ELEMENT_TYPE *)RL_MALLOC(mesh.triangleCount*3*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE)); // Mesh vertices position array for (int i = 0; i < mesh.vertexCount; i++) @@ -2526,7 +2532,7 @@ Mesh GenMeshCube(float width, float height, float length) mesh.normals = (float *)RL_MALLOC(24*3*sizeof(float)); memcpy(mesh.normals, normals, 24*3*sizeof(float)); - mesh.indices = (unsigned short *)RL_MALLOC(36*sizeof(unsigned short)); + mesh.indices = (RL_INDEX_BUFFER_ELEMENT_TYPE *)RL_MALLOC(36*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE)); int k = 0; @@ -4311,7 +4317,7 @@ static Model LoadIQM(const char *fileName) model.meshes[i].boneWeights = RL_CALLOC(model.meshes[i].vertexCount*4, sizeof(float)); // Up-to 4 bones supported! model.meshes[i].triangleCount = imesh[i].num_triangles; - model.meshes[i].indices = RL_CALLOC(model.meshes[i].triangleCount*3, sizeof(unsigned short)); + model.meshes[i].indices = RL_CALLOC(model.meshes[i].triangleCount*3, sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE)); // Animated vertex data, what we actually process for rendering // NOTE: Animated vertex should be re-uploaded to GPU (if not using GPU skinning) @@ -5153,7 +5159,7 @@ static Model LoadGLTF(const char *fileName) if (attribute->component_type == cgltf_component_type_r_16u) { // Init raylib mesh indices to copy glTF attribute data - model.meshes[meshIndex].indices = RL_MALLOC(attribute->count*sizeof(unsigned short)); + model.meshes[meshIndex].indices = RL_MALLOC(attribute->count*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE)); // Load unsigned short data type into mesh.indices LOAD_ATTRIBUTE(attribute, 1, unsigned short, model.meshes[meshIndex].indices) @@ -5161,14 +5167,14 @@ static Model LoadGLTF(const char *fileName) else if (attribute->component_type == cgltf_component_type_r_32u) { // Init raylib mesh indices to copy glTF attribute data - model.meshes[meshIndex].indices = RL_MALLOC(attribute->count*sizeof(unsigned short)); + model.meshes[meshIndex].indices = RL_MALLOC(attribute->count*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE)); // Load data into a temp buffer to be converted to raylib data type unsigned int *temp = RL_MALLOC(attribute->count*sizeof(unsigned int)); LOAD_ATTRIBUTE(attribute, 1, unsigned int, temp); // Convert data to raylib indices data type (unsigned short) - for (unsigned int d = 0; d < attribute->count; d++) model.meshes[meshIndex].indices[d] = (unsigned short)temp[d]; + for (unsigned int d = 0; d < attribute->count; d++) model.meshes[meshIndex].indices[d] = (RL_INDEX_BUFFER_ELEMENT_TYPE)temp[d]; TRACELOG(LOG_WARNING, "MODEL: [%s] Indices data converted from u32 to u16, possible loss of data", fileName); @@ -5617,7 +5623,7 @@ static Model LoadVOX(const char *fileName) memcpy(pmesh->vertices, pvertices, size); // Copy indices - size = voxarray.indices.used*sizeof(unsigned short); + size = voxarray.indices.used*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE); pmesh->indices = RL_MALLOC(size); memcpy(pmesh->indices, pindices, size);