From 440b292c39162284a447b34d3a692143af9fbc87 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 30 Oct 2025 17:21:22 -0700 Subject: - Replace SceneCamera with Camera. - Remove backpointer from scene types to node to decouple underlying types from the scene graph. --- src/scene/animation.c | 10 ++-------- src/scene/animation_impl.h | 30 ++++++++++++++---------------- src/scene/camera.c | 28 ++++++---------------------- src/scene/camera_impl.h | 12 ------------ src/scene/model_impl.h | 1 - src/scene/node.c | 32 +++++++++++--------------------- src/scene/node_impl.h | 4 +--- src/scene/object.c | 4 ---- src/scene/object_impl.h | 4 +--- src/scene/scene_graph.h | 2 +- 10 files changed, 36 insertions(+), 91 deletions(-) delete mode 100644 src/scene/camera_impl.h (limited to 'src/scene') diff --git a/src/scene/animation.c b/src/scene/animation.c index 601c400..a498961 100644 --- a/src/scene/animation.c +++ b/src/scene/animation.c @@ -204,10 +204,6 @@ void gfx_destroy_anima(Anima** anima) { mem_free_animation(&animation); } - if ((*anima)->parent.val) { - gfx_del_node((*anima)->parent); - } - mem_free_anima(anima); } } @@ -450,10 +446,8 @@ void gfx_update_animation(Anima* anima, R t) { // which we have constructed to be the common root of all skeletons. // // This procedure touches every joint exactly once. - SceneNode* root_node = mem_get_node(anima->parent); - // LOGD("Root: %u, child: %u", anima->parent.val, root->child.val); - const mat4 root_global_transform = gfx_get_node_global_transform(root_node); - const mat4 root_inv_global_transform = mat4_inverse(root_global_transform); + const mat4 root_global_transform = mat4_id(); + const mat4 root_inv_global_transform = mat4_id(); Joint* root_joint = get_anima_root_joint(anima); compute_joint_matrices_rec( diff --git a/src/scene/animation_impl.h b/src/scene/animation_impl.h index 4408158..ef1492c 100644 --- a/src/scene/animation_impl.h +++ b/src/scene/animation_impl.h @@ -12,7 +12,6 @@ #include #include -#include typedef struct Buffer Buffer; @@ -24,24 +23,24 @@ typedef struct Buffer Buffer; /// Joints are mutable and store the transform and joint matrices that result /// from animation, aside from the inverse bind matrix. typedef struct Joint { - joint_idx child; /// First child Joint; index into Anima's joints. - joint_idx next; /// Next sibling Joint; index into Anima's joints. - mat4 transform; /// Local transform relative to parent. - mat4 inv_bind_matrix; /// Transforms the mesh into the joint's local space. - mat4 joint_matrix; /// inv(global) * global joint transform * inv(bind). - aabb3 box; /// Bounding box of vertices affected by joint. + joint_idx child; // First child Joint; index into Anima's joints. + joint_idx next; // Next sibling Joint; index into Anima's joints. + mat4 transform; // Local transform relative to parent. + mat4 inv_bind_matrix; // Transforms the mesh into the joint's local space. + mat4 joint_matrix; // inv(global) * global joint transform * inv(bind). + aabb3 box; // Bounding box of vertices affected by joint. } Joint; /// Animation skeleton. typedef struct Skeleton { skeleton_idx next; size_t num_joints; - joint_idx joints[GFX_MAX_NUM_JOINTS]; /// Indices into Anima's joints array. + joint_idx joints[GFX_MAX_NUM_JOINTS]; // Indices into Anima's joints array. } Skeleton; /// A keyframe of animation. typedef struct Keyframe { - R time; /// Start time in [0, end animation time] + R time; // Start time in [0, end animation time] union { vec3 translation; quat rotation; @@ -50,7 +49,7 @@ typedef struct Keyframe { /// Animation channel. typedef struct Channel { - joint_idx target; /// Index into Anima's joints array. + joint_idx target; // Index into Anima's joints array. ChannelType type; AnimationInterpolation interpolation; size_t num_keyframes; @@ -89,10 +88,9 @@ typedef struct AnimationState { /// have no parent (a skeleton need not have its own root and can be a set of /// disjoint node hierarchies). typedef struct Anima { - node_idx parent; /// Parent SceneNode. - skeleton_idx skeleton; /// Index of first skeleton. - animation_idx animation; /// Index of first animation. - AnimationState state; /// Current animation state. - size_t num_joints; /// Number of actual joints in the array. - Joint joints[GFX_MAX_NUM_JOINTS]; /// Shared by all skeletons. + skeleton_idx skeleton; // Index of first skeleton. + animation_idx animation; // Index of first animation. + AnimationState state; // Current animation state. + size_t num_joints; // Number of actual joints in the array. + Joint joints[GFX_MAX_NUM_JOINTS]; // Shared by all skeletons. } Anima; diff --git a/src/scene/camera.c b/src/scene/camera.c index bb073ba..475101d 100644 --- a/src/scene/camera.c +++ b/src/scene/camera.c @@ -1,37 +1,21 @@ -#include "camera_impl.h" +#include #include "memory.h" -#include "node_impl.h" #include +#include -SceneCamera* gfx_make_camera() { - SceneCamera* camera = mem_alloc_camera(); - - camera->camera = camera_perspective( +Camera* gfx_make_camera() { + Camera* camera = mem_alloc_camera(); + *camera = camera_perspective( /*fovy=*/90.0 * TO_RAD, /*aspect=*/16.0 / 9.0, /*near=*/0.1, /*far=*/1000); - return camera; } -void gfx_destroy_camera(SceneCamera** camera) { +void gfx_destroy_camera(Camera** camera) { assert(camera); if (*camera) { - if ((*camera)->parent.val) { - gfx_del_node((*camera)->parent); - } mem_free_camera(camera); } } - -void gfx_set_camera_camera(SceneCamera* scene_camera, Camera* camera) { - assert(scene_camera); - assert(camera); - scene_camera->camera = *camera; -} - -Camera* gfx_get_camera_camera(SceneCamera* camera) { - assert(camera); - return &camera->camera; -} diff --git a/src/scene/camera_impl.h b/src/scene/camera_impl.h deleted file mode 100644 index 20c3890..0000000 --- a/src/scene/camera_impl.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -#include "types.h" - -#include - -typedef struct SceneCamera { - Camera camera; - node_idx parent; // Parent SceneNode. -} SceneCamera; diff --git a/src/scene/model_impl.h b/src/scene/model_impl.h index 39ac27f..079aede 100644 --- a/src/scene/model_impl.h +++ b/src/scene/model_impl.h @@ -7,7 +7,6 @@ /// Model. typedef struct Model { node_idx root; - node_idx parent; // Parent SceneNode. } Model; /// Create a new model. diff --git a/src/scene/node.c b/src/scene/node.c index 7cc315c..7032324 100644 --- a/src/scene/node.c +++ b/src/scene/node.c @@ -1,15 +1,15 @@ #include "node_impl.h" #include "animation_impl.h" -#include "camera_impl.h" #include "memory.h" -#include "model_impl.h" #include "object_impl.h" #include "render/llr_impl.h" #include "scene_graph.h" #include "gfx_assert.h" +#include + #include #include @@ -19,7 +19,7 @@ static void scene_node_make(SceneNode* node) { node->transform = mat4_id(); } -SceneNode* gfx_make_node() { +SceneNode* gfx_make_node(void) { SceneNode* node = mem_alloc_node(); scene_node_make(node); return node; @@ -30,16 +30,14 @@ SceneNode* gfx_make_anima_node(Anima* anima) { SceneNode* node = gfx_make_node(); node->type = AnimaNode; node->anima = mem_get_anima_index(anima); - anima->parent = mem_get_node_index(node); return node; } -SceneNode* gfx_make_camera_node(SceneCamera* camera) { +SceneNode* gfx_make_camera_node(Camera* camera) { assert(camera); SceneNode* node = gfx_make_node(); node->type = CameraNode; node->camera = mem_get_camera_index(camera); - camera->parent = mem_get_node_index(node); return node; } @@ -48,7 +46,6 @@ SceneNode* gfx_make_light_node(Light* light) { SceneNode* node = gfx_make_node(); node->type = LightNode; node->light = mem_get_light_index(light); - light->parent = mem_get_node_index(node); return node; } @@ -57,7 +54,6 @@ SceneNode* gfx_make_model_node(Model* model) { SceneNode* node = gfx_make_node(); node->type = ModelNode; node->model = mem_get_model_index(model); - model->parent = mem_get_node_index(node); return node; } @@ -66,7 +62,6 @@ SceneNode* gfx_make_object_node(SceneObject* object) { SceneNode* node = gfx_make_node(); node->type = ObjectNode; node->object = mem_get_object_index(object); - object->parent = mem_get_node_index(node); return node; } @@ -74,24 +69,19 @@ SceneNode* gfx_make_object_node(SceneObject* object) { static void free_node_resource(SceneNode* node) { assert(node); - // Set the resource's parent node back to 0 to avoid a recursive call into - // gfx_del_node(). switch (node->type) { case AnimaNode: { - Anima* anima = mem_get_anima(node->anima); - anima->parent.val = 0; + Anima* anima = mem_get_anima(node->anima); gfx_destroy_anima(&anima); return; } case CameraNode: { - SceneCamera* camera = mem_get_camera(node->camera); - camera->parent.val = 0; + Camera* camera = mem_get_camera(node->camera); gfx_destroy_camera(&camera); return; } case LightNode: { - Light* light = mem_get_light(node->light); - light->parent.val = 0; + Light* light = mem_get_light(node->light); gfx_destroy_light(&light); return; } @@ -100,13 +90,13 @@ static void free_node_resource(SceneNode* node) { } case ObjectNode: { SceneObject* object = mem_get_object(node->object); - object->parent.val = 0; gfx_destroy_object(&object); return; } - case LogicalNode: + case LogicalNode: { return; // Logical nodes have no resource. } + } FAIL("unhandled node type"); } @@ -170,11 +160,11 @@ Anima* gfx_get_node_anima_mut(SceneNode* node) { NODE_GET(node, anima, AnimaNode); } -const SceneCamera* gfx_get_node_camera(const SceneNode* node) { +const Camera* gfx_get_node_camera(const SceneNode* node) { NODE_GET(node, camera, CameraNode); } -SceneCamera* gfx_get_node_camera_mut(SceneNode* node) { +Camera* gfx_get_node_camera_mut(SceneNode* node) { NODE_GET(node, camera, CameraNode); } diff --git a/src/scene/node_impl.h b/src/scene/node_impl.h index fb88bd5..15ee232 100644 --- a/src/scene/node_impl.h +++ b/src/scene/node_impl.h @@ -4,6 +4,7 @@ #include "types.h" +#include #include /// Scene node. @@ -12,9 +13,6 @@ /// together form a strict tree hierarchy and not a more general DAG. typedef struct SceneNode { NodeType type; - // TODO: Inline the actual object here and get rid of the indirection and the - // extra pools? The "scene camera" is kind of a useless data structure, for - // example. union { anima_idx anima; camera_idx camera; diff --git a/src/scene/object.c b/src/scene/object.c index d1f355c..7004ce1 100644 --- a/src/scene/object.c +++ b/src/scene/object.c @@ -57,11 +57,7 @@ SceneObject* gfx_make_object(const ObjectDesc* desc) { void gfx_destroy_object(SceneObject** object) { assert(object); - if (*object) { - if ((*object)->parent.val) { - gfx_del_node((*object)->parent); - } mem_free_object(object); } } diff --git a/src/scene/object_impl.h b/src/scene/object_impl.h index e864e53..b7a0752 100644 --- a/src/scene/object_impl.h +++ b/src/scene/object_impl.h @@ -19,7 +19,5 @@ typedef struct MeshLink { typedef struct SceneObject { mesh_link_idx mesh_link; /// First MeshLink in the list. skeleton_idx skeleton; /// 0 for static objects. - // TODO: Can we just store node indices in nodes, not the contained object? - node_idx parent; /// Parent SceneNode. - aabb3 box; + aabb3 box; } SceneObject; diff --git a/src/scene/scene_graph.h b/src/scene/scene_graph.h index e7135a4..36c3a98 100644 --- a/src/scene/scene_graph.h +++ b/src/scene/scene_graph.h @@ -20,7 +20,7 @@ #define MEM_GET_INDEX(ITEM) \ _Generic( \ (ITEM), \ - SceneCamera *: mem_get_camera_index, \ + Camera *: mem_get_camera_index, \ Material *: mem_get_material_index, \ Mesh *: mem_get_mesh_index, \ MeshLink *: mem_get_mesh_link_index, \ -- cgit v1.2.3