12#include <Eigen/Geometry>
22#define TINYGLTF_IMPLEMENTATION
23#define TINYGLTF_NO_STB_IMAGE
24#define TINYGLTF_NO_STB_IMAGE_WRITE
25#define STBIWDEF static inline
42 std::vector<GHOST_XrControllerModelVertex>
vertices;
51 const tinygltf::BufferView &buffer_view,
52 const tinygltf::Buffer &buffer,
57 if (accessor.byteOffset + (accessor.count - 1) * byte_stride + element_size >
58 buffer_view.byteLength)
64 if (buffer_view.byteOffset + buffer_view.byteLength > buffer.data.size()) {
69template<
float (GHOST_XrControllerModelVertex::*field)[3]>
71 const tinygltf::BufferView &buffer_view,
72 const tinygltf::Buffer &buffer,
75 if (accessor.type != TINYGLTF_TYPE_VEC3) {
77 "glTF: Accessor for primitive attribute has incorrect type (VEC3 expected).");
80 if (accessor.componentType != TINYGLTF_COMPONENT_TYPE_FLOAT) {
82 "glTF: Accessor for primitive attribute has incorrect component type (FLOAT expected).");
86 constexpr size_t packed_size =
sizeof(
float) * 3;
87 const size_t stride = buffer_view.byteStride == 0 ? packed_size : buffer_view.byteStride;
93 primitive.
vertices.resize(accessor.count);
96 const uint8_t *buffer_ptr = buffer.data.data() + buffer_view.byteOffset + accessor.byteOffset;
97 for (
size_t i = 0; i < accessor.count; i++, buffer_ptr += stride) {
98 memcpy(primitive.
vertices[i].*field, buffer_ptr, stride);
103 const std::string &attribute_name,
107 const auto &accessor = gltf_model.accessors.at(accessor_id);
109 if (accessor.bufferView == -1) {
110 throw GHOST_XrException(
"glTF: Accessor for primitive attribute specifies no bufferview.");
113 const tinygltf::BufferView &buffer_view = gltf_model.bufferViews.at(accessor.bufferView);
114 if (!
ELEM(buffer_view.target, TINYGLTF_TARGET_ARRAY_BUFFER, 0)) {
116 "glTF: Accessor for primitive attribute uses bufferview with invalid 'target' type.");
119 const tinygltf::Buffer &buffer = gltf_model.buffers.at(buffer_view.buffer);
121 if (attribute_name.compare(
"POSITION") == 0) {
123 accessor, buffer_view, buffer, primitive);
125 else if (attribute_name.compare(
"NORMAL") == 0) {
127 accessor, buffer_view, buffer, primitive);
135template<
typename TSrcIndex>
137 const tinygltf::BufferView &buffer_view,
138 const tinygltf::Buffer &buffer,
143 if (!
ELEM(buffer_view.target, TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER, 0)) {
145 "glTF: Accessor for indices uses bufferview with invalid 'target' type.");
148 constexpr size_t component_size_bytes =
sizeof(TSrcIndex);
150 if (!
ELEM(buffer_view.byteStride, 0, component_size_bytes)) {
152 "glTF: Accessor for indices uses bufferview with invalid 'byteStride'.");
155 validate_accessor(accessor, buffer_view, buffer, component_size_bytes, component_size_bytes);
158 if ((accessor.count % 3) != 0) {
159 throw GHOST_XrException(
"glTF: Unexpected number of indices for triangle primitive");
162 const TSrcIndex *index_buffer =
reinterpret_cast<const TSrcIndex *
>(
163 buffer.data.data() + buffer_view.byteOffset + accessor.byteOffset);
164 for (uint32_t i = 0; i < accessor.count; i++) {
165 primitive.
indices.push_back(*(index_buffer + i));
173 const tinygltf::Accessor &accessor,
176 if (accessor.type != TINYGLTF_TYPE_SCALAR) {
180 if (accessor.bufferView == -1) {
181 throw GHOST_XrException(
"glTF: Index accessor without bufferView is currently not supported.");
184 const tinygltf::BufferView &buffer_view = gltf_model.bufferViews.at(accessor.bufferView);
185 const tinygltf::Buffer &buffer = gltf_model.buffers.at(buffer_view.buffer);
187 if (accessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) {
190 else if (accessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) {
193 else if (accessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT) {
197 throw GHOST_XrException(
"glTF: Accessor for indices specifies invalid 'componentType'.");
202 const tinygltf::Primitive &gltf_primitive)
204 if (gltf_primitive.mode != TINYGLTF_MODE_TRIANGLES) {
206 "glTF: Unsupported primitive mode. Only TINYGLTF_MODE_TRIANGLES is supported.");
213 for (
const auto &[attr_name, accessor_idx] : gltf_primitive.attributes) {
217 if (gltf_primitive.indices != -1) {
219 load_index_accessor(gltf_model, gltf_model.accessors.at(gltf_primitive.indices), primitive);
229 const float parent_transform[4][4],
230 float r_local_transform[4][4],
231 float r_world_transform[4][4])
235 if (gltf_node.matrix.size() == 16) {
236 const std::vector<double> &dm = gltf_node.matrix;
241 memcpy(r_local_transform, m,
sizeof(
float[4][4]));
245 std::vector<double> translation = gltf_node.translation;
246 std::vector<double> rotation = gltf_node.rotation;
247 std::vector<double>
scale = gltf_node.scale;
248 Eigen::Matrix4f &m = *(Eigen::Matrix4f *)r_local_transform;
249 Eigen::Quaternionf q;
250 Eigen::Matrix3f scalemat;
252 if (translation.size() != 3) {
253 translation.resize(3);
254 translation[0] = translation[1] = translation[2] = 0.0;
256 if (rotation.
size() != 4) {
258 rotation[0] = rotation[1] = rotation[2] = 0.0;
261 if (
scale.size() != 3) {
266 q.w() =
float(rotation[3]);
267 q.x() =
float(rotation[0]);
268 q.y() =
float(rotation[1]);
269 q.z() =
float(rotation[2]);
272 scalemat.setIdentity();
278 m.block<3, 3>(0, 0) = q.toRotationMatrix() * scalemat;
279 m.block<3, 1>(0, 3) = Eigen::Vector3f(
280 float(translation[0]),
float(translation[1]),
float(translation[2]));
283 *(Eigen::Matrix4f *)r_world_transform = *(Eigen::Matrix4f *)parent_transform *
284 *(Eigen::Matrix4f *)r_local_transform;
290 const float parent_transform[4][4],
291 const std::string &parent_name,
292 const std::vector<XrControllerModelNodePropertiesMSFT> &node_properties,
293 std::vector<GHOST_XrControllerModelVertex> &vertices,
294 std::vector<uint32_t> &indices,
295 std::vector<GHOST_XrControllerModelComponent> &components,
296 std::vector<GHOST_XrControllerModelNode> &nodes,
297 std::vector<int32_t> &node_state_indices)
299 const tinygltf::Node &gltf_node = gltf_model.nodes.at(gltf_node_id);
300 float world_transform[4][4];
304 node.parent_idx = parent_idx;
307 for (
size_t i = 0; i < node_properties.size(); ++i) {
308 if ((node_state_indices[i] < 0) && (parent_name == node_properties[i].parentNodeName) &&
309 (gltf_node.name == node_properties[i].nodeName))
311 node_state_indices[i] = node_idx;
316 if (gltf_node.mesh != -1) {
317 const tinygltf::Mesh &gltf_mesh = gltf_model.meshes.at(gltf_node.mesh);
319 GHOST_XrControllerModelComponent &component = components.emplace_back();
320 node.component_idx = components.size() - 1;
321 memcpy(component.transform, world_transform,
sizeof(component.transform));
322 component.vertex_offset = vertices.size();
323 component.index_offset = indices.size();
325 for (
const tinygltf::Primitive &gltf_primitive : gltf_mesh.primitives) {
329 const size_t start_vertex = vertices.size();
330 size_t offset = start_vertex;
331 size_t count = primitive.
vertices.size();
332 vertices.resize(offset + count);
333 memcpy(vertices.data() + offset,
335 count *
sizeof(
decltype(primitive.
vertices)::value_type));
337 offset = indices.size();
338 count = primitive.
indices.size();
339 indices.resize(offset + count);
340 for (
size_t i = 0; i < count; i += 3) {
341 indices[offset + i + 0] = start_vertex + primitive.
indices[i + 0];
342 indices[offset + i + 1] = start_vertex + primitive.
indices[i + 2];
343 indices[offset + i + 2] = start_vertex + primitive.
indices[i + 1];
347 component.vertex_count = vertices.size() - component.vertex_offset;
348 component.index_count = indices.size() - component.index_offset;
352 for (
const int child_node_id : gltf_node.children) {
412 const char *subaction_path_str)
416 CHECK_XR(xrStringToPath(instance, subaction_path_str, &subaction_path_),
417 (std::string(
"Failed to get user path \"") + subaction_path_str +
"\".").data());
422 if (load_task_.valid()) {
429 if (data_loaded_ || load_task_.valid()) {
434 XrControllerModelKeyStateMSFT key_state{XR_TYPE_CONTROLLER_MODEL_KEY_STATE_MSFT};
436 "Failed to get controller model key state.");
438 if (key_state.modelKey != XR_NULL_CONTROLLER_MODEL_KEY_MSFT) {
439 model_key_ = key_state.modelKey;
441 load_task_ = std::async(std::launch::async,
442 [&, session = session]() {
return loadControllerModel(session); });
446void GHOST_XrControllerModel::loadControllerModel(XrSession session)
449 uint32_t buf_size = 0;
451 "Failed to get controller model buffer size.");
453 std::vector<uint8_t> buf((
size_t(buf_size)));
455 "Failed to load controller model binary buffers.");
458 tinygltf::TinyGLTF gltf_loader;
459 tinygltf::Model gltf_model;
464 auto load_img_func = [](tinygltf::Image *img,
472 void *user_pointer) ->
bool {
484 gltf_loader.SetImageLoader(load_img_func,
nullptr);
487 if (!gltf_loader.LoadBinaryFromMemory(&gltf_model, &err_msg,
nullptr, buf.data(), buf_size)) {
488 throw GHOST_XrException((
"Failed to load glTF controller model: " + err_msg).c_str());
492 XrControllerModelPropertiesMSFT model_properties{XR_TYPE_CONTROLLER_MODEL_PROPERTIES_MSFT};
493 model_properties.nodeCapacityInput = 0;
495 "Failed to get controller model node properties count.");
497 std::vector<XrControllerModelNodePropertiesMSFT> node_properties(
498 model_properties.nodeCountOutput, {XR_TYPE_CONTROLLER_MODEL_NODE_PROPERTIES_MSFT});
499 model_properties.nodeCapacityInput =
uint32_t(node_properties.size());
500 model_properties.nodeProperties = node_properties.data();
502 "Failed to get controller model node properties.");
504 node_state_indices_.resize(node_properties.size(), -1);
507 const tinygltf::Scene &default_scene = gltf_model.scenes.at(
508 (gltf_model.defaultScene == -1) ? 0 : gltf_model.defaultScene);
510 const std::string root_name =
"";
511 float root_transform[4][4] = {{0}};
512 root_transform[0][0] = root_transform[1][1] = root_transform[2][2] = root_transform[3][3] = 1.0f;
514 for (
const int node_id : default_scene.nodes) {
525 node_state_indices_);
538 XrControllerModelStateMSFT model_state{XR_TYPE_CONTROLLER_MODEL_STATE_MSFT};
539 model_state.nodeCapacityInput = 0;
541 "Failed to get controller model node state count.");
543 const uint32_t count = model_state.nodeCountOutput;
544 std::vector<XrControllerModelNodeStateMSFT> node_states(
545 count, {XR_TYPE_CONTROLLER_MODEL_NODE_STATE_MSFT});
546 model_state.nodeCapacityInput = count;
547 model_state.nodeStates = node_states.data();
549 "Failed to get controller model node states.");
552 assert(node_state_indices_.size() == count);
554 for (uint32_t state_idx = 0; state_idx < count; ++state_idx) {
555 const int32_t &node_idx = node_state_indices_[state_idx];
557 const XrPosef &pose = node_states[state_idx].nodePose;
558 Eigen::Matrix4f &m = *(Eigen::Matrix4f *)nodes_[node_idx].local_transform;
559 Eigen::Quaternionf q(
560 pose.orientation.w, pose.orientation.x, pose.orientation.y, pose.orientation.z);
562 m.block<3, 3>(0, 0) = q.toRotationMatrix();
563 m.block<3, 1>(0, 3) = Eigen::Vector3f(pose.position.x, pose.position.y, pose.position.z);
568 std::vector<Eigen::Matrix4f> world_transforms(nodes_.size());
571 world_transforms[i] = (
node.parent_idx >= 0) ? world_transforms[
node.parent_idx] *
572 *(Eigen::Matrix4f *)
node.local_transform :
573 *(Eigen::Matrix4f *)
node.local_transform;
574 if (
node.component_idx >= 0) {
575 memcpy(components_[
node.component_idx].transform,
576 world_transforms[i].data(),
577 sizeof(components_[
node.component_idx].transform));
586 r_data.count_vertices = uint32_t(vertices_.size());
587 r_data.vertices = vertices_.data();
588 r_data.count_indices = uint32_t(indices_.size());
589 r_data.indices = indices_.data();
590 r_data.count_components = uint32_t(components_.size());
591 r_data.components = components_.data();
594 r_data.count_vertices = 0;
595 r_data.vertices =
nullptr;
596 r_data.count_indices = 0;
597 r_data.indices =
nullptr;
598 r_data.count_components = 0;
599 r_data.components =
nullptr;
static void init_controller_model_extension_functions(XrInstance instance)
static void load_node(const tinygltf::Model &gltf_model, int gltf_node_id, int32_t parent_idx, const float parent_transform[4][4], const std::string &parent_name, const std::vector< XrControllerModelNodePropertiesMSFT > &node_properties, std::vector< GHOST_XrControllerModelVertex > &vertices, std::vector< uint32_t > &indices, std::vector< GHOST_XrControllerModelComponent > &components, std::vector< GHOST_XrControllerModelNode > &nodes, std::vector< int32_t > &node_state_indices)
static void read_vertices(const tinygltf::Accessor &accessor, const tinygltf::BufferView &buffer_view, const tinygltf::Buffer &buffer, GHOST_XrPrimitive &primitive)
static XrInstance g_instance
static void validate_accessor(const tinygltf::Accessor &accessor, const tinygltf::BufferView &buffer_view, const tinygltf::Buffer &buffer, size_t byte_stride, size_t element_size)
static PFN_xrLoadControllerModelMSFT g_xrLoadControllerModelMSFT
static void read_indices(const tinygltf::Accessor &accessor, const tinygltf::BufferView &buffer_view, const tinygltf::Buffer &buffer, GHOST_XrPrimitive &primitive)
static PFN_xrGetControllerModelPropertiesMSFT g_xrGetControllerModelPropertiesMSFT
static void load_index_accessor(const tinygltf::Model &gltf_model, const tinygltf::Accessor &accessor, GHOST_XrPrimitive &primitive)
static PFN_xrGetControllerModelStateMSFT g_xrGetControllerModelStateMSFT
static PFN_xrGetControllerModelKeyMSFT g_xrGetControllerModelKeyMSFT
static GHOST_XrPrimitive read_primitive(const tinygltf::Model &gltf_model, const tinygltf::Primitive &gltf_primitive)
static void calc_node_transforms(const tinygltf::Node &gltf_node, const float parent_transform[4][4], float r_local_transform[4][4], float r_world_transform[4][4])
static void load_attribute_accessor(const tinygltf::Model &gltf_model, const std::string &attribute_name, int accessor_id, GHOST_XrPrimitive &primitive)
#define INIT_EXTENSION_FUNCTION(name)
#define CHECK_XR(call, error_msg)
void updateComponents(XrSession session)
GHOST_XrControllerModel(XrInstance instance, const char *subaction_path)
void getData(GHOST_XrControllerModelData &r_data)
~GHOST_XrControllerModel()
void load(XrSession session)
GHOST_XrException(const char *msg, int result=0)
constexpr int64_t size() const
#define assert(assertion)
float3x3 scale(float3x3 mat, float2 scale)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float local_transform[4][4]
std::vector< GHOST_XrControllerModelVertex > vertices
std::vector< uint32_t > indices