From c9541abc4c99bfe42ec9a12bcf1ce97d3c7a61de Mon Sep 17 00:00:00 2001 From: brankoku <48296877+brankoku@users.noreply.github.com> Date: Sat, 9 Nov 2019 23:58:13 -0500 Subject: [PATCH] [rlgl] Update function `rlUpdateMeshAt` --- src/rlgl.h | 92 +++++++++++------------------------------------------- 1 file changed, 18 insertions(+), 74 deletions(-) diff --git a/src/rlgl.h b/src/rlgl.h index 362496be0..5c71c4782 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -2513,72 +2513,7 @@ unsigned int rlLoadAttribBuffer(unsigned int vaoId, int shaderLoc, void *buffer, // Update vertex or index data on GPU (upload new data to one buffer) void rlUpdateMesh(Mesh mesh, int buffer, int num) { -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - // Activate mesh VAO - if (vaoSupported) glBindVertexArray(mesh.vaoId); - - switch (buffer) - { - case 0: // Update vertices (vertex position) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); - if (num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*num, mesh.vertices, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*num, mesh.vertices); - - } break; - case 1: // Update texcoords (vertex texture coordinates) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); - if (num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*num, mesh.texcoords, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*num, mesh.texcoords); - - } break; - case 2: // Update normals (vertex normals) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[2]); - if (num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*num, mesh.normals, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*num, mesh.normals); - - } break; - case 3: // Update colors (vertex colors) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[3]); - if (num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*num, mesh.colors, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*num, mesh.colors); - - } break; - case 4: // Update tangents (vertex tangents) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - if (num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*num, mesh.tangents, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*4*num, mesh.tangents); - } break; - case 5: // Update texcoords2 (vertex second texture coordinates) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[5]); - if (num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*num, mesh.texcoords2, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*num, mesh.texcoords2); - } break; - case 6: // Update indices (triangle index buffer) - { - unsigned short *indices = mesh.indices; - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vboId[6]); - if (num >= mesh.triangleCount*3) // 3 indices per triangle - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices)*num, indices, GL_DYNAMIC_DRAW); - else - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(*indices)*num, indices); - } break; - default: break; - } - - // Unbind the current VAO - if (vaoSupported) glBindVertexArray(0); - - // Another option would be using buffer mapping... - //mesh.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); - // Now we can modify vertices - //glUnmapBuffer(GL_ARRAY_BUFFER); -#endif + rlUpdateMeshAt(mesh, buffer, num, 0); } // Update vertex or index data on GPU, at index @@ -2595,51 +2530,60 @@ void rlUpdateMeshAt(Mesh mesh, int buffer, int num, int index) case 0: // Update vertices (vertex position) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); - if (index + num >= mesh.vertexCount) break; + if (index == 0 && num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*num, mesh.vertices, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.vertexCount) break; else glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*3*index, sizeof(float)*3*num, mesh.vertices); } break; case 1: // Update texcoords (vertex texture coordinates) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); - if (index + num >= mesh.vertexCount) break; + if (index == 0 && num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*num, mesh.texcoords, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.vertexCount) break; else glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*2*index, sizeof(float)*2*num, mesh.texcoords); } break; case 2: // Update normals (vertex normals) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[2]); - if (index + num >= mesh.vertexCount) break; + if (index == 0 && num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*num, mesh.normals, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.vertexCount) break; else glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*3*index, sizeof(float)*3*num, mesh.normals); } break; case 3: // Update colors (vertex colors) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[3]); - if (index + num >= mesh.vertexCount) break; + if (index == 0 && num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*num, mesh.colors, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.vertexCount) break; else glBufferSubData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*index, sizeof(unsigned char)*4*num, mesh.colors); } break; case 4: // Update tangents (vertex tangents) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - if (index + num >= mesh.vertexCount) break; + if (index == 0 && num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*num, mesh.tangents, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.vertexCount) break; else glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*4*index, sizeof(float)*4*num, mesh.tangents); } break; case 5: // Update texcoords2 (vertex second texture coordinates) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[5]); - if (index + num >= mesh.vertexCount) break; + if (index == 0 && num >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*num, mesh.texcoords2, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.vertexCount) break; else glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*2*index, sizeof(float)*2*num, mesh.texcoords2); } break; case 6: // Update indices (triangle index buffer) { + // the * 3 is because each triangle has 3 indices unsigned short *indices = mesh.indices; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vboId[6]); - if (index + num >= mesh.triangleCount*3) // 3 indices per triangle + if (index == 0 && num >= mesh.triangleCount) + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices)*num*3, indices, GL_DYNAMIC_DRAW); + else if (index + num >= mesh.triangleCount) break; else - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices)*index, sizeof(*indices)*num, indices); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices)*index*3, sizeof(*indices)*num*3, indices); } break; default: break; }