1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#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) {
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.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<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 = 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;
}
}
}
|