From 388f90e6c9b7acc4954e6d8f31f06999788cafbb Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 3 Feb 2024 11:29:19 +0100 Subject: [PATCH] Rename scene_object -> node, translation -> position, rotation -> orientation --- client/render/scene_data.cpp | 42 ++++++++++++++-------------- client/render/scene_data.h | 47 +++++++++++++++++--------------- client/render/scene_renderer.cpp | 8 +++--- client/scenes/input_profile.cpp | 24 ++++++++-------- client/scenes/input_profile.h | 4 +-- 5 files changed, 64 insertions(+), 61 deletions(-) diff --git a/client/render/scene_data.cpp b/client/render/scene_data.cpp index 071f7ec9..e47a0405 100644 --- a/client/render/scene_data.cpp +++ b/client/render/scene_data.cpp @@ -762,10 +762,10 @@ public: return meshes; } - std::vector load_all_objects() + std::vector load_all_objects() { - std::vector unsorted_objects; - unsorted_objects.resize(gltf.nodes.size(), {.parent_id = scene_data::scene_object::root_id }); + std::vector unsorted_objects; + unsorted_objects.resize(gltf.nodes.size(), {.parent_id = scene_data::node::root_id }); for (const auto & [index, gltf_node]: utils::enumerate(gltf.nodes)) { @@ -779,8 +779,8 @@ public: auto TRS = std::get(gltf_node.transform); - unsorted_objects[index].translation = glm::make_vec3(TRS.translation.data()); - unsorted_objects[index].rotation = glm::make_quat(TRS.rotation.data()); + unsorted_objects[index].position = glm::make_vec3(TRS.translation.data()); + unsorted_objects[index].orientation = glm::make_quat(TRS.rotation.data()); unsorted_objects[index].scale = glm::make_vec3(TRS.scale.data()); unsorted_objects[index].visible = true; unsorted_objects[index].name = gltf_node.name; @@ -789,15 +789,15 @@ public: return unsorted_objects; } - std::vector topological_sort(const std::vector & unsorted_objects) + std::vector topological_sort(const std::vector & unsorted_objects) { - std::vector sorted_objects; + std::vector sorted_objects; std::vector new_index; std::vector already_sorted; sorted_objects.reserve(unsorted_objects.size()); already_sorted.resize(unsorted_objects.size(), false); - new_index.resize(unsorted_objects.size(), scene_data::scene_object::root_id); + new_index.resize(unsorted_objects.size(), scene_data::node::root_id); [[maybe_unused]] bool loop_detected = true; @@ -808,7 +808,7 @@ public: if (already_sorted[i]) continue; - if (unsorted_objects[i].parent_id == scene_data::scene_object::root_id) + if (unsorted_objects[i].parent_id == scene_data::node::root_id) { sorted_objects.push_back(unsorted_objects[i]); already_sorted[i] = true; @@ -830,7 +830,7 @@ public: assert(sorted_objects.size() == unsorted_objects.size()); for(size_t i = 0; i < sorted_objects.size(); i++) { - assert(sorted_objects[i].parent_id == scene_data::scene_object::root_id || sorted_objects[i].parent_id < i); + assert(sorted_objects[i].parent_id == scene_data::node::root_id || sorted_objects[i].parent_id < i); } return sorted_objects; @@ -901,9 +901,9 @@ scene_data scene_loader::operator()(const std::filesystem::path & gltf_path) return data; } -scene_data & scene_data::import(scene_data && other, scene_object_handle parent) +scene_data & scene_data::import(scene_data && other, node_handle parent) { - assert(parent.scene == this || parent.id == scene_object::root_id); + assert(parent.scene == this || parent.id == node::root_id); size_t mesh_offset = meshes.size(); size_t scene_objects_offset = scene_objects.size(); @@ -918,7 +918,7 @@ scene_data & scene_data::import(scene_data && other, scene_object_handle parent) if (i.mesh_id) *i.mesh_id += mesh_offset; - if (i.parent_id == scene_object::root_id) + if (i.parent_id == node::root_id) { i.parent_id = parent.id; } @@ -943,14 +943,14 @@ scene_data & scene_data::import(scene_data && other) return import(std::move(other), {}); } -scene_object_handle scene_data::new_node() +node_handle scene_data::new_node() { size_t id = scene_objects.size(); - scene_objects.push_back(scene_object{ - .parent_id = scene_object::root_id, - .translation = {0, 0, 0}, - .rotation = {1, 0, 0, 0}, + scene_objects.push_back(node{ + .parent_id = node::root_id, + .position = {0, 0, 0}, + .orientation = {1, 0, 0, 0}, .scale = {1, 1, 1}, .visible = true }); @@ -958,7 +958,7 @@ scene_object_handle scene_data::new_node() return {id, this}; } -scene_object_handle scene_data::find_node(std::string_view name) +node_handle scene_data::find_node(std::string_view name) { for(auto&& [index, node]: utils::enumerate(scene_objects)) { @@ -972,7 +972,7 @@ scene_object_handle scene_data::find_node(std::string_view name) throw std::runtime_error("Node " + std::string(name) + " not found"); } -scene_object_handle scene_data::find_node(scene_object_handle root, std::string_view name) +node_handle scene_data::find_node(node_handle root, std::string_view name) { assert(root.id < scene_objects.size()); assert(root.scene == this); @@ -985,7 +985,7 @@ scene_object_handle scene_data::find_node(scene_object_handle root, std::string_ { size_t parent = scene_objects[index].parent_id; - if (parent == scene_object::root_id) + if (parent == node::root_id) continue; if (!flag[parent]) diff --git a/client/render/scene_data.h b/client/render/scene_data.h index 9349ea82..08fb24cc 100644 --- a/client/render/scene_data.h +++ b/client/render/scene_data.h @@ -54,7 +54,7 @@ struct hash : utils::magic_hash {}; } // namespace std -class scene_object_handle; +class node_handle; struct scene_data { @@ -169,20 +169,22 @@ struct scene_data std::shared_ptr buffer; }; - struct scene_object + struct node { static constexpr size_t root_id = size_t(-1); size_t parent_id; std::optional mesh_id; - glm::vec3 translation; - glm::quat rotation; + glm::vec3 position; + glm::quat orientation; glm::vec3 scale; std::string name; bool visible; + uint32_t layer_mask = 1; + // std::vector bones_transform; // TODO: bones }; @@ -190,7 +192,7 @@ struct scene_data // TODO: skybox std::vector meshes; - std::vector scene_objects; + std::vector scene_objects; scene_data() = default; scene_data(const scene_data &) = delete; @@ -198,12 +200,13 @@ struct scene_data scene_data & operator=(const scene_data &) = delete; scene_data & operator=(scene_data &&) = default; - scene_data& import(scene_data && other, scene_object_handle parent); + scene_data& import(scene_data && other, node_handle parent); scene_data& import(scene_data && other); - scene_object_handle new_node(); - scene_object_handle find_node(std::string_view name); - scene_object_handle find_node(scene_object_handle parent, std::string_view name); + node_handle new_node(); + node_handle find_node(std::string_view name); + node_handle find_node(node_handle parent, std::string_view name); + std::vector find_children(node_handle parent); std::shared_ptr find_material(std::string_view name); }; @@ -228,47 +231,47 @@ public: scene_data operator()(const std::filesystem::path & gltf_path); }; -class scene_object_handle +class node_handle { friend struct scene_data; - size_t id = scene_data::scene_object::root_id; + size_t id = scene_data::node::root_id; scene_data * scene = nullptr; public: - scene_object_handle() = default; - scene_object_handle(size_t id, scene_data * scene) : id(id), scene(scene) {} - scene_object_handle(const scene_object_handle&) = default; - scene_object_handle& operator=(const scene_object_handle&) = default; + node_handle() = default; + node_handle(size_t id, scene_data * scene) : id(id), scene(scene) {} + node_handle(const node_handle&) = default; + node_handle& operator=(const node_handle&) = default; - scene_data::scene_object& operator*() + scene_data::node& operator*() { assert(scene != nullptr); assert(id < scene->scene_objects.size()); return scene->scene_objects[id]; } - const scene_data::scene_object& operator*() const + const scene_data::node& operator*() const { assert(scene != nullptr); assert(id < scene->scene_objects.size()); return scene->scene_objects[id]; } - scene_data::scene_object* operator->() + scene_data::node* operator->() { assert(scene != nullptr); assert(id < scene->scene_objects.size()); return &scene->scene_objects[id]; } - const scene_data::scene_object* operator->() const + const scene_data::node* operator->() const { assert(scene != nullptr); assert(id < scene->scene_objects.size()); return &scene->scene_objects[id]; } - scene_object_handle parent() + node_handle parent() { assert(scene != nullptr); assert(id < scene->scene_objects.size()); @@ -276,7 +279,7 @@ public: return {scene->scene_objects[id].parent_id, scene}; } - const scene_object_handle parent() const + const node_handle parent() const { assert(scene != nullptr); assert(id < scene->scene_objects.size()); @@ -286,6 +289,6 @@ public: operator bool() const { - return id != scene_data::scene_object::root_id; + return id != scene_data::node::root_id; } }; diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 1b3db22d..24ec6e12 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -664,7 +664,7 @@ void scene_renderer::update_material_descriptor_set(scene_data::material& materi device.updateDescriptorSets(write_ds, {}); } -static void print_scene_hierarchy(const scene_data& scene, std::span model_matrices, size_t root = scene_data::scene_object::root_id, int level = 0) +static void print_scene_hierarchy(const scene_data& scene, std::span model_matrices, size_t root = scene_data::node::root_id, int level = 0) { for(const auto&& [index, node]: utils::enumerate(scene.scene_objects)) { @@ -692,7 +692,7 @@ void scene_renderer::render(scene_data & scene, const std::array& clea size_t nb_frames = frames.size(); //size_t nb_objects = scene.scene_objects.size(); - size_t nb_objects = std::count_if(scene.scene_objects.begin(), scene.scene_objects.end(), [](const scene_data::scene_object& object){ + size_t nb_objects = std::count_if(scene.scene_objects.begin(), scene.scene_objects.end(), [](const scene_data::node& object){ return object.mesh_id; }); @@ -741,10 +741,10 @@ void scene_renderer::render(scene_data & scene, const std::array& clea for(const auto& [index, object]: utils::enumerate(scene.scene_objects)) { - glm::mat4 transform_to_parent = glm::translate(glm::mat4(1), object.translation) * (glm::mat4)object.rotation * glm::scale(glm::mat4(1), object.scale); + glm::mat4 transform_to_parent = glm::translate(glm::mat4(1), object.position) * (glm::mat4)object.orientation * glm::scale(glm::mat4(1), object.scale); float det = object.scale.x * object.scale.y * object.scale.z; - if (object.parent_id == scene_data::scene_object::root_id) + if (object.parent_id == scene_data::node::root_id) { transform_to_root[index] = transform_to_parent; reverse_side[index] = det < 0; diff --git a/client/scenes/input_profile.cpp b/client/scenes/input_profile.cpp index 50da5494..cb17c55c 100644 --- a/client/scenes/input_profile.cpp +++ b/client/scenes/input_profile.cpp @@ -187,12 +187,12 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l auto max_node = data.find_node(response.value["maxNodeName"]); node_state_transform min{ - min_node->translation, - min_node->rotation}; + min_node->position, + min_node->orientation}; node_state_transform max{ - max_node->translation, - max_node->rotation}; + max_node->position, + max_node->orientation}; vr.state = std::make_pair(min, max); } @@ -208,7 +208,7 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l for(auto&& [layout, model]: models) { - scene_object_handle root_node = scene.new_node(); + node_handle root_node = scene.new_node(); root_node->name = layout; XrSpace space; @@ -263,7 +263,7 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l break; } - scene_object_handle controller_root_node; + node_handle controller_root_node; for(auto&& [i, j]: model_handles) { if (j->name == json_response.layout) @@ -279,13 +279,13 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l } } -void apply_visual_response(scene_object_handle node, std::pair transforms, float value) +void apply_visual_response(node_handle node, std::pair transforms, float value) { - node->translation = glm::mix(transforms.first.position, transforms.second.position, value); - node->rotation = glm::slerp(transforms.first.orientation, transforms.second.orientation, value); + node->position = glm::mix(transforms.first.position, transforms.second.position, value); + node->orientation = glm::slerp(transforms.first.orientation, transforms.second.orientation, value); } -void apply_visual_response(scene_object_handle node, input_profile::node_state_visibility, float value) +void apply_visual_response(node_handle node, input_profile::node_state_visibility, float value) { node->visible = value > 0.5; } @@ -297,8 +297,8 @@ void input_profile::apply(XrSpace world_space, XrTime predicted_display_time) if (auto location = application::locate_controller(space, world_space, predicted_display_time); location) { node->visible = true; - node->translation = location->first; - node->rotation = location->second; + node->position = location->first; + node->orientation = location->second; } else { diff --git a/client/scenes/input_profile.h b/client/scenes/input_profile.h index 385379c5..c4eae8fa 100644 --- a/client/scenes/input_profile.h +++ b/client/scenes/input_profile.h @@ -40,7 +40,7 @@ struct input_profile struct node_target { - scene_object_handle node; + node_handle node; node_state state; }; @@ -58,7 +58,7 @@ struct input_profile std::string id; std::vector responses; - std::vector> model_handles; + std::vector> model_handles; input_profile(const std::filesystem::path& json_profile, scene_loader& loader, scene_data& scene);