/* 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/SceneLayout.h" #include #include #define stemobject_implementation SceneLayout stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); SceneLayout * SceneLayout_create(Atom name, unsigned int meshCount, struct SceneMesh * meshes, unsigned int lightCount, struct SceneLight * lights) { stemobject_create_implementation(init, name, meshCount, meshes, lightCount, lights) } static void sharedInit(SceneLayout * self, Atom name) { call_super(init, self); self->name = name; } bool SceneLayout_init(SceneLayout * self, Atom name, unsigned int meshCount, struct SceneMesh * meshes, unsigned int lightCount, struct SceneLight * lights) { sharedInit(self, name); self->meshCount = meshCount; if (meshCount == 0) { self->meshes = NULL; } else { self->meshes = malloc(sizeof(*meshes) * meshCount); memcpy(self->meshes, meshes, sizeof(*meshes) * meshCount); } self->lightCount = lightCount; if (lightCount == 0) { self->lights = NULL; } else { self->lights = malloc(sizeof(*lights) * lightCount); memcpy(self->lights, lights, sizeof(*lights) * lightCount); } return true; } void SceneLayout_dispose(SceneLayout * self) { free(self->meshes); free(self->lights); call_super(dispose, self); } SceneLayout * SceneLayout_deserialize(compat_type(DeserializationContext *) deserializationContext) { stemobject_deserialize_implementation(loadSerializedData) } #define LIGHT_TYPE_ENUM_VALUES { \ {"POINT", SceneLightType_point}, \ {"DIRECTIONAL", SceneLightType_directional} \ } bool SceneLayout_loadSerializedData(SceneLayout * self, compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; uint16_t formatVersion; const char * formatType, * nameString, * meshNameString, * lightNameString; unsigned int meshCount, meshIndex, lightCount, lightIndex; struct SceneMesh * meshes; struct SceneLight * lights; context->vtable->beginStructure(context, SCENELAYOUT_FORMAT_TYPE); formatType = context->vtable->readString(context, "format_type"); if (context->status != SERIALIZATION_ERROR_OK || strcmp(formatType, SCENELAYOUT_FORMAT_TYPE)) { return false; } formatVersion = context->vtable->readUInt16(context, "format_version"); if (context->status != SERIALIZATION_ERROR_OK || formatVersion > SCENELAYOUT_FORMAT_VERSION) { return false; } nameString = context->vtable->readStringNullable(context, "name"); meshCount = context->vtable->beginDictionary(context, "meshes"); if (context->status != SERIALIZATION_ERROR_OK) { return false; } meshes = malloc(sizeof(*meshes) * meshCount); for (meshIndex = 0; meshIndex < meshCount; meshIndex++) { meshNameString = context->vtable->readNextDictionaryKey(context); context->vtable->beginStructure(context, meshNameString); context->vtable->beginStructure(context, "offset"); meshes[meshIndex].offset.x = context->vtable->readFloat(context, "x"); meshes[meshIndex].offset.y = context->vtable->readFloat(context, "y"); meshes[meshIndex].offset.z = context->vtable->readFloat(context, "z"); context->vtable->endStructure(context); context->vtable->beginStructure(context, "scale"); meshes[meshIndex].scale.x = context->vtable->readFloat(context, "x"); meshes[meshIndex].scale.y = context->vtable->readFloat(context, "y"); meshes[meshIndex].scale.z = context->vtable->readFloat(context, "z"); context->vtable->endStructure(context); context->vtable->beginStructure(context, "rotation"); meshes[meshIndex].rotation.x = context->vtable->readFloat(context, "x"); meshes[meshIndex].rotation.y = context->vtable->readFloat(context, "y"); meshes[meshIndex].rotation.z = context->vtable->readFloat(context, "z"); meshes[meshIndex].rotation.w = context->vtable->readFloat(context, "w"); context->vtable->endStructure(context); context->vtable->endStructure(context); if (context->status != SERIALIZATION_ERROR_OK) { break; } meshes[meshIndex].meshName = Atom_fromString(meshNameString); } context->vtable->endDictionary(context); if (context->status != SERIALIZATION_ERROR_OK) { free(meshes); return false; } lightCount = context->vtable->beginDictionary(context, "lights"); if (context->status != SERIALIZATION_ERROR_OK) { free(meshes); return false; } lights = malloc(sizeof(*lights) * lightCount); for (lightIndex = 0; lightIndex < lightCount; lightIndex++) { lightNameString = context->vtable->readNextDictionaryKey(context); context->vtable->beginStructure(context, lightNameString); Serialization_enumKeyValue lightTypeEnumValues[] = LIGHT_TYPE_ENUM_VALUES; lights[lightIndex].type = context->vtable->readEnumeration(context, "type", sizeof(lightTypeEnumValues) / sizeof(lightTypeEnumValues[0]), lightTypeEnumValues); context->vtable->beginStructure(context, lights[lightIndex].type == SceneLightType_point ? "position" : "direction"); lights[lightIndex].vector.x = context->vtable->readFloat(context, "x"); lights[lightIndex].vector.y = context->vtable->readFloat(context, "y"); lights[lightIndex].vector.z = context->vtable->readFloat(context, "z"); context->vtable->endStructure(context); context->vtable->beginStructure(context, "color"); lights[lightIndex].color.red = context->vtable->readFloat(context, "red"); lights[lightIndex].color.green = context->vtable->readFloat(context, "green"); lights[lightIndex].color.blue = context->vtable->readFloat(context, "blue"); context->vtable->endStructure(context); lights[lightIndex].intensity = context->vtable->readFloat(context, "intensity"); if (lights[lightIndex].type == SceneLightType_point) { lights[lightIndex].falloff = context->vtable->readFloat(context, "falloff"); } else { lights[lightIndex].falloff = 0.0f; } context->vtable->endStructure(context); if (context->status != SERIALIZATION_ERROR_OK) { break; } lights[lightIndex].name = Atom_fromString(lightNameString); } context->vtable->endDictionary(context); context->vtable->endStructure(context); if (context->status != SERIALIZATION_ERROR_OK) { free(meshes); free(lights); return false; } sharedInit(self, Atom_fromString(nameString)); self->meshCount = meshCount; self->meshes = meshes; self->lightCount = lightCount; self->lights = lights; return true; } void SceneLayout_serialize(SceneLayout * self, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; unsigned int meshIndex, lightIndex; context->vtable->beginStructure(context, SCENELAYOUT_FORMAT_TYPE); context->vtable->writeString(context, "format_type", SCENELAYOUT_FORMAT_TYPE); context->vtable->writeUInt16(context, "format_version", SCENELAYOUT_FORMAT_VERSION); context->vtable->writeStringNullable(context, "name", self->name); context->vtable->beginDictionary(context, "meshes"); for (meshIndex = 0; meshIndex < self->meshCount; meshIndex++) { context->vtable->beginStructure(context, self->meshes[meshIndex].meshName); context->vtable->beginStructure(context, "offset"); context->vtable->writeFloat(context, "x", self->meshes[meshIndex].offset.x); context->vtable->writeFloat(context, "y", self->meshes[meshIndex].offset.y); context->vtable->writeFloat(context, "z", self->meshes[meshIndex].offset.z); context->vtable->endStructure(context); context->vtable->beginStructure(context, "scale"); context->vtable->writeFloat(context, "x", self->meshes[meshIndex].scale.x); context->vtable->writeFloat(context, "y", self->meshes[meshIndex].scale.y); context->vtable->writeFloat(context, "z", self->meshes[meshIndex].scale.z); context->vtable->endStructure(context); context->vtable->beginStructure(context, "rotation"); context->vtable->writeFloat(context, "x", self->meshes[meshIndex].rotation.x); context->vtable->writeFloat(context, "y", self->meshes[meshIndex].rotation.y); context->vtable->writeFloat(context, "z", self->meshes[meshIndex].rotation.z); context->vtable->writeFloat(context, "w", self->meshes[meshIndex].rotation.w); context->vtable->endStructure(context); context->vtable->endStructure(context); } context->vtable->endDictionary(context); context->vtable->beginDictionary(context, "lights"); for (lightIndex = 0; lightIndex < self->lightCount; lightIndex++) { context->vtable->beginStructure(context, self->lights[lightIndex].name); Serialization_enumKeyValue lightTypeEnumValues[] = LIGHT_TYPE_ENUM_VALUES; context->vtable->writeEnumeration(context, "type", self->lights[lightIndex].type, sizeof(lightTypeEnumValues) / sizeof(lightTypeEnumValues[0]), lightTypeEnumValues); context->vtable->beginStructure(context, self->lights[lightIndex].type == SceneLightType_point ? "position" : "direction"); context->vtable->writeFloat(context, "x", self->lights[lightIndex].vector.x); context->vtable->writeFloat(context, "y", self->lights[lightIndex].vector.y); context->vtable->writeFloat(context, "z", self->lights[lightIndex].vector.z); context->vtable->endStructure(context); context->vtable->beginStructure(context, "color"); context->vtable->writeFloat(context, "red", self->lights[lightIndex].color.red); context->vtable->writeFloat(context, "green", self->lights[lightIndex].color.green); context->vtable->writeFloat(context, "blue", self->lights[lightIndex].color.blue); context->vtable->endStructure(context); context->vtable->writeFloat(context, "intensity", self->lights[lightIndex].intensity); if (self->lights[lightIndex].type == SceneLightType_point) { context->vtable->writeFloat(context, "falloff", self->lights[lightIndex].falloff); } context->vtable->endStructure(context); } context->vtable->endDictionary(context); context->vtable->endStructure(context); }