Skip to content

Commit 24b5d90

Browse files
authored
Make vkb::sg::Node a unified class vkb::scene_graph::Node<bindingType> (#1413)
* unify_Node * Fix dynamic_cast to reinterpret_cast in Node<bindingType>::get_parent() + two minor improvements
1 parent ee69196 commit 24b5d90

39 files changed

+299
-277
lines changed

framework/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ set(SCENE_GRAPH_FILES
142142
scene_graph/hpp_scene.h
143143
# Source Files
144144
scene_graph/component.cpp
145-
scene_graph/node.cpp
146145
scene_graph/scene.cpp
147146
scene_graph/script.cpp)
148147

framework/common/hpp_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ namespace vkb
2727
{
2828
namespace common
2929
{
30-
inline sg::Node &add_free_camera(vkb::scene_graph::HPPScene &scene, const std::string &node_name, vk::Extent2D const &extent)
30+
inline vkb::scene_graph::NodeCpp &add_free_camera(vkb::scene_graph::HPPScene &scene, const std::string &node_name, vk::Extent2D const &extent)
3131
{
32-
return vkb::add_free_camera(reinterpret_cast<vkb::sg::Scene &>(scene), node_name, static_cast<VkExtent2D>(extent));
32+
return reinterpret_cast<vkb::scene_graph::NodeCpp &>(
33+
vkb::add_free_camera(reinterpret_cast<vkb::sg::Scene &>(scene), node_name, static_cast<VkExtent2D>(extent)));
3334
}
3435

3536
inline void screenshot(vkb::rendering::RenderContextCpp &render_context, const std::string &filename)

framework/common/utils.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,15 @@ std::string to_snake_case(const std::string &text)
215215
return result.str();
216216
}
217217

218-
sg::Light &add_light(sg::Scene &scene, sg::LightType type, const glm::vec3 &position, const glm::quat &rotation, const sg::LightProperties &props, sg::Node *parent_node)
218+
sg::Light &add_light(sg::Scene &scene,
219+
sg::LightType type,
220+
const glm::vec3 &position,
221+
const glm::quat &rotation,
222+
const sg::LightProperties &props,
223+
vkb::scene_graph::NodeC *parent_node)
219224
{
220225
auto light_ptr = std::make_unique<sg::Light>("light");
221-
auto node = std::make_unique<sg::Node>(-1, "light node");
226+
auto node = std::make_unique<vkb::scene_graph::NodeC>(-1, "light node");
222227

223228
if (parent_node)
224229
{
@@ -244,22 +249,22 @@ sg::Light &add_light(sg::Scene &scene, sg::LightType type, const glm::vec3 &posi
244249
return light;
245250
}
246251

247-
sg::Light &add_point_light(sg::Scene &scene, const glm::vec3 &position, const sg::LightProperties &props, sg::Node *parent_node)
252+
sg::Light &add_point_light(sg::Scene &scene, const glm::vec3 &position, const sg::LightProperties &props, vkb::scene_graph::NodeC *parent_node)
248253
{
249254
return add_light(scene, sg::LightType::Point, position, {}, props, parent_node);
250255
}
251256

252-
sg::Light &add_directional_light(sg::Scene &scene, const glm::quat &rotation, const sg::LightProperties &props, sg::Node *parent_node)
257+
sg::Light &add_directional_light(sg::Scene &scene, const glm::quat &rotation, const sg::LightProperties &props, vkb::scene_graph::NodeC *parent_node)
253258
{
254259
return add_light(scene, sg::LightType::Directional, {}, rotation, props, parent_node);
255260
}
256261

257-
sg::Light &add_spot_light(sg::Scene &scene, const glm::vec3 &position, const glm::quat &rotation, const sg::LightProperties &props, sg::Node *parent_node)
262+
sg::Light &add_spot_light(sg::Scene &scene, const glm::vec3 &position, const glm::quat &rotation, const sg::LightProperties &props, vkb::scene_graph::NodeC *parent_node)
258263
{
259264
return add_light(scene, sg::LightType::Spot, position, rotation, props, parent_node);
260265
}
261266

262-
sg::Node &add_free_camera(sg::Scene &scene, const std::string &node_name, VkExtent2D extent)
267+
vkb::scene_graph::NodeC &add_free_camera(sg::Scene &scene, const std::string &node_name, VkExtent2D extent)
263268
{
264269
auto camera_node = scene.find_node(node_name);
265270

framework/common/utils.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ void screenshot(vkb::rendering::RenderContextC &render_context, const std::strin
6666
* @param parent_node The parent node for the line, defaults to root
6767
* @return The newly created light component
6868
*/
69-
sg::Light &add_light(sg::Scene &scene, sg::LightType type, const glm::vec3 &position, const glm::quat &rotation = {}, const sg::LightProperties &props = {}, sg::Node *parent_node = nullptr);
69+
sg::Light &add_light(sg::Scene &scene,
70+
sg::LightType type,
71+
const glm::vec3 &position,
72+
const glm::quat &rotation = {},
73+
const sg::LightProperties &props = {},
74+
vkb::scene_graph::NodeC *parent_node = nullptr);
7075

7176
/**
7277
* @brief Adds a point light to the scene with the specified parameters
@@ -76,7 +81,8 @@ sg::Light &add_light(sg::Scene &scene, sg::LightType type, const glm::vec3 &posi
7681
* @param parent_node The parent node for the line, defaults to root
7782
* @return The newly created light component
7883
*/
79-
sg::Light &add_point_light(sg::Scene &scene, const glm::vec3 &position, const sg::LightProperties &props = {}, sg::Node *parent_node = nullptr);
84+
sg::Light &
85+
add_point_light(sg::Scene &scene, const glm::vec3 &position, const sg::LightProperties &props = {}, vkb::scene_graph::NodeC *parent_node = nullptr);
8086

8187
/**
8288
* @brief Adds a directional light to the scene with the specified parameters
@@ -86,7 +92,10 @@ sg::Light &add_point_light(sg::Scene &scene, const glm::vec3 &position, const sg
8692
* @param parent_node The parent node for the line, defaults to root
8793
* @return The newly created light component
8894
*/
89-
sg::Light &add_directional_light(sg::Scene &scene, const glm::quat &rotation, const sg::LightProperties &props = {}, sg::Node *parent_node = nullptr);
95+
sg::Light &add_directional_light(sg::Scene &scene,
96+
const glm::quat &rotation,
97+
const sg::LightProperties &props = {},
98+
vkb::scene_graph::NodeC *parent_node = nullptr);
9099

91100
/**
92101
* @brief Adds a spot light to the scene with the specified parameters
@@ -97,7 +106,11 @@ sg::Light &add_directional_light(sg::Scene &scene, const glm::quat &rotation, co
97106
* @param parent_node The parent node for the line, defaults to root
98107
* @return The newly created light component
99108
*/
100-
sg::Light &add_spot_light(sg::Scene &scene, const glm::vec3 &position, const glm::quat &rotation, const sg::LightProperties &props = {}, sg::Node *parent_node = nullptr);
109+
sg::Light &add_spot_light(sg::Scene &scene,
110+
const glm::vec3 &position,
111+
const glm::quat &rotation,
112+
const sg::LightProperties &props = {},
113+
vkb::scene_graph::NodeC *parent_node = nullptr);
101114

102115
/**
103116
* @brief Add free camera script to a node with a camera object.
@@ -107,6 +120,6 @@ sg::Light &add_spot_light(sg::Scene &scene, const glm::vec3 &position, const glm
107120
* @param extent The initial resolution of the camera
108121
* @return Node where the script was attached as component
109122
*/
110-
sg::Node &add_free_camera(sg::Scene &scene, const std::string &node_name, VkExtent2D extent);
123+
vkb::scene_graph::NodeC &add_free_camera(sg::Scene &scene, const std::string &node_name, VkExtent2D extent);
111124

112125
} // namespace vkb

framework/gltf_loader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_
844844
// Load nodes
845845
auto meshes = scene.get_components<sg::Mesh>();
846846

847-
std::vector<std::unique_ptr<sg::Node>> nodes;
847+
std::vector<std::unique_ptr<vkb::scene_graph::NodeC>> nodes;
848848

849849
for (size_t node_index = 0; node_index < model.nodes.size(); ++node_index)
850850
{
@@ -1016,7 +1016,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_
10161016
scene.set_components(std::move(animations));
10171017

10181018
// Load scenes
1019-
std::queue<std::pair<sg::Node &, int>> traverse_nodes;
1019+
std::queue<std::pair<vkb::scene_graph::NodeC &, int>> traverse_nodes;
10201020

10211021
tinygltf::Scene *gltf_scene{nullptr};
10221022

@@ -1038,7 +1038,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_
10381038
throw std::runtime_error("Couldn't determine which scene to load!");
10391039
}
10401040

1041-
auto root_node = std::make_unique<sg::Node>(0, gltf_scene->name);
1041+
auto root_node = std::make_unique<vkb::scene_graph::NodeC>(0, gltf_scene->name);
10421042

10431043
for (auto node_index : gltf_scene->nodes)
10441044
{
@@ -1075,7 +1075,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_
10751075
scene.set_nodes(std::move(nodes));
10761076

10771077
// Create node for the default camera
1078-
auto camera_node = std::make_unique<sg::Node>(-1, "default_camera");
1078+
auto camera_node = std::make_unique<vkb::scene_graph::NodeC>(-1, "default_camera");
10791079

10801080
auto default_camera = create_default_camera();
10811081
default_camera->set_node(*camera_node);
@@ -1320,9 +1320,9 @@ std::unique_ptr<sg::SubMesh> GLTFLoader::load_model(uint32_t index, bool storage
13201320
return std::move(submesh);
13211321
}
13221322

1323-
std::unique_ptr<sg::Node> GLTFLoader::parse_node(const tinygltf::Node &gltf_node, size_t index) const
1323+
std::unique_ptr<vkb::scene_graph::NodeC> GLTFLoader::parse_node(const tinygltf::Node &gltf_node, size_t index) const
13241324
{
1325-
auto node = std::make_unique<sg::Node>(index, gltf_node.name);
1325+
auto node = std::make_unique<vkb::scene_graph::NodeC>(index, gltf_node.name);
13261326

13271327
auto &transform = node->get_component<sg::Transform>();
13281328

framework/gltf_loader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define TINYGLTF_NO_EXTERNAL_IMAGE
2828
#include <tiny_gltf.h>
2929

30+
#include "scene_graph/node.h"
3031
#include "timer.h"
3132

3233
#include "vulkan/vulkan.h"
@@ -91,7 +92,7 @@ class GLTFLoader
9192
std::unique_ptr<sg::SubMesh> read_model_from_file(const std::string &file_name, uint32_t index, bool storage_buffer = false, VkBufferUsageFlags additional_buffer_usage_flags = 0);
9293

9394
protected:
94-
virtual std::unique_ptr<sg::Node> parse_node(const tinygltf::Node &gltf_node, size_t index) const;
95+
virtual std::unique_ptr<vkb::scene_graph::NodeC> parse_node(const tinygltf::Node &gltf_node, size_t index) const;
9596

9697
virtual std::unique_ptr<sg::Camera> parse_camera(const tinygltf::Camera &gltf_camera) const;
9798

framework/rendering/subpasses/geometry_subpass.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void GeometrySubpass::prepare()
5656
}
5757
}
5858

59-
void GeometrySubpass::get_sorted_nodes(std::multimap<float, std::pair<sg::Node *, sg::SubMesh *>> &opaque_nodes, std::multimap<float, std::pair<sg::Node *, sg::SubMesh *>> &transparent_nodes)
59+
void GeometrySubpass::get_sorted_nodes(std::multimap<float, std::pair<vkb::scene_graph::NodeC *, sg::SubMesh *>> &opaque_nodes,
60+
std::multimap<float, std::pair<vkb::scene_graph::NodeC *, sg::SubMesh *>> &transparent_nodes)
6061
{
6162
auto camera_transform = camera.get_node()->get_transform().get_world_matrix();
6263

@@ -90,8 +91,8 @@ void GeometrySubpass::get_sorted_nodes(std::multimap<float, std::pair<sg::Node *
9091

9192
void GeometrySubpass::draw(vkb::core::CommandBufferC &command_buffer)
9293
{
93-
std::multimap<float, std::pair<sg::Node *, sg::SubMesh *>> opaque_nodes;
94-
std::multimap<float, std::pair<sg::Node *, sg::SubMesh *>> transparent_nodes;
94+
std::multimap<float, std::pair<vkb::scene_graph::NodeC *, sg::SubMesh *>> opaque_nodes;
95+
std::multimap<float, std::pair<vkb::scene_graph::NodeC *, sg::SubMesh *>> transparent_nodes;
9596

9697
get_sorted_nodes(opaque_nodes, transparent_nodes);
9798

@@ -142,7 +143,7 @@ void GeometrySubpass::draw(vkb::core::CommandBufferC &command_buffer)
142143
}
143144
}
144145

145-
void GeometrySubpass::update_uniform(vkb::core::CommandBufferC &command_buffer, sg::Node &node, size_t thread_index)
146+
void GeometrySubpass::update_uniform(vkb::core::CommandBufferC &command_buffer, vkb::scene_graph::NodeC &node, size_t thread_index)
146147
{
147148
GlobalUniform global_uniform;
148149

framework/rendering/subpasses/geometry_subpass.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ using CommandBufferC = CommandBuffer<vkb::BindingType::C>;
3535
namespace sg
3636
{
3737
class Scene;
38-
class Node;
3938
class Mesh;
4039
class SubMesh;
4140
class Camera;
@@ -97,7 +96,7 @@ class GeometrySubpass : public vkb::rendering::SubpassC
9796
void set_thread_index(uint32_t index);
9897

9998
protected:
100-
virtual void update_uniform(vkb::core::CommandBufferC &command_buffer, sg::Node &node, size_t thread_index);
99+
virtual void update_uniform(vkb::core::CommandBufferC &command_buffer, vkb::scene_graph::NodeC &node, size_t thread_index);
101100

102101
void draw_submesh(vkb::core::CommandBufferC &command_buffer, sg::SubMesh &sub_mesh, VkFrontFace front_face = VK_FRONT_FACE_COUNTER_CLOCKWISE);
103102

@@ -114,8 +113,8 @@ class GeometrySubpass : public vkb::rendering::SubpassC
114113
* @brief Sorts objects based on distance from camera and classifies them
115114
* into opaque and transparent in the arrays provided
116115
*/
117-
void get_sorted_nodes(std::multimap<float, std::pair<sg::Node *, sg::SubMesh *>> &opaque_nodes,
118-
std::multimap<float, std::pair<sg::Node *, sg::SubMesh *>> &transparent_nodes);
116+
void get_sorted_nodes(std::multimap<float, std::pair<vkb::scene_graph::NodeC *, sg::SubMesh *>> &opaque_nodes,
117+
std::multimap<float, std::pair<vkb::scene_graph::NodeC *, sg::SubMesh *>> &transparent_nodes);
119118

120119
sg::Camera &camera;
121120

framework/scene_graph/components/camera.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019-2020, Arm Limited and Contributors
1+
/* Copyright (c) 2019-2025, Arm Limited and Contributors
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -44,12 +44,12 @@ glm::mat4 Camera::get_view()
4444
return glm::inverse(transform.get_world_matrix());
4545
}
4646

47-
void Camera::set_node(Node &n)
47+
void Camera::set_node(vkb::scene_graph::NodeC &n)
4848
{
4949
node = &n;
5050
}
5151

52-
Node *Camera::get_node()
52+
vkb::scene_graph::NodeC *Camera::get_node()
5353
{
5454
return node;
5555
}

framework/scene_graph/components/camera.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2019-2024, Arm Limited and Contributors
1+
/* Copyright (c) 2019-2025, Arm Limited and Contributors
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -28,6 +28,7 @@
2828

2929
#include "common/helpers.h"
3030
#include "scene_graph/component.h"
31+
#include "scene_graph/node.h"
3132

3233
namespace vkb
3334
{
@@ -46,16 +47,16 @@ class Camera : public Component
4647

4748
glm::mat4 get_view();
4849

49-
void set_node(Node &node);
50+
void set_node(vkb::scene_graph::NodeC &node);
5051

51-
Node *get_node();
52+
vkb::scene_graph::NodeC *get_node();
5253

5354
const glm::mat4 get_pre_rotation();
5455

5556
void set_pre_rotation(const glm::mat4 &pre_rotation);
5657

5758
private:
58-
Node *node{nullptr};
59+
vkb::scene_graph::NodeC *node{nullptr};
5960

6061
glm::mat4 pre_rotation{1.0f};
6162
};

0 commit comments

Comments
 (0)