/* Copyright (c) 2023 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 "3dmodelio/ArmaturePoseIO.h" #include ArmaturePose * ArmaturePose_deserializeMinimal(compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { #define stemobject_implementation ArmaturePose stemobject_deserialize_implementation(loadSerializedDataMinimal, formatVersion) #undef stemobject_implementation } ArmaturePose * ArmaturePose_deserialize(compat_type(DeserializationContext *) deserializationContext) { #define stemobject_implementation ArmaturePose stemobject_deserialize_implementation(loadSerializedData) #undef stemobject_implementation } bool ArmaturePose_loadSerializedDataMinimal(ArmaturePose * pose, compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { DeserializationContext * context = deserializationContext; if (context->status != SERIALIZATION_ERROR_OK || formatVersion > ARMATURE_POSE_FORMAT_VERSION) { return false; } unsigned int poseBoneCount = call_virtual(beginArray, context, "pose_bones"); if (context->status != SERIALIZATION_ERROR_OK || poseBoneCount >= ARMATURE_POSE_BONE_COUNT_MAX) { return false; } ArmaturePoseBone poseBones[poseBoneCount]; for (unsigned int poseBoneIndex = 0; poseBoneIndex < poseBoneCount; poseBoneIndex++) { call_virtual(beginStructure, context, "pose_bone"); poseBones[poseBoneIndex].boneID = call_virtual(readUInt32, context, "bone_id"); call_virtual(beginStructure, context, "offset"); poseBones[poseBoneIndex].offset.x = call_virtual(readFloat, context, "x"); poseBones[poseBoneIndex].offset.y = call_virtual(readFloat, context, "y"); poseBones[poseBoneIndex].offset.z = call_virtual(readFloat, context, "z"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "scale"); poseBones[poseBoneIndex].scale.x = call_virtual(readFloat, context, "x"); poseBones[poseBoneIndex].scale.y = call_virtual(readFloat, context, "y"); poseBones[poseBoneIndex].scale.z = call_virtual(readFloat, context, "z"); call_virtual(endStructure, context); call_virtual(beginStructure, context, "rotation"); poseBones[poseBoneIndex].rotation.x = call_virtual(readFloat, context, "x"); poseBones[poseBoneIndex].rotation.y = call_virtual(readFloat, context, "y"); poseBones[poseBoneIndex].rotation.z = call_virtual(readFloat, context, "z"); poseBones[poseBoneIndex].rotation.z = call_virtual(readFloat, context, "w"); call_virtual(endStructure, context); } call_virtual(endArray, context); if (context->status != SERIALIZATION_ERROR_OK) { return false; } return ArmaturePose_init(pose, poseBoneCount, poseBones); } bool ArmaturePose_loadSerializedData(ArmaturePose * pose, compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; call_virtual(beginStructure, context, ARMATURE_POSE_FORMAT_TYPE); const char * formatType = call_virtual(readString, context, "format_type"); if (context->status != SERIALIZATION_ERROR_OK || strcmp(formatType, ARMATURE_POSE_FORMAT_TYPE)) { return NULL; } uint16_t formatVersion = call_virtual(readUInt16, context, "format_version"); bool success = ArmaturePose_loadSerializedDataMinimal(pose, context, formatVersion); call_virtual(endStructure, context); return success; } void ArmaturePose_serializeMinimal(ArmaturePose * pose, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(beginArray, context, "pose_bones"); for (unsigned int poseBoneIndex = 0; poseBoneIndex < pose->poseBoneCount; poseBoneIndex++) { call_virtual(beginStructure, context, "pose_bone"); call_virtual(writeUInt32, context, "bone_id", pose->poseBones[poseBoneIndex].boneID); call_virtual(beginStructure, context, "offset"); call_virtual(writeFloat, context, "x", pose->poseBones[poseBoneIndex].offset.x); call_virtual(writeFloat, context, "y", pose->poseBones[poseBoneIndex].offset.y); call_virtual(writeFloat, context, "z", pose->poseBones[poseBoneIndex].offset.z); call_virtual(endStructure, context); call_virtual(beginStructure, context, "scale"); call_virtual(writeFloat, context, "x", pose->poseBones[poseBoneIndex].scale.x); call_virtual(writeFloat, context, "y", pose->poseBones[poseBoneIndex].scale.y); call_virtual(writeFloat, context, "z", pose->poseBones[poseBoneIndex].scale.z); call_virtual(endStructure, context); call_virtual(beginStructure, context, "rotation"); call_virtual(writeFloat, context, "x", pose->poseBones[poseBoneIndex].rotation.x); call_virtual(writeFloat, context, "y", pose->poseBones[poseBoneIndex].rotation.y); call_virtual(writeFloat, context, "z", pose->poseBones[poseBoneIndex].rotation.z); call_virtual(writeFloat, context, "w", pose->poseBones[poseBoneIndex].rotation.z); call_virtual(endStructure, context); } call_virtual(endArray, context); } void ArmaturePose_serialize(ArmaturePose * pose, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(beginStructure, context, ARMATURE_POSE_FORMAT_TYPE); call_virtual(writeString, context, "format_type", ARMATURE_POSE_FORMAT_TYPE); call_virtual(writeUInt16, context, "format_version", ARMATURE_POSE_FORMAT_VERSION); ArmaturePose_serializeMinimal(pose, context); call_virtual(endStructure, context); }