#include "serialization-camera.hpp" #include "serialization-component.hpp" #include "ecs.hpp" #include "math-helpers.hpp" #include "scene.hpp" #include #include "vendor-glm-include.hpp" void pke_serialize_camera(srlztn_serialize_helper *h, const PkeCamera *cam) { PkeCamera c{}; if (cam->uuid != pk_uuid_zed && cam->uuid != pk_uuid_max) { h->o << SRLZTN_CAMERA_UUID << cam->uuid << std::endl; } if (cam->type != c.type) { h->o << SRLZTN_CAMERA_TYPE << int(static_cast(cam->type)) << std::endl; } if (cam->view != c.view) { h->o << SRLZTN_CAMERA_ORIENTATION << int(static_cast(cam->view)) << std::endl; } if (cam->phys.target_inst_uuid != pk_uuid_zed) { h->o << SRLZTN_CAMERA_TARGET_INSTANCE_UUID << cam->phys.target_inst_uuid << std::endl; } if (cam->isPrimary != c.isPrimary) { h->o << SRLZTN_CAMERA_IS_PRIMARY << cam->isPrimary << std::endl; } CompInstance &comp = *ECS_GetInstance(cam->phys.instHandle); InstPos baseInst{}; baseInst.posRot = btTransform{}; baseInst.posRot.setIdentity(); baseInst.scale = btVector3(1, 1, 1); btTransform trans; comp.bt.motionState->getWorldTransform(trans); btVector3 scale = comp.bt.rigidBody->getCollisionShape()->getLocalScaling(); btVector3 pos = trans.getOrigin(); btQuaternion rot = trans.getRotation(); glm::vec3 glm_pos; glm::quat quat_rot; glm::vec3 glm_scale; BulletToGlm(pos, glm_pos); BulletToGlm(rot, quat_rot); BulletToGlm(scale, glm_scale); pke_serialize_inst_pos(h, glm_pos, quat_rot, glm_scale); } bool FindFirstInstanceHandle(void *handle, void *mapping) { srlztn_instance_mapping *inst_mapping = reinterpret_cast(mapping); return inst_mapping->serialized_uuid == *reinterpret_cast(handle); } void pke_deserialize_camera(srlztn_deserialize_helper *h) { 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); while (h->i->getline(h->read_line, h->read_line_len)) { if (strcmp(h->read_line, SRLZTN_OBJ_END) == 0) { uint32_t targetInstanceIndex = -1; if (target_uuid != pk_uuid_zed) { targetInstanceIndex = pk_arr_find_first_index(&h->mapping, &target_uuid, FindFirstInstanceHandle); 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.", 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_scene_register_camera(h->scene->scene_handle, rCam.camHandle); 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; } return; } if (pke_deserialize_inst_pos(h, pos, quat_rot, scale)) { continue; } if (strncmp(h->read_line, SRLZTN_CAMERA_UUID, strlen(SRLZTN_CAMERA_TYPE)) == 0) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_UUID); (h->read_line + prefixLen) >> cam.uuid; continue; } if (strncmp(h->read_line, SRLZTN_CAMERA_TYPE, strlen(SRLZTN_CAMERA_TYPE)) == 0) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_TYPE); PkeCameraType_T cam_type; stn_res = pk_stn(&cam_type, h->read_line + prefixLen, 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, h->read_line); } continue; } if (strncmp(h->read_line, SRLZTN_CAMERA_ORIENTATION, strlen(SRLZTN_CAMERA_ORIENTATION)) == 0) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_ORIENTATION); PkeCameraView_T cam_view; stn_res = pk_stn(&cam_view, h->read_line + prefixLen, 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, h->read_line); } continue; } if (strstr(h->read_line, SRLZTN_CAMERA_TARGET_INSTANCE_UUID)) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_TARGET_INSTANCE_UUID); (h->read_line + prefixLen) >> target_uuid; continue; } if (strstr(h->read_line, SRLZTN_CAMERA_IS_PRIMARY)) { uint64_t prefixLen = strlen(SRLZTN_CAMERA_IS_PRIMARY); stn_res = pk_stn(&cam.isPrimary, h->read_line + prefixLen, nullptr); if (stn_res != PK_STN_RES_SUCCESS) { fprintf(stderr, "[%s] Err '%u' parsing camera primary from: '%s'\n", __FILE__, stn_res, h->read_line); } continue; } } }