Rename scene_object -> node, translation -> position, rotation -> orientation

This commit is contained in:
Guillaume Meunier
2024-02-04 11:23:32 +01:00
parent 5ad82e7fa1
commit 388f90e6c9
5 changed files with 64 additions and 61 deletions
+21 -21
View File
@@ -762,10 +762,10 @@ public:
return meshes;
}
std::vector<scene_data::scene_object> load_all_objects()
std::vector<scene_data::node> load_all_objects()
{
std::vector<scene_data::scene_object> unsorted_objects;
unsorted_objects.resize(gltf.nodes.size(), {.parent_id = scene_data::scene_object::root_id });
std::vector<scene_data::node> 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<fastgltf::Node::TRS>(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<scene_data::scene_object> topological_sort(const std::vector<scene_data::scene_object> & unsorted_objects)
std::vector<scene_data::node> topological_sort(const std::vector<scene_data::node> & unsorted_objects)
{
std::vector<scene_data::scene_object> sorted_objects;
std::vector<scene_data::node> sorted_objects;
std::vector<size_t> new_index;
std::vector<bool> 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])
+25 -22
View File
@@ -54,7 +54,7 @@ struct hash<sampler_info> : utils::magic_hash<sampler_info>
{};
} // namespace std
class scene_object_handle;
class node_handle;
struct scene_data
{
@@ -169,20 +169,22 @@ struct scene_data
std::shared_ptr<buffer_allocation> buffer;
};
struct scene_object
struct node
{
static constexpr size_t root_id = size_t(-1);
size_t parent_id;
std::optional<size_t> 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<glm::mat4> bones_transform; // TODO: bones
};
@@ -190,7 +192,7 @@ struct scene_data
// TODO: skybox
std::vector<scene_data::mesh> meshes;
std::vector<scene_data::scene_object> scene_objects;
std::vector<scene_data::node> 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<node_handle> find_children(node_handle parent);
std::shared_ptr<material> 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;
}
};
+4 -4
View File
@@ -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<glm::mat4> 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<glm::mat4> 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<float, 4>& 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<float, 4>& 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;
+12 -12
View File
@@ -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<input_profile::node_state_transform, input_profile::node_state_transform> transforms, float value)
void apply_visual_response(node_handle node, std::pair<input_profile::node_state_transform, input_profile::node_state_transform> 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
{
+2 -2
View File
@@ -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<visual_response> responses;
std::vector<std::pair<XrSpace, scene_object_handle>> model_handles;
std::vector<std::pair<XrSpace, node_handle>> model_handles;
input_profile(const std::filesystem::path& json_profile, scene_loader& loader, scene_data& scene);