diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-02 20:46:41 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-02 20:46:41 -0400 |
| commit | a02c7589c6c9e902c59a632aa650635336fe648c (patch) | |
| tree | 8ec4800ff9f9effbd9141e02131123448679bc93 /src/serialization-camera.cpp | |
| parent | fad302f7db146a78900f9b21dbbcd97761093c1b (diff) | |
pke: checkpoint: major serialization refactor
Diffstat (limited to 'src/serialization-camera.cpp')
| -rw-r--r-- | src/serialization-camera.cpp | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/serialization-camera.cpp b/src/serialization-camera.cpp new file mode 100644 index 0000000..abf404b --- /dev/null +++ b/src/serialization-camera.cpp @@ -0,0 +1,153 @@ + +#include "serialization-camera.hpp" + +#include "serialization-component.hpp" +#include "ecs.hpp" +#include "math-helpers.hpp" +#include "scene.hpp" + +#include <BulletCollision/CollisionShapes/btCollisionShape.h> + +#include "vendor-glm-include.hpp" + +void pke_serialize_camera(srlztn_serialize_helper *h, const PkeCamera *cam) { + NULL_CHAR_ARR(handleStr, 23); + 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<PkeCameraType_T>(cam->type)) << std::endl; + } + if (cam->view != c.view) { + h->o << SRLZTN_CAMERA_ORIENTATION << int(static_cast<PkeCameraView_T>(cam->view)) << std::endl; + } + if (cam->phys.instHandle != InstanceHandle_MAX) { + snprintf(handleStr, 22, "0x%08X 0x%08X", cam->phys.instHandle.bucketIndex, cam->phys.instHandle.itemIndex); + h->o << SRLZTN_CAMERA_INSTANCE_HANDLE << handleStr << std::endl; + } + if (cam->phys.targetInstHandle != InstanceHandle_MAX) { + snprintf(handleStr, 22, "0x%08X 0x%08X", cam->phys.targetInstHandle.bucketIndex, cam->phys.targetInstHandle.itemIndex); + h->o << SRLZTN_CAMERA_TARGET_INSTANCE_UUID << handleStr << 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<srlztn_instance_mapping *>(mapping); + return inst_mapping->serialized_uuid == *reinterpret_cast<pk_uuid *>(handle); +} +void pke_deserialize_camera(srlztn_deserialize_helper *h) { + PK_STN_RES stn_res; + PkeCamera cam{}; + InstPos instPos; + InstanceHandle instanceHandle = InstanceHandle_MAX; + pk_uuid target_uuid = pk_uuid_zed; + glm::vec3 pos; + glm::quat quat_rot; + glm::vec3 scale; + 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) { + + int64_t targetInstanceIndex = -1; + + if (target_uuid != pk_uuid_zed) { + targetInstanceIndex = pk_arr_find_first_index(&h->mapping, &target_uuid, FindFirstInstanceHandle); + } + + 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 > -1) { + 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); + stn_res = pk_stn(&cam.type, h->read_line + prefixLen); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[%s] Err '%u' parsing camera type from: '%s'", __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); + stn_res = pk_stn(&cam.view, h->read_line + prefixLen); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[%s] Err '%u' parsing camera view from: '%s'", __FILE__, stn_res, h->read_line); + } + continue; + } + if (strstr(h->read_line, SRLZTN_CAMERA_INSTANCE_HANDLE)) { + uint64_t prefixLen = strlen(SRLZTN_CAMERA_INSTANCE_HANDLE); + h->read_line[prefixLen + 10] = '\0'; + stn_res = pk_stn(&instanceHandle.bucketIndex, h->read_line + prefixLen); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[%s] Err '%u' parsing camera instance handle from: '%s'", __FILE__, stn_res, h->read_line); + } + stn_res = pk_stn(&instanceHandle.itemIndex, h->read_line + prefixLen + 11); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[%s] Err '%u' parsing camera instance handle from: '%s'", __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) >> cam.uuid; + continue; + } + if (strncmp(h->read_line, SRLZTN_CAMERA_IS_PRIMARY, strlen(SRLZTN_CAMERA_IS_PRIMARY)) == 0) { + uint64_t prefixLen = strlen(SRLZTN_CAMERA_IS_PRIMARY); + stn_res = pk_stn(&cam.isPrimary, h->read_line + prefixLen); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[%s] Err '%u' parsing camera primary from: '%s'", __FILE__, stn_res, h->read_line); + } + continue; + } + } +} |
