mostly migrated for c compatibility

This commit is contained in:
Daniel Ledda
2025-11-11 04:31:20 +01:00
parent 6958228da7
commit 344056744d
14 changed files with 3050 additions and 355 deletions

View File

@@ -1,54 +1,52 @@
#include <cstring>
#include "scene.h"
Entity *getEntity(Scene *s, uint32 id) {
return &s->entities[id - 1];
return &s->entities.data[id - 1];
}
SceneGraphNode *getSceneGraphNodeForEntity(Scene *s, uint32 entityHandle) {
return &s->graphNodes[s->entities[entityHandle - 1].graphNodeHandle - 1];
return &s->graphNodes.data[s->entities.data[entityHandle - 1].graphNodeHandle - 1];
}
SceneGraphNode *getSceneGraphNode(Scene *s, uint32 sceneGraphNodeHandle) {
return &s->graphNodes[sceneGraphNodeHandle - 1];
return &s->graphNodes.data[sceneGraphNodeHandle - 1];
}
uint32 createEntity(Scene *s) {
s->entities.emplace_back();
s->graphNodes.emplace_back();
s->entities.back().graphNodeHandle = (uint32)s->graphNodes.size();
s->graphNodes.back().entityHandle = (uint32)s->entities.size();
uint32 handle = (uint32)s->entities.size();
uint32 graphNodeHandle = (uint32)s->graphNodes.size();
uint32 createSceneGraphNode(Arena *arena, Scene *s) {
appendList(&s->graphNodes, (SceneGraphNode){0});
s->graphNodes.data[s->graphNodes.head - 1].children = PushList(arena, uint32, 64);
return (uint32)s->graphNodes.head;
}
uint32 createEntity(Arena *arena, Scene *s) {
appendList(&s->entities, (Entity){0});
uint32 graphNodeId = createSceneGraphNode(arena, s);
s->entities.data[s->entities.head - 1].graphNodeHandle = graphNodeId;
getSceneGraphNode(s, graphNodeId)->entityHandle = (uint32)s->entities.head;
uint32 handle = (uint32)s->entities.head;
uint32 graphNodeHandle = (uint32)s->graphNodes.head;
initGraphNode(getSceneGraphNode(s, graphNodeHandle));
return handle;
}
uint32 createSceneGraphNode(Scene *s) {
s->graphNodes.emplace_back();
return (uint32)s->graphNodes.size();
}
void initGraphNode(SceneGraphNode *n) {
n->scale = glm::vec3(1.0f, 1.0f, 1.0f);
n->translation = glm::vec3(0.0f, 0.0f, 0.0f);
n->rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
n->local = glm::mat4(1.0f);
n->scale = RLVector3{1.0f, 1.0f, 1.0f};
n->translation = RLVector3{0.0f, 0.0f, 0.0f};
n->rotation = Quaternion{1.0f, 0.0f, 0.0f, 0.0f};
n->local = MatrixIdentity();
n->world = n->local;
}
void recalcGraphNode(SceneGraphNode *n) {
n->local = glm::scale(
glm::translate(
glm::mat4(1.0f),
n->translation
) * glm::toMat4(n->rotation),
n->scale
);
n->local = MatrixCompose(n->translation, n->rotation, n->scale);
}
Scene createScene() {
Scene createScene(Arena *arena) {
Scene result = {};
result.sceneRoot = createSceneGraphNode(&result);
result.entities = PushList(arena, Entity, 1024);
result.graphNodes = PushList(arena, SceneGraphNode, 1024);
result.sceneRoot = createSceneGraphNode(arena, &result);
return result;
}
@@ -57,11 +55,12 @@ void recalcSceneGraphNode(Scene *s, uint32 parentHandle) {
if (node->entityHandle) {
getEntity(s, node->entityHandle)->flags |= EntityFlags_Render;
}
for (uint32 &nodeId : node->children) {
for (EachIn(node->children, i)) {
uint32 nodeId = node->children.data[i];
SceneGraphNode *graphNode = getSceneGraphNode(s, nodeId);
graphNode->parentHandle = parentHandle;
recalcGraphNode(graphNode);
graphNode->world = node->world * graphNode->local;
graphNode->world = MatrixMultiply(graphNode->local, node->world);
recalcSceneGraphNode(s, nodeId);
}
}
@@ -76,9 +75,10 @@ void removeEntity(Scene *s, uint32 entityHandle) {
SceneGraphNode *graphNode = getSceneGraphNode(s, entity->graphNodeHandle);
if (graphNode->parentHandle) {
SceneGraphNode *parentNode = getSceneGraphNode(s, graphNode->parentHandle);
for (int i = 0; i < parentNode->children.size(); i++) {
if (parentNode->children.at(i) == entity->graphNodeHandle) {
parentNode->children.erase(parentNode->children.begin() + i);
for (int i = 0; i < parentNode->children.head; i++) {
if (parentNode->children.data[i] == entity->graphNodeHandle) {
memcpy(&parentNode->children.data[i], &parentNode->children.data[i + 1], parentNode->children.head - i);
parentNode->children.head -= 1;
graphNode->parentHandle = 0;
break;
}
@@ -87,5 +87,7 @@ void removeEntity(Scene *s, uint32 entityHandle) {
}
void sceneNodeAddEntity(Scene *s, uint32 graphNodeHandle, uint32 entityHandle) {
getSceneGraphNode(s, graphNodeHandle)->children.push_back(getEntity(s, entityHandle)->graphNodeHandle);
list<uint32> *childList = &getSceneGraphNode(s, graphNodeHandle)->children;
appendList(childList, getEntity(s, entityHandle)->graphNodeHandle);
print("%zu HI\n", childList->length);
}

View File

@@ -1,7 +1,5 @@
#include <vector>
#include "glm/glm.hpp"
#include <glm/gtx/quaternion.hpp>
#include "../gfx/gfx.h"
#include "../lib/raymath.h"
enum EntityFlags {
EntityFlags_Visible=1<<0,
@@ -17,27 +15,27 @@ struct Entity {
};
struct SceneGraphNode {
glm::mat4 local;
glm::mat4 world;
glm::vec3 translation;
glm::quat rotation;
glm::vec3 scale;
std::vector<uint32> children;
Matrix local;
Matrix world;
RLVector3 translation;
Quaternion rotation;
RLVector3 scale;
list<uint32> children;
uint32 entityHandle;
uint32 parentHandle;
};
struct Scene {
uint32 sceneRoot;
std::vector<Entity> entities;
std::vector<SceneGraphNode> graphNodes;
list<Entity> entities;
list<SceneGraphNode> graphNodes;
};
uint32 createEntity(Scene *s);
uint32 createEntity(Arena *arena, Scene *s);
Entity *getEntity(Scene *s);
SceneGraphNode *getSceneGraphNode(Scene *s, int id);
uint32 createSceneGraphNode(Scene *s);
Scene createScene(Scene *s);
uint32 createSceneGraphNode(Arena *arena, Scene *s);
Scene createScene(Arena *arena);
void initGraphNode(SceneGraphNode *n);
void recalcGraphNode(SceneGraphNode *n);
void recalcSceneGraphNode(Scene *s, uint32 parentHandle);