/* Copyright (c) 2018 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "gamemath/ArmaturePose.h" #include "utilities/IOUtilities.h" #include #include #include #define stemobject_implementation ArmaturePose stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); ArmaturePose * ArmaturePose_create(unsigned int poseBoneCount, ArmaturePoseBone * poseBones) { stemobject_create_implementation(init, poseBoneCount, poseBones) } ArmaturePose * ArmaturePose_createWithArmature(Armature * armature) { stemobject_create_implementation(initWithArmature, armature) } bool ArmaturePose_init(ArmaturePose * self, unsigned int poseBoneCount, ArmaturePoseBone * poseBones) { call_super(init, self); self->poseBoneCount = poseBoneCount; self->poseBones = memdup(poseBones, poseBoneCount * sizeof(*poseBones)); self->poseTransformDirtyValue = 0; self->poseBoneTransformCount = 0; self->poseBoneTransforms = NULL; return true; } static void copyArmatureBoneInfo(ArmaturePose * self, Armature * armature) { for (unsigned int boneIndex = 0; boneIndex < armature->boneCount; boneIndex++) { self->poseBones[boneIndex].boneID = armature->bones[boneIndex].boneID; self->poseBones[boneIndex].offset = VECTOR3f_ZERO; self->poseBones[boneIndex].scale = VECTOR3f(1.0f, 1.0f, 1.0f); self->poseBones[boneIndex].rotation = QUATERNIONf_IDENTITY; self->poseBones[boneIndex].armatureBoneIndex = boneIndex; self->poseBones[boneIndex].parentPoseBoneIndex = armature->bones[boneIndex].parentIndex; } } bool ArmaturePose_initWithArmature(ArmaturePose * self, Armature * armature) { call_super(init, self); self->poseBoneCount = armature->boneCount; if (armature->boneCount == 0) { self->poseBones = NULL; } else { self->poseBones = malloc(armature->boneCount * sizeof(*self->poseBones)); copyArmatureBoneInfo(self, armature); } self->poseTransformDirtyValue = 0; self->poseBoneTransformCount = 0; self->poseBoneTransforms = NULL; return true; } void ArmaturePose_initCopy(ArmaturePose * self, ArmaturePose * original) { call_super(init, self); self->poseBoneCount = original->poseBoneCount; self->poseBones = memdup(original->poseBones, self->poseBoneCount * sizeof(*self->poseBones)); self->poseTransformDirtyValue = original->poseTransformDirtyValue; self->poseBoneTransformCount = original->poseBoneTransformCount; self->poseBoneTransforms = memdup(original->poseBoneTransforms, self->poseBoneTransformCount * sizeof(*self->poseBoneTransforms)); } void ArmaturePose_dispose(ArmaturePose * self) { free(self->poseBones); free(self->poseBoneTransforms); call_super(dispose, self); } ArmaturePose * ArmaturePose_copy(ArmaturePose * self) { stemobject_copy_implementation(initCopy) } void ArmaturePose_resetAllPoseBones(ArmaturePose * self) { for (unsigned int poseBoneIndex = 0; poseBoneIndex < self->poseBoneCount; poseBoneIndex++) { self->poseBones[poseBoneIndex].offset = VECTOR3f_ZERO; self->poseBones[poseBoneIndex].scale = V3f(1.0f, 1.0f, 1.0f); self->poseBones[poseBoneIndex].rotation = QUATERNIONf_IDENTITY; } } void ArmaturePose_computePoseBoneTransforms(ArmaturePose * self, Armature * armature) { if (self->poseBoneCount > self->poseBoneTransformCount) { self->poseBoneTransforms = realloc(self->poseBoneTransforms, self->poseBoneCount * sizeof(*self->poseBoneTransforms)); } self->poseBoneTransformCount = self->poseBoneCount; for (unsigned int poseBoneIndex = 0; poseBoneIndex < self->poseBoneCount; poseBoneIndex++) { Matrix4x4f matrix; unsigned int armatureBoneIndex = self->poseBones[poseBoneIndex].armatureBoneIndex; if (self->poseBones[poseBoneIndex].parentPoseBoneIndex == BONE_INDEX_NOT_FOUND) { matrix = MATRIX4x4f_IDENTITY; } else { matrix = self->poseBoneTransforms[self->poseBones[poseBoneIndex].parentPoseBoneIndex]; } Matrix4x4f_translate(&matrix, self->poseBones[poseBoneIndex].offset.x, self->poseBones[poseBoneIndex].offset.y, self->poseBones[poseBoneIndex].offset.z); Matrix4x4f_translate(&matrix, armature->bones[armatureBoneIndex].position.x, armature->bones[armatureBoneIndex].position.y, armature->bones[armatureBoneIndex].position.z); Matrix4x4f_scale(&matrix, self->poseBones[poseBoneIndex].scale.x, self->poseBones[poseBoneIndex].scale.y, self->poseBones[poseBoneIndex].scale.z); Quaternionf boneRotation = Quaternionf_multiplied(Quaternionf_multiplied(armature->bones[armatureBoneIndex].baseOrientation, self->poseBones[poseBoneIndex].rotation), Quaternionf_inverted(armature->bones[poseBoneIndex].baseOrientation)); Matrix4x4f_multiply(&matrix, Quaternionf_toMatrix(boneRotation)); Matrix4x4f_translate(&matrix, -armature->bones[armatureBoneIndex].position.x, -armature->bones[armatureBoneIndex].position.y, -armature->bones[armatureBoneIndex].position.z); self->poseBoneTransforms[poseBoneIndex] = matrix; } self->poseTransformDirtyValue++; } Vector3f ArmaturePose_getPoseBonePosition(ArmaturePose * self, unsigned int poseBoneIndex, Armature * armature) { assert(poseBoneIndex < self->poseBoneCount); return Matrix4x4f_multiplyVector3f(self->poseBoneTransforms[poseBoneIndex], armature->bones[self->poseBones[poseBoneIndex].armatureBoneIndex].position); } Vector3f ArmaturePose_getPoseBoneEndpoint(ArmaturePose * self, unsigned int poseBoneIndex, Armature * armature) { assert(poseBoneIndex < self->poseBoneCount); return Matrix4x4f_multiplyVector3f(self->poseBoneTransforms[poseBoneIndex], armature->bones[self->poseBones[poseBoneIndex].armatureBoneIndex].endpoint); } void ArmaturePose_combinePoses(ArmaturePose * self, ArmaturePose * pose) { for (unsigned int poseBoneIndex = 0; poseBoneIndex < pose->poseBoneCount; poseBoneIndex++) { ArmaturePoseBone * toPoseBone = ArmaturePose_getPoseBone(self, pose->poseBones[poseBoneIndex].boneID); if (toPoseBone != NULL) { toPoseBone->offset = Vector3f_add(toPoseBone->offset, pose->poseBones[poseBoneIndex].offset); toPoseBone->scale = Vector3f_multiplyComponents(toPoseBone->scale, pose->poseBones[poseBoneIndex].scale); toPoseBone->rotation = Quaternionf_multiplied(toPoseBone->rotation, pose->poseBones[poseBoneIndex].rotation); } } } bool ArmaturePose_combinePoseBones(ArmaturePose * self, ArmaturePose * pose) { bool poseBonesAdded = false; for (unsigned int poseBoneIndex = 0; poseBoneIndex < pose->poseBoneCount; poseBoneIndex++) { ArmaturePoseBone * toPoseBone = ArmaturePose_getPoseBone(self, pose->poseBones[poseBoneIndex].boneID); if (toPoseBone == NULL) { ArmaturePose_addPoseBone(self, pose->poseBones[poseBoneIndex]); poseBonesAdded = true; } else { toPoseBone->offset = Vector3f_add(toPoseBone->offset, pose->poseBones[poseBoneIndex].offset); toPoseBone->scale = Vector3f_multiplyComponents(toPoseBone->scale, pose->poseBones[poseBoneIndex].scale); toPoseBone->rotation = Quaternionf_multiplied(toPoseBone->rotation, pose->poseBones[poseBoneIndex].rotation); } } return poseBonesAdded; } void ArmaturePose_resolveArmatureBoneIndexes(ArmaturePose * self, Armature * armature) { for (unsigned int poseBoneIndex = 0; poseBoneIndex < self->poseBoneCount; poseBoneIndex++) { unsigned int armatureBoneIndex = Armature_getBoneIndex(armature, self->poseBones[poseBoneIndex].boneID); self->poseBones[poseBoneIndex].armatureBoneIndex = armatureBoneIndex; if (armatureBoneIndex == BONE_INDEX_NOT_FOUND) { self->poseBones[poseBoneIndex].parentPoseBoneIndex = BONE_INDEX_NOT_FOUND; } else { self->poseBones[poseBoneIndex].parentPoseBoneIndex = ArmaturePose_getPoseBoneIndex(self, armature->bones[armatureBoneIndex].parentID); } } } void ArmaturePose_addPoseBone(ArmaturePose * self, ArmaturePoseBone poseBone) { ArmaturePose_insertPoseBone(self, self->poseBoneCount, poseBone); } void ArmaturePose_insertPoseBone(ArmaturePose * self, unsigned int poseBoneIndex, ArmaturePoseBone poseBone) { assert(poseBoneIndex <= self->poseBoneCount); self->poseBones = realloc(self->poseBones, (self->poseBoneCount + 1) * sizeof(*self->poseBones)); for (unsigned int poseBoneIndex2 = self->poseBoneCount; poseBoneIndex2 > poseBoneIndex; poseBoneIndex2--) { self->poseBones[poseBoneIndex2] = self->poseBones[poseBoneIndex2 - 1]; } self->poseBones[poseBoneIndex] = poseBone; self->poseBoneCount++; } ArmaturePoseBone * ArmaturePose_getPoseBone(ArmaturePose * self, ArmatureBoneID boneID) { unsigned int poseBoneIndex = ArmaturePose_getPoseBoneIndex(self, boneID); if (poseBoneIndex != BONE_INDEX_NOT_FOUND) { return &self->poseBones[poseBoneIndex]; } return NULL; } unsigned int ArmaturePose_getPoseBoneIndex(ArmaturePose * self, ArmatureBoneID boneID) { if (boneID != BONE_ID_NONE) { for (unsigned int poseBoneIndex = 0; poseBoneIndex < self->poseBoneCount; poseBoneIndex++) { if (self->poseBones[poseBoneIndex].boneID == boneID) { return poseBoneIndex; } } } return BONE_INDEX_NOT_FOUND; } void ArmaturePose_removePoseBoneAtIndex(ArmaturePose * self, unsigned int poseBoneIndex) { if (poseBoneIndex < self->poseBoneCount) { self->poseBoneCount--; for (; poseBoneIndex < self->poseBoneCount; poseBoneIndex++) { self->poseBones[poseBoneIndex] = self->poseBones[poseBoneIndex + 1]; } } } void ArmaturePose_reorderPoseBone(ArmaturePose * self, unsigned int fromPoseBoneIndex, unsigned int toPoseBoneIndex) { assert(fromPoseBoneIndex < self->poseBoneCount); assert(toPoseBoneIndex < self->poseBoneCount); ArmaturePoseBone swap = self->poseBones[fromPoseBoneIndex]; int direction = (toPoseBoneIndex > fromPoseBoneIndex) * 2 - 1; for (unsigned int swapIndex = fromPoseBoneIndex; swapIndex != toPoseBoneIndex; swapIndex += direction) { self->poseBones[swapIndex] = self->poseBones[swapIndex + direction]; } self->poseBones[toPoseBoneIndex] = swap; } void ArmaturePose_copyBonesFromArmature(ArmaturePose * self, Armature * armature) { if (self->poseBoneCount != armature->boneCount) { self->poseBoneCount = armature->boneCount; self->poseBones = realloc(self->poseBones, self->poseBoneCount * sizeof(*self->poseBones)); } copyArmatureBoneInfo(self, armature); }