60 lines
1.1 KiB
C
60 lines
1.1 KiB
C
enum { MAX_NODE_CHILDREN = 3 };
|
|
|
|
typedef struct transform {
|
|
vec3 position;
|
|
vec3 rotation;
|
|
vec3 scale;
|
|
} transform_t;
|
|
|
|
typedef struct node {
|
|
transform_t transform;
|
|
int parent;
|
|
int children[MAX_NODE_CHILDREN];
|
|
mat4 matrix;
|
|
mat4 rotation;
|
|
} node_t;
|
|
|
|
mat4
|
|
calc_matrix(node_t* node, transform_t transform)
|
|
{
|
|
mat4 translation = mat4_identity();
|
|
translation.e03 = transform.position.x;
|
|
translation.e13 = transform.position.y;
|
|
translation.e23 = transform.position.z;
|
|
|
|
mat4 rotation = node->rotation;
|
|
|
|
mat4 scale = mat4_identity();
|
|
scale.e00 = transform.scale.x;
|
|
scale.e11 = transform.scale.y;
|
|
scale.e22 = transform.scale.z;
|
|
|
|
mat4 result = mat4_multm(rotation, scale);
|
|
result = mat4_multm(translation, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
void
|
|
update_node(node_t* nodes, node_t* node)
|
|
{
|
|
if (node->parent != -1)
|
|
{
|
|
node_t* parent = &nodes[node->parent];
|
|
|
|
node->matrix = mat4_multm(parent->matrix, calc_matrix(node, node->transform));
|
|
}
|
|
else
|
|
{
|
|
node->matrix = calc_matrix(node, node->transform);
|
|
}
|
|
|
|
for (int i = 0; i < MAX_NODE_CHILDREN; i++)
|
|
{
|
|
if (node->children[i] != -1)
|
|
{
|
|
update_node(nodes, &nodes[node->children[i]]);
|
|
}
|
|
}
|
|
}
|