Made a big mess of the codebase

This commit is contained in:
2025-08-28 07:14:13 -05:00
parent 30232d1275
commit af1329710d
44 changed files with 1363 additions and 388 deletions

View File

@@ -6,7 +6,6 @@
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
node.c
)
# Subdirs

View File

@@ -1,129 +0,0 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "node.h"
#include "util/memory.h"
#include "assert/assert.h"
#include "display/display.h"
node_t NODE_DATA[ECS_ENTITY_COUNT_MAX] = { 0 };
ecscomponent_t NODE_COMPONENT = ecsComponentInit(
NODE_DATA,
((ecscomponentcallbacks_t){
.init = nodeInit,
.entityAdd = nodeEntityAdded,
.entityRemove = nodeEntityRemoved
})
);
void nodeInit(void) {
}
void nodeEntityAdded(const ecsid_t id) {
glm_mat4_identity(NODE_DATA[id].transform);
}
void nodeEntityRemoved(const ecsid_t id) {
}
void nodeMatrixGet(const ecsid_t id, mat4 dest) {
node_t *node;
if(nodeHas(id)) {
node = &NODE_DATA[id];
} else {
node = nodeAdd(id);
}
glm_mat4_copy(node->transform, dest);
}
void nodeMatrixSet(const ecsid_t id, mat4 in) {
node_t *node;
if(nodeHas(id)) {
node = &NODE_DATA[id];
} else {
node = nodeAdd(id);
}
glm_mat4_copy(in, node->transform);
// Extract position, scale, rotation from the matrix.
node->position[0] = in[3][0];
node->position[1] = in[3][1];
node->position[2] = in[3][2];
node->scale[0] = glm_vec3_norm((vec3){ in[0][0], in[0][1], in[0][2] });
node->scale[1] = glm_vec3_norm((vec3){ in[1][0], in[1][1], in[1][2] });
node->scale[2] = glm_vec3_norm((vec3){ in[2][0], in[2][1], in[2][2] });
// Remove scale from the matrix to extract rotation.
if(node->scale[0] != 0.0f) {
in[0][0] /= node->scale[0];
in[0][1] /= node->scale[0];
in[0][2] /= node->scale[0];
glm_vec3_copy(in[0], node->rotation);
glm_vec3_copy(in[1], node->rotation);
glm_vec3_copy(in[2], node->rotation);
node->rotation[1] = asinf(-in[0][2]);
if (cosf(node->rotation[1]) != 0.0f) {
node->rotation[0] = atan2f(in[1][2], in[2][2]);
node->rotation[2] = atan2f(in[0][1], in[0][0]);
} else {
node->rotation[0] = 0.0f;
node->rotation[2] = atan2f(-in[1][0], in[1][1]);
}
} else {
node->rotation[0] = 0.0f;
node->rotation[1] = 0.0f;
node->rotation[2] = 0.0f;
}
}
void nodePositionGet(const ecsid_t id, vec3 out) {
node_t *node;
if(nodeHas(id)) {
node = &NODE_DATA[id];
} else {
node = nodeAdd(id);
}
glm_vec3_copy(node->position, out);
}
void nodeMatrixUpdate(const ecsid_t id) {
node_t *node;
if(nodeHas(id)) {
node = &NODE_DATA[id];
} else {
node = nodeAdd(id);
}
glm_mat4_identity(node->transform);
mat4 rot;
glm_euler(node->rotation, rot);
glm_mat4_mul(node->transform, rot, node->transform);
// glm_scale(node->transform, node->scale);
}
void nodeMatrixPush(const ecsid_t id) {
assertTrue(nodeHas(id), "Not a node component");
node_t *node = nodeGet(id);
#if DISPLAY_SDL2
glPushMatrix();
glMultMatrixf((const GLfloat*)node->transform);
#endif
}
void nodeMatrixPop(void) {
#if DISPLAY_SDL2
glPopMatrix();
#endif
}

View File

@@ -1,83 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "ecs/ecscomponent.h"
#define SCENE_ITEM_CHILD_MAX 16
typedef struct {
mat4 transform;
vec3 position;
vec3 scale;
vec3 rotation; // Euler angles in radians
} node_t;
extern node_t NODE_DATA[ECS_ENTITY_COUNT_MAX];
extern ecscomponent_t NODE_COMPONENT;
#define nodeHas(id) ecsComponentDataHas(&NODE_COMPONENT, id)
#define nodeGet(id) \
((node_t*)ecsComponentDataGet(&NODE_COMPONENT, id))
#define nodeAdd(id) \
((node_t*)ecsComponentDataAdd(&NODE_COMPONENT, id))
#define nodeRemove(id) ecsComponentDataRemove(&NODE_COMPONENT, id)
/**
* Initialize the node component.
*/
void nodeInit(void);
/**
* Callback for when an entity is added to the ECS.
*
* @param id The ID of the entity being added.
*/
void nodeEntityAdded(const ecsid_t id);
/**
* Callback for when an entity is removed from the ECS.
*
* @param id The ID of the entity being removed.
*/
void nodeEntityRemoved(const ecsid_t id);
/**
* Get the local transformation matrix of a node.
*
* @param id The ID of the node.
* @param out Pointer to a mat4 where the local matrix will be stored.
*/
void nodeMatrixGet(const ecsid_t id, mat4 out);
/**
* Set the local transformation matrix of a node.
*
* @param id The ID of the node.
* @param in Pointer to a mat4 containing the new local matrix.
*/
void nodeMatrixSet(const ecsid_t id, mat4 in);
/**
* Get the local position of a node.
*
* @param id The ID of the node.
*/
void nodeMatrixUpdate(const ecsid_t id);
/**
* Push the node's transformation matrix onto the OpenGL matrix stack.
*
* @param id The ID of the node.
*/
void nodeMatrixPush(const ecsid_t id);
/**
* Pop the last transformation matrix from the OpenGL matrix stack.
*/
void nodeMatrixPop(void);

View File

@@ -6,38 +6,10 @@
*/
#include "scenetest.h"
#include "scene/node.h"
#include "display/camera.h"
#include "display/mesh/meshrenderer.h"
#include "display/mesh/quad.h"
texture_t test;
void sceneTestAdd(void) {
// Initialize the entity with a camera component
ecsid_t camera = ecsEntityAdd();
node_t *node = nodeAdd(camera);
camera_t *camData = cameraAdd(camera);
mat4 lookAt;
glm_lookat(
(vec3){ 3.0f, 3.0f, 3.0f },
(vec3){ 0.0f, 0.0f, 0.0f },
(vec3){ 0.0f, 1.0f, 0.0f },
lookAt
);
nodeMatrixSet(camera, lookAt);
// color4b_t pixels[4] = {
// COLOR_RED, COLOR_GREEN,
// COLOR_BLUE, COLOR_WHITE
// };
// textureInit(&test, 2, 2, TEXTURE_FORMAT_RGBA, pixels);
// Test cube
ecsid_t cube = ecsEntityAdd();
node = nodeAdd(cube);
meshrenderer_t *renderer = meshRendererAdd(cube);
renderer->mesh = &QUAD_MESH_SIMPLE;
renderer->texture = &test;
}

View File

@@ -6,7 +6,6 @@
*/
#pragma once
#include "ecs/ecssystem.h"
#include "display/texture/texture.h"
extern texture_t test;