Add RL_INDEX_BUFFER_ELEMENT_TYPE

This commit is contained in:
Denys Maistruk 2023-12-14 16:15:43 -06:00
parent 10c82595b0
commit 178ba7949b
3 changed files with 31 additions and 15 deletions

View File

@ -347,8 +347,11 @@ typedef struct Mesh {
float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
float *tangents; // Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) 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 char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
#if defined(GRAPHICS_API_OPENGL_ES2)
unsigned short *indices; // Vertex indices (in case vertex data comes indexed) unsigned short *indices; // Vertex indices (in case vertex data comes indexed)
#else
unsigned int *indices;
#endif
// Animation vertex data // Animation vertex data
float *animVertices; // Animated vertex positions (after bones transformations) float *animVertices; // Animated vertex positions (after bones transformations)
float *animNormals; // Animated normals (after bones transformations) float *animNormals; // Animated normals (after bones transformations)

View File

@ -238,6 +238,13 @@
#define RL_CULL_DISTANCE_FAR 1000.0 // Default far cull distance #define RL_CULL_DISTANCE_FAR 1000.0 // Default far cull distance
#endif #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) // Texture parameters (equivalent to OpenGL defines)
#define RL_TEXTURE_WRAP_S 0x2802 // GL_TEXTURE_WRAP_S #define RL_TEXTURE_WRAP_S 0x2802 // GL_TEXTURE_WRAP_S
#define RL_TEXTURE_WRAP_T 0x2803 // GL_TEXTURE_WRAP_T #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) void rlDrawVertexArrayElements(int offset, int count, const void *buffer)
{ {
// NOTE: Added pointer math separately from function to avoid UBSAN complaining // 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; 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 // 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) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: Added pointer math separately from function to avoid UBSAN complaining // 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; 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 #endif
} }

View File

@ -1278,7 +1278,7 @@ void UploadMesh(Mesh *mesh, bool dynamic)
if (mesh->indices != NULL) 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); 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) 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]); 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]); 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.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float)); mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*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 // Mesh vertices position array
for (int i = 0; i < mesh.vertexCount; i++) 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)); mesh.normals = (float *)RL_MALLOC(24*3*sizeof(float));
memcpy(mesh.normals, normals, 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; 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].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].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 // Animated vertex data, what we actually process for rendering
// NOTE: Animated vertex should be re-uploaded to GPU (if not using GPU skinning) // 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) if (attribute->component_type == cgltf_component_type_r_16u)
{ {
// Init raylib mesh indices to copy glTF attribute data // 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 unsigned short data type into mesh.indices
LOAD_ATTRIBUTE(attribute, 1, unsigned short, model.meshes[meshIndex].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) else if (attribute->component_type == cgltf_component_type_r_32u)
{ {
// Init raylib mesh indices to copy glTF attribute data // 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 // Load data into a temp buffer to be converted to raylib data type
unsigned int *temp = RL_MALLOC(attribute->count*sizeof(unsigned int)); unsigned int *temp = RL_MALLOC(attribute->count*sizeof(unsigned int));
LOAD_ATTRIBUTE(attribute, 1, unsigned int, temp); LOAD_ATTRIBUTE(attribute, 1, unsigned int, temp);
// Convert data to raylib indices data type (unsigned short) // 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); 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); memcpy(pmesh->vertices, pvertices, size);
// Copy indices // Copy indices
size = voxarray.indices.used*sizeof(unsigned short); size = voxarray.indices.used*sizeof(RL_INDEX_BUFFER_ELEMENT_TYPE);
pmesh->indices = RL_MALLOC(size); pmesh->indices = RL_MALLOC(size);
memcpy(pmesh->indices, pindices, size); memcpy(pmesh->indices, pindices, size);