3 #include "engine/logger.h"
7 Scene* currentScene = NULL;
11 Transform identity() {
12 return (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = 0.0f,
13 .b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = 0.0f,
14 .c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = 0.0f,
15 .d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f };
18 void multiply(Transform* transform, Transform by) {
19 GLfloat* a = (GLfloat*) &by;
20 GLfloat* b = (GLfloat*) transform;
22 for (size_t row = 0; row < 4; ++row) {
23 for (size_t col = 0; col < 4; ++col) {
25 a[(row * 4) + 0] * b[(0 * 4) + col]
26 + a[(row * 4) + 1] * b[(1 * 4) + col]
27 + a[(row * 4) + 2] * b[(2 * 4) + col]
28 + a[(row * 4) + 3] * b[(3 * 4) + col];
33 void translate(Transform* transform, Vector3D vec) {
34 multiply(transform, (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = vec.x,
35 .b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = vec.y,
36 .c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = vec.z,
37 .d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f });
40 Vector3D translationOf(Transform transform) {
41 return (Vector3D) { transform.a4, transform.b4, transform.c4 };
45 Scene* scene = malloc(sizeof(Scene));
46 *scene = (Scene) { .parent = NULL,
49 .transform = identity(),
54 void insertChildScene(Scene* scene, Scene* newChild) {
55 if (!scene || !newChild) {
58 if (newChild->parent) {
60 "Cannot insert scene 0x%p as child of 0x%p, because it is already child of 0x%p",
67 scene->children = realloc(scene->children, ++(scene->numChildren) * sizeof(Scene*));
68 scene->children[scene->numChildren - 1] = newChild;
70 newChild->parent = scene;
73 void reparentScene(Scene* scene, Scene* newParent) {
79 Scene* parent = scene->parent;
80 size_t indexInParent = parent->numChildren;
81 for (size_t i = 0; i < parent->numChildren; ++i) {
82 if (parent->children[i] == scene) {
87 // This includes the hopefully impossible case where parent->numChildren == 0
88 if (indexInParent == parent->numChildren) {
89 logError("Scene %p not found in children of parent %p", scene, parent);
92 for (size_t i = indexInParent; i < parent->numChildren - 1; ++i) {
93 parent->children[i] = parent->children[i + 1];
95 parent->children = realloc(parent->children, --(parent->numChildren) * sizeof(Scene*));
101 insertChildScene(newParent, scene);