#include "serialization-camera.hpp" #include "camera.hpp" #include "compile-time-assert.hpp" #include "ecs.hpp" #include "level.hpp" #include "math-helpers.hpp" #include "serialization-component.hpp" #include "serialization.hpp" #include "vendor-glm-include.hpp" #include "pk.h" #include pk_handle pke_serialize_camera(srlztn_serialize_helper *h, const PkeCamera *cam) { assert(h != nullptr); assert(cam != nullptr); char *s; pk_handle inst_pos_handle; pke_kve kve{}; pke_kve_container kvec{}; InstPos baseInst{}; btTransform trans; glm::vec3 glm_pos; glm::quat quat_rot; glm::vec3 glm_scale; kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_CAMERA); kvec.bkt = h->bkt; kvec.arr.bkt = h->bkt; kvec.children.bkt = h->bkt; kvec.child_handles.bkt = h->bkt; pk_arr_reset(&kvec.arr); pk_arr_reset(&kvec.children); pk_arr_reset(&kvec.child_handles); if (PK_HAS_FLAG(cam->entity_flags, ENTITY_FLAG_DO_NOT_SERIALIZE)) { return PK_HANDLE_MAX; } CompInstance &comp = *ECS_GetInstance(cam->phys.instHandle); baseInst.posRot = btTransform{}; baseInst.posRot.setIdentity(); baseInst.scale = btVector3(1, 1, 1); comp.bt.motionState->getWorldTransform(trans); btVector3 scale = comp.bt.rigidBody->getCollisionShape()->getLocalScaling(); btVector3 pos = trans.getOrigin(); btQuaternion rot = trans.getRotation(); BulletToGlm(pos, glm_pos); BulletToGlm(rot, quat_rot); BulletToGlm(scale, glm_scale); inst_pos_handle = pke_serialize_inst_pos(h, glm_pos, quat_rot, glm_scale); kvec.srlztn_handle = h->handle_head; h->handle_head.itemIndex++; pk_arr_append_t(&kvec.child_handles, inst_pos_handle); compt_a<128==sizeof(PkeCamera)>(); if (cam->uuid != pk_uuid_zed && cam->uuid != pk_uuid_max) { kve.key = SRLZTN_CAMERA_UUID; s = pk_new_arr(37, h->bkt); sprintf(s, pk_uuid_printf_format, pk_uuid_printf_var(cam->uuid)); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } { kve.key = SRLZTN_CAMERA_TYPE; s = pk_new_arr(5, h->bkt); sprintf(s, "0x%.2X", static_cast(cam->type)); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } { kve.key = SRLZTN_CAMERA_ORIENTATION; s = pk_new_arr(5, h->bkt); sprintf(s, "0x%.2X", static_cast(cam->view)); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } if (cam->phys.target_inst_uuid != pk_uuid_zed) { kve.key = SRLZTN_CAMERA_TARGET_INSTANCE_UUID; s = pk_new_arr(37, h->bkt); sprintf(s, pk_uuid_printf_format, pk_uuid_printf_var(cam->phys.target_inst_uuid)); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } if (cam->isPrimary) { kve.key = SRLZTN_CAMERA_IS_PRIMARY; s = pk_new_arr(2, h->bkt); sprintf(s, "%i", cam->isPrimary); kve.val = s; kve.end = SRLZTN_KVE_END; pk_arr_append_t(&kvec.arr, kve); } pk_arr_append_t(&h->kvp_containers, kvec); kvec.arr.data = nullptr; kvec.children.data = nullptr; kvec.child_handles.data = nullptr; return kvec.srlztn_handle; } void pke_deserialize_camera(srlztn_deserialize_helper *h, pke_kve_container *kvec) { uint32_t i; PK_STN_RES stn_res = PK_STN_RES(0); PkeCamera cam{}; cam.type = PKE_CAMERA_TYPE_PERSPECTIVE; cam.view = PKE_CAMERA_VIEW_FREE; InstPos instPos; pk_uuid target_uuid = pk_uuid_zed; glm::vec3 pos = glm::vec3(0); glm::quat quat_rot = glm::quat(0, 0, 0, 1); glm::vec3 scale = glm::vec3(1); instPos.mass = 1; instPos.posRot.setIdentity(); instPos.scale = btVector3(1, 1, 1); for (i = 0; i < kvec->children.next; ++i) { pke_kve_container *child_kvec = kvec->children[i]; if (strncmp(child_kvec->type_code.val, SRLZTN_OBJ_INSTANCE_POSITION, strlen(SRLZTN_OBJ_INSTANCE_POSITION)) == 0) { pke_deserialize_inst_pos(h, child_kvec, pos, quat_rot, scale); } } compt_a<128==sizeof(PkeCamera)>(); for (i = 0; i < kvec->arr.next; ++i) { if (strncmp(kvec->arr[i].key, SRLZTN_CAMERA_UUID, strlen(SRLZTN_CAMERA_TYPE)) == 0) { kvec->arr[i].val >> cam.uuid; continue; } if (strncmp(kvec->arr[i].key, SRLZTN_CAMERA_TYPE, strlen(SRLZTN_CAMERA_TYPE)) == 0) { PkeCameraType_T cam_type; stn_res = pk_stn(&cam_type, kvec->arr[i].val, nullptr); cam.type = PkeCameraType(cam_type); if (stn_res != PK_STN_RES_SUCCESS) { fprintf(stderr, "[%s] Err '%u' parsing camera type from: '%s'\n", __FILE__, stn_res, kvec->arr[i].val); } continue; } if (strncmp(kvec->arr[i].key, SRLZTN_CAMERA_ORIENTATION, strlen(SRLZTN_CAMERA_ORIENTATION)) == 0) { PkeCameraView_T cam_view; stn_res = pk_stn(&cam_view, kvec->arr[i].val, nullptr); cam.view = PkeCameraView(cam_view); if (stn_res != PK_STN_RES_SUCCESS) { fprintf(stderr, "[%s] Err '%u' parsing camera view from: '%s'\n", __FILE__, stn_res, kvec->arr[i].val); } continue; } if (strstr(kvec->arr[i].key, SRLZTN_CAMERA_TARGET_INSTANCE_UUID)) { kvec->arr[i].val >> target_uuid; continue; } if (strstr(kvec->arr[i].key, SRLZTN_CAMERA_IS_PRIMARY)) { stn_res = pk_stn(&cam.isPrimary, kvec->arr[i].val, nullptr); if (stn_res != PK_STN_RES_SUCCESS) { fprintf(stderr, "[%s] Err '%u' parsing camera primary from: '%s'\n", __FILE__, stn_res, kvec->arr[i].val); } continue; } } uint32_t targetInstanceIndex = -1; if (target_uuid != pk_uuid_zed) { targetInstanceIndex = pk_arr_find_first_index(&h->mapping, &target_uuid, srlztn_mapping_find_first_handle_by_uuid); if (targetInstanceIndex == uint32_t(-1)) { fprintf(stderr, "[pke_deserialize_camera] Camera has target instance uuid '" pk_uuid_printf_format "', but failed to find target instance.\n", pk_uuid_printf_var(target_uuid)); } } btVector3 bt_pos; btQuaternion bt_rot; GlmToBullet(pos, bt_pos); GlmToBullet(scale, instPos.scale); GlmToBullet(quat_rot, bt_rot); instPos.posRot.setOrigin(bt_pos); instPos.posRot.setRotation(bt_rot); auto &rCam = PkeCamera_Register(cam.uuid, instPos); rCam.type = cam.type; rCam.view = cam.view; rCam.isPrimary = cam.isPrimary; pke_level_register_camera(h->level, &rCam); if (targetInstanceIndex != uint32_t(-1)) { rCam.phys.target_inst_handle = h->mapping[targetInstanceIndex].created_instance->instanceHandle; PkeCamera_TargetInstance(rCam.camHandle, h->mapping[targetInstanceIndex].created_instance); } if (rCam.isPrimary == true) { ActiveCamera = &rCam; } srlztn_ecs_mapping map{}; map.created_entity = &rCam; map.serialized_uuid = rCam.uuid; map.created_instance = nullptr; pk_arr_append_t(&h->mapping, map); }