GenMeshTangents Fix

This commit is contained in:
Jason Penick 2021-07-16 13:16:34 -05:00
parent 56cab4b6a9
commit b3b4376269

View File

@ -2652,8 +2652,30 @@ BoundingBox GetMeshBoundingBox(Mesh mesh)
// Implementation base don: https://answers.unity.com/questions/7789/calculating-tangents-vector4.html
void GenMeshTangents(Mesh* mesh)
{
if (mesh->tangents == NULL) mesh->tangents = (float *)RL_MALLOC(mesh->vertexCount*4*sizeof(float));
else TRACELOG(LOG_WARNING, "MESH: Tangents data already available, re-writting");
/*
* Added - 7/16/2021 - Jason Penick
* Check and see if the mesh already has tangents.
* If the mesh does not have tangents we can simply alloate the space for the data.
*
* If the mesh did have tangents, we can not readily depend on the current vertexCount
* to be the actual length of the existing tangent data as the mesh vertices could
* have been altered and now contain less or more vertices which would result in
* either updating to few tangents, or attempting to over-run the space allocated
* for the tangents previously. In order to account for this the memory for the
* current tangent data must be freed as well as the previously assigned VertexBuffer,
* and then we can recreate the tangent data with the new size designated by vertexCount
* and create a new VertexBuffer to assign for the tangents.
*
*/
if (mesh->tangents == NULL)
{
mesh->tangents = (float*)RL_MALLOC(mesh->vertexCount * 4 * sizeof(float));
}
else {
rlUnloadVertexBuffer(mesh->vboId[SHADER_LOC_VERTEX_TANGENT]);
RL_FREE(mesh->tangents);
mesh->tangents = (float*)RL_MALLOC(mesh->vertexCount * 4 * sizeof(float));
}
Vector3* tan1 = (Vector3*)RL_MALLOC(mesh->vertexCount * sizeof(Vector3));
Vector3* tan2 = (Vector3*)RL_MALLOC(mesh->vertexCount * sizeof(Vector3));
@ -2726,6 +2748,16 @@ void GenMeshTangents(Mesh *mesh)
// Load a new tangent attributes buffer
mesh->vboId[SHADER_LOC_VERTEX_TANGENT] = rlLoadVertexBuffer(mesh->tangents, mesh->vertexCount * 4 * sizeof(float), false);
/*
* Added - 7/16/2021 - Jason Penick
* Upate the tangets on the GPU.
* Issue raised by Kenneth on discord.
*/
rlEnableVertexArray(mesh->vaoId);
rlSetVertexAttribute(4, 4, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(4);
rlDisableVertexArray();
TRACELOG(LOG_INFO, "MESH: Tangents data computed for provided mesh");
}