use bone weights for animation (#1728)
This commit is contained in:
parent
581bd0eb02
commit
1c45a882f7
1 changed files with 39 additions and 27 deletions
66
src/models.c
66
src/models.c
|
@ -1425,41 +1425,53 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
||||||
int vCounter = 0;
|
int vCounter = 0;
|
||||||
int boneCounter = 0;
|
int boneCounter = 0;
|
||||||
int boneId = 0;
|
int boneId = 0;
|
||||||
|
float boneWeight = 0.0;
|
||||||
|
|
||||||
for (int i = 0; i < model.meshes[m].vertexCount; i++)
|
for (int i = 0; i < model.meshes[m].vertexCount; i++)
|
||||||
{
|
{
|
||||||
boneId = model.meshes[m].boneIds[boneCounter];
|
model.meshes[m].animVertices[vCounter] = 0;
|
||||||
inTranslation = model.bindPose[boneId].translation;
|
model.meshes[m].animVertices[vCounter + 1] = 0;
|
||||||
inRotation = model.bindPose[boneId].rotation;
|
model.meshes[m].animVertices[vCounter + 2] = 0;
|
||||||
//inScale = model.bindPose[boneId].scale;
|
|
||||||
outTranslation = anim.framePoses[frame][boneId].translation;
|
|
||||||
outRotation = anim.framePoses[frame][boneId].rotation;
|
|
||||||
outScale = anim.framePoses[frame][boneId].scale;
|
|
||||||
|
|
||||||
// Vertices processing
|
model.meshes[m].animNormals[vCounter] = 0;
|
||||||
// NOTE: We use meshes.vertices (default vertex position) to calculate meshes.animVertices (animated vertex position)
|
model.meshes[m].animNormals[vCounter + 1] = 0;
|
||||||
animVertex = (Vector3){ model.meshes[m].vertices[vCounter], model.meshes[m].vertices[vCounter + 1], model.meshes[m].vertices[vCounter + 2] };
|
model.meshes[m].animNormals[vCounter + 2] = 0;
|
||||||
animVertex = Vector3Multiply(animVertex, outScale);
|
|
||||||
animVertex = Vector3Subtract(animVertex, inTranslation);
|
|
||||||
animVertex = Vector3RotateByQuaternion(animVertex, QuaternionMultiply(outRotation, QuaternionInvert(inRotation)));
|
|
||||||
animVertex = Vector3Add(animVertex, outTranslation);
|
|
||||||
model.meshes[m].animVertices[vCounter] = animVertex.x;
|
|
||||||
model.meshes[m].animVertices[vCounter + 1] = animVertex.y;
|
|
||||||
model.meshes[m].animVertices[vCounter + 2] = animVertex.z;
|
|
||||||
|
|
||||||
// Normals processing
|
for (int j = 0; j < 4; j++)
|
||||||
// NOTE: We use meshes.baseNormals (default normal) to calculate meshes.normals (animated normals)
|
|
||||||
if (model.meshes[m].normals != NULL)
|
|
||||||
{
|
{
|
||||||
animNormal = (Vector3){ model.meshes[m].normals[vCounter], model.meshes[m].normals[vCounter + 1], model.meshes[m].normals[vCounter + 2] };
|
boneId = model.meshes[m].boneIds[boneCounter];
|
||||||
animNormal = Vector3RotateByQuaternion(animNormal, QuaternionMultiply(outRotation, QuaternionInvert(inRotation)));
|
boneWeight = model.meshes[m].boneWeights[boneCounter];
|
||||||
model.meshes[m].animNormals[vCounter] = animNormal.x;
|
inTranslation = model.bindPose[boneId].translation;
|
||||||
model.meshes[m].animNormals[vCounter + 1] = animNormal.y;
|
inRotation = model.bindPose[boneId].rotation;
|
||||||
model.meshes[m].animNormals[vCounter + 2] = animNormal.z;
|
//inScale = model.bindPose[boneId].scale;
|
||||||
}
|
outTranslation = anim.framePoses[frame][boneId].translation;
|
||||||
|
outRotation = anim.framePoses[frame][boneId].rotation;
|
||||||
|
outScale = anim.framePoses[frame][boneId].scale;
|
||||||
|
|
||||||
|
// Vertices processing
|
||||||
|
// NOTE: We use meshes.vertices (default vertex position) to calculate meshes.animVertices (animated vertex position)
|
||||||
|
animVertex = (Vector3){ model.meshes[m].vertices[vCounter], model.meshes[m].vertices[vCounter + 1], model.meshes[m].vertices[vCounter + 2] };
|
||||||
|
animVertex = Vector3Multiply(animVertex, outScale);
|
||||||
|
animVertex = Vector3Subtract(animVertex, inTranslation);
|
||||||
|
animVertex = Vector3RotateByQuaternion(animVertex, QuaternionMultiply(outRotation, QuaternionInvert(inRotation)));
|
||||||
|
animVertex = Vector3Add(animVertex, outTranslation);
|
||||||
|
model.meshes[m].animVertices[vCounter] += animVertex.x * boneWeight;
|
||||||
|
model.meshes[m].animVertices[vCounter + 1] += animVertex.y * boneWeight;
|
||||||
|
model.meshes[m].animVertices[vCounter + 2] += animVertex.z * boneWeight;
|
||||||
|
|
||||||
|
// Normals processing
|
||||||
|
// NOTE: We use meshes.baseNormals (default normal) to calculate meshes.normals (animated normals)
|
||||||
|
if (model.meshes[m].normals != NULL)
|
||||||
|
{
|
||||||
|
animNormal = (Vector3){ model.meshes[m].normals[vCounter], model.meshes[m].normals[vCounter + 1], model.meshes[m].normals[vCounter + 2] };
|
||||||
|
animNormal = Vector3RotateByQuaternion(animNormal, QuaternionMultiply(outRotation, QuaternionInvert(inRotation)));
|
||||||
|
model.meshes[m].animNormals[vCounter] += animNormal.x * boneWeight;
|
||||||
|
model.meshes[m].animNormals[vCounter + 1] += animNormal.y * boneWeight;
|
||||||
|
model.meshes[m].animNormals[vCounter + 2] += animNormal.z * boneWeight;
|
||||||
|
}
|
||||||
|
boneCounter += 1;
|
||||||
|
}
|
||||||
vCounter += 3;
|
vCounter += 3;
|
||||||
boneCounter += 4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload new vertex data to GPU for model drawing
|
// Upload new vertex data to GPU for model drawing
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue