/* 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 "3dmodelio/MeshData.h" #include "utilities/IOUtilities.h" #include #include #define stemobject_implementation MeshData stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); #define MESH_VERSION_0_SUPPORT 1 MeshData * MeshData_create(const char * name, PrimitiveType primitiveType, const char * shaderName, const char * configurationDataName, unsigned int textureCount, struct MeshData_texture * textures, VertexFormat * vertexFormat, bool takeOwnershipOfVertexFormat, void * vertices, unsigned int vertexCount, uint32_t * indexes, unsigned int indexCount) { stemobject_create_implementation(init, name, primitiveType, shaderName, configurationDataName, textureCount, textures, vertexFormat, takeOwnershipOfVertexFormat, vertices, vertexCount, indexes, indexCount) } bool MeshData_init(MeshData * self, const char * name, PrimitiveType primitiveType, const char * shaderName, const char * configurationDataName, unsigned int textureCount, struct MeshData_texture * textures, VertexFormat * vertexFormat, bool takeOwnershipOfVertexFormat, void * vertices, unsigned int vertexCount, uint32_t * indexes, unsigned int indexCount) { call_super(init, self); self->name = strdup_nullSafe(name); self->primitiveType = primitiveType; self->shaderName = strdup_nullSafe(shaderName); self->configurationDataName = strdup_nullSafe(configurationDataName); self->textureCount = textureCount; if (textureCount == 0) { self->textures = NULL; } else { self->textures = memdup(textures, textureCount * sizeof(*textures)); for (unsigned int textureIndex = 0; textureIndex < textureCount; textureIndex++) { self->textures[textureIndex].name = strdup_nullSafe(textures[textureIndex].name); } } self->vertexFormat = vertexFormat; self->private_ivar(vertexFormatOwned) = takeOwnershipOfVertexFormat; self->vertices = vertices; self->vertexCount = vertexCount; self->indexes = indexes; self->indexCount = indexCount; return true; } void MeshData_dispose(MeshData * self) { free(self->name); free(self->shaderName); free(self->configurationDataName); if (self->private_ivar(vertexFormatOwned)) { VertexFormat_dispose(self->vertexFormat); } for (unsigned int textureIndex = 0; textureIndex < self->textureCount; textureIndex++) { free(self->textures[textureIndex].name); } free(self->textures); free(self->vertices); free(self->indexes); call_super(dispose, self); } MeshData * MeshData_deserialize(compat_type(DeserializationContext *) deserializationContext, ShaderRegistry * shaderRegistry) { stemobject_deserialize_implementation(loadSerializedData, shaderRegistry) } #define PRIMITIVE_TYPE_ENUM_VALUES { \ {"points", PRIMITIVE_POINTS}, \ {"lines", PRIMITIVE_LINES}, \ {"line_strip", PRIMITIVE_LINE_STRIP}, \ {"triangles", PRIMITIVE_TRIANGLES}, \ {"triangle_strip", PRIMITIVE_TRIANGLE_STRIP} \ } #define ATTRIBUTE_TYPE_ENUM_VALUES { \ {"float", ATTRIBUTE_TYPE_FLOAT}, \ {"int", ATTRIBUTE_TYPE_INT}, \ {"uint", ATTRIBUTE_TYPE_UINT} \ } #define ATTRIBUTE_USAGE_ENUM_VALUES { \ {"unspecified", ATTRIBUTE_USAGE_UNSPECIFIED}, \ {"position", ATTRIBUTE_USAGE_POSITION}, \ {"normal", ATTRIBUTE_USAGE_NORMAL}, \ {"color", ATTRIBUTE_USAGE_COLOR}, \ {"texture_coordinate", ATTRIBUTE_USAGE_TEXTURE_COORDINATE}, \ {"texture_index", ATTRIBUTE_USAGE_TEXTURE_INDEX}, \ {"tangent", ATTRIBUTE_USAGE_TANGENT}, \ {"bone_index", ATTRIBUTE_USAGE_BONE_INDEX}, \ {"bone_weight", ATTRIBUTE_USAGE_BONE_WEIGHT} \ } bool MeshData_loadSerializedData(MeshData * self, compat_type(DeserializationContext *) deserializationContext, ShaderRegistry * shaderRegistry) { DeserializationContext * context = deserializationContext; call_virtual(beginStructure, context, MESHDATA_FORMAT_TYPE); const char * formatType = call_virtual(readString, context, "format_type"); if (context->status != SERIALIZATION_ERROR_OK || strcmp(formatType, MESHDATA_FORMAT_TYPE)) { return false; } uint16_t formatVersion = call_virtual(readUInt16, context, "format_version"); if (context->status != SERIALIZATION_ERROR_OK || formatVersion > MESHDATA_FORMAT_VERSION) { return false; } const char * nameString = call_virtual(readStringNullable, context, "name"); PrimitiveType primitiveType; const char * shaderName, * configurationDataName; VertexFormat * vertexFormat; bool ownVertexFormat; unsigned int textureCount; struct MeshData_texture * textures; if (formatVersion == 0) { #if MESH_VERSION_0_SUPPORT const char * armatureName = call_virtual(readStringNullable, context, "armature"); configurationDataName = call_virtual(readStringNullable, context, "material"); primitiveType = PRIMITIVE_TRIANGLES; if (armatureName == NULL) { shaderName = Atom_getExisting("3d_static_normal_mapped"); } else { shaderName = Atom_getExisting("3d_animated_normal_mapped"); } VertexAttributeTypeSpec attributeTypeSpecs[] = { {"position", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_POSITION, 3}, {"texCoord", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_TEXTURE_COORDINATE, 2}, {"normal", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_NORMAL, 3}, {"tangent", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_TANGENT, 4}, {"color", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_COLOR, 4}, {"boneID", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_BONE_INDEX, 4}, {"boneWeight", ATTRIBUTE_TYPE_FLOAT, ATTRIBUTE_USAGE_BONE_WEIGHT, 4} }; struct ShaderRegistry_entry * shaderEntry = NULL; if (shaderRegistry != NULL) { shaderEntry = ShaderRegistry_lookup(shaderRegistry, shaderName); } if (shaderEntry != NULL && VertexFormat_matchesSpec(shaderEntry->shader->vertexFormat, armatureName == NULL ? 5 : 7, attributeTypeSpecs)) { vertexFormat = shaderEntry->shader->vertexFormat; ownVertexFormat = false; } else { vertexFormat = VertexFormat_create(armatureName == NULL ? 5 : 7, attributeTypeSpecs); ownVertexFormat = true; } textureCount = 0; textures = NULL; #else return false; #endif } else { Serialization_enumKeyValue primitiveTypeEnumValues[] = PRIMITIVE_TYPE_ENUM_VALUES; primitiveType = call_virtual(readEnumeration, context, "primitive_type", sizeof(primitiveTypeEnumValues) / sizeof(primitiveTypeEnumValues[0]), primitiveTypeEnumValues); shaderName = call_virtual(readStringNullable, context, "shader"); configurationDataName = call_virtual(readStringNullable, context, "configuration_data"); textureCount = call_virtual(beginArray, context, "textures"); if (textureCount == 0) { textures = NULL; } else { textures = malloc(textureCount * sizeof(*textures)); for (unsigned int textureIndex = 0; textureIndex < textureCount; textureIndex++) { call_virtual(beginStructure, context, NULL); textures[textureIndex].index = call_virtual(readUInt8, context, "index"); textures[textureIndex].name = (char *) call_virtual(readStringNullable, context, "name"); call_virtual(endStructure, context); } } call_virtual(endArray, context); unsigned int attributeCount = call_virtual(beginDictionary, context, "vertex_format"); if (attributeCount == 0) { free(textures); return false; } VertexAttributeTypeSpec attributeTypes[attributeCount]; for (unsigned int attributeIndex = 0; attributeIndex < attributeCount; attributeIndex++) { attributeTypes[attributeIndex].name = call_virtual(readNextDictionaryKey, context); call_virtual(beginStructure, context, attributeTypes[attributeIndex].name); Serialization_enumKeyValue attributeTypeEnumValues[] = ATTRIBUTE_TYPE_ENUM_VALUES; attributeTypes[attributeIndex].dataType = call_virtual(readEnumeration, context, "type", sizeof(attributeTypeEnumValues) / sizeof(attributeTypeEnumValues[0]), attributeTypeEnumValues); Serialization_enumKeyValue attributeUsageEnumValues[] = ATTRIBUTE_USAGE_ENUM_VALUES; attributeTypes[attributeIndex].usageHint = call_virtual(readEnumeration, context, "usage", sizeof(attributeUsageEnumValues) / sizeof(attributeUsageEnumValues[0]), attributeUsageEnumValues); attributeTypes[attributeIndex].componentCount = call_virtual(readUInt8, context, "count"); call_virtual(endStructure, context); } call_virtual(endDictionary, context); if (context->status != SERIALIZATION_ERROR_OK) { free(textures); return false; } struct ShaderRegistry_entry * shaderEntry = NULL; if (shaderRegistry != NULL) { shaderEntry = ShaderRegistry_lookup(shaderRegistry, shaderName); } if (shaderEntry != NULL && VertexFormat_matchesSpec(shaderEntry->shader->vertexFormat, attributeCount, attributeTypes)) { vertexFormat = shaderEntry->shader->vertexFormat; ownVertexFormat = false; } else { vertexFormat = VertexFormat_create(attributeCount, attributeTypes); ownVertexFormat = true; } } size_t verticesSize, indexesSize; void * vertices = (void *) call_virtual(readBlob, context, "vertices", &verticesSize); uint32_t * indexes = (uint32_t *) call_virtual(readBlob, context, "indexes", &indexesSize); call_virtual(endStructure, context); if (context->status != SERIALIZATION_ERROR_OK) { free(textures); return false; } unsigned int vertexCount = verticesSize / vertexFormat->bytesPerVertex; unsigned int indexCount = indexesSize / sizeof(*indexes); MeshData_init(self, nameString, primitiveType, shaderName, configurationDataName, textureCount, textures, vertexFormat, ownVertexFormat, memdup(vertices, vertexCount * vertexFormat->bytesPerVertex), vertexCount, memdup(indexes, indexCount * sizeof(*indexes)), indexCount); return true; } void MeshData_serialize(MeshData * self, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(beginStructure, context, MESHDATA_FORMAT_TYPE); call_virtual(writeString, context, "format_type", MESHDATA_FORMAT_TYPE); call_virtual(writeUInt16, context, "format_version", MESHDATA_FORMAT_VERSION); call_virtual(writeStringNullable, context, "name", self->name); Serialization_enumKeyValue primitiveTypeEnumValues[] = PRIMITIVE_TYPE_ENUM_VALUES; call_virtual(writeEnumeration, context, "primitive_type", self->primitiveType, sizeof(primitiveTypeEnumValues) / sizeof(primitiveTypeEnumValues[0]), primitiveTypeEnumValues); call_virtual(writeStringNullable, context, "shader", self->shaderName); call_virtual(writeStringNullable, context, "configuration_data", self->configurationDataName); call_virtual(beginArray, context, "textures"); for (unsigned int textureIndex = 0; textureIndex < self->textureCount; textureIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeUInt8, context, "index", self->textures[textureIndex].index); call_virtual(writeStringNullable, context, "name", self->textures[textureIndex].name); call_virtual(endStructure, context); } call_virtual(endArray, context); call_virtual(beginDictionary, context, "vertex_format"); for (unsigned int attributeIndex = 0; attributeIndex < self->vertexFormat->attributeCount; attributeIndex++) { call_virtual(beginStructure, context, self->vertexFormat->attributeTypes[attributeIndex].name); Serialization_enumKeyValue attributeTypeEnumValues[] = ATTRIBUTE_TYPE_ENUM_VALUES; call_virtual(writeEnumeration, context, "type", self->vertexFormat->attributeTypes[attributeIndex].dataType, sizeof(attributeTypeEnumValues) / sizeof(attributeTypeEnumValues[0]), attributeTypeEnumValues); Serialization_enumKeyValue attributeUsageEnumValues[] = ATTRIBUTE_USAGE_ENUM_VALUES; call_virtual(writeEnumeration, context, "usage", self->vertexFormat->attributeTypes[attributeIndex].usageHint, sizeof(attributeUsageEnumValues) / sizeof(attributeUsageEnumValues[0]), attributeUsageEnumValues); call_virtual(writeUInt8, context, "count", self->vertexFormat->attributeTypes[attributeIndex].componentCount); call_virtual(endStructure, context); } call_virtual(endDictionary, context); call_virtual(writeBlob, context, "vertices", self->vertices, self->vertexCount * self->vertexFormat->bytesPerVertex); call_virtual(writeBlob, context, "indexes", self->indexes, self->indexCount * sizeof(*self->indexes)); call_virtual(endStructure, context); }