diff options
Diffstat (limited to 'src/scene/object.c')
-rw-r--r-- | src/scene/object.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/scene/object.c b/src/scene/object.c new file mode 100644 index 0000000..e8e3ee6 --- /dev/null +++ b/src/scene/object.c | |||
@@ -0,0 +1,83 @@ | |||
1 | #include "object_impl.h" | ||
2 | |||
3 | #include <gfx/core.h> | ||
4 | |||
5 | #include "mesh_impl.h" | ||
6 | #include "node_impl.h" | ||
7 | #include "scene_memory.h" | ||
8 | |||
9 | #include <assert.h> | ||
10 | |||
11 | static aabb3 calc_object_aabb(const SceneObject* object) { | ||
12 | assert(object); | ||
13 | |||
14 | bool first = true; | ||
15 | aabb3 box; | ||
16 | |||
17 | mesh_link_idx ml = object->mesh_link; | ||
18 | while (ml.val) { | ||
19 | const MeshLink* mesh_link = mem_get_mesh_link(ml); | ||
20 | const mesh_idx mi = mesh_link->mesh; | ||
21 | if (mi.val) { | ||
22 | const Mesh* mesh = mem_get_mesh(mi); | ||
23 | const aabb3 mesh_box = gfx_get_geometry_aabb(mesh->geometry); | ||
24 | if (first) { | ||
25 | box = mesh_box; | ||
26 | first = false; | ||
27 | } else { | ||
28 | box = aabb3_sum(box, mesh_box); | ||
29 | } | ||
30 | } | ||
31 | ml = mesh_link->next; | ||
32 | } | ||
33 | |||
34 | return box; | ||
35 | } | ||
36 | |||
37 | static void add_object_mesh(SceneObject* object, Mesh* mesh) { | ||
38 | assert(object); | ||
39 | assert(mesh); | ||
40 | |||
41 | MeshLink* link = mem_alloc_mesh_link(); | ||
42 | link->mesh = mem_get_mesh_index(mesh); | ||
43 | link->next = object->mesh_link; | ||
44 | object->mesh_link = mem_get_mesh_link_index(link); | ||
45 | } | ||
46 | |||
47 | SceneObject* gfx_make_object(const ObjectDesc* desc) { | ||
48 | assert(desc); | ||
49 | |||
50 | SceneObject* object = mem_alloc_object(); | ||
51 | for (size_t i = 0; i < desc->num_meshes; ++i) { | ||
52 | add_object_mesh(object, desc->meshes[i]); | ||
53 | } | ||
54 | object->box = calc_object_aabb(object); | ||
55 | return object; | ||
56 | } | ||
57 | |||
58 | void gfx_destroy_object(SceneObject** object) { | ||
59 | assert(object); | ||
60 | |||
61 | if (*object) { | ||
62 | if ((*object)->parent.val) { | ||
63 | gfx_del_node((*object)->parent); | ||
64 | } | ||
65 | mem_free_object(object); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | void gfx_set_object_skeleton(SceneObject* object, const Skeleton* skeleton) { | ||
70 | assert(object); | ||
71 | assert(skeleton); | ||
72 | object->skeleton = mem_get_skeleton_index(skeleton); | ||
73 | } | ||
74 | |||
75 | const Skeleton* gfx_get_object_skeleton(const SceneObject* object) { | ||
76 | assert(object); | ||
77 | return (object->skeleton.val == 0) ? 0 : mem_get_skeleton(object->skeleton); | ||
78 | } | ||
79 | |||
80 | aabb3 gfx_get_object_aabb(const SceneObject* object) { | ||
81 | assert(object); | ||
82 | return object->box; | ||
83 | } | ||