70 lines
1.6 KiB
C
70 lines
1.6 KiB
C
|
mat4
|
||
|
calc_matrix(transform_t* transform)
|
||
|
{
|
||
|
mat4 translation = mat4_identity();
|
||
|
translation.e03 = transform->position.x;
|
||
|
translation.e13 = transform->position.y;
|
||
|
translation.e23 = transform->position.z;
|
||
|
|
||
|
mat4 rotation;
|
||
|
{
|
||
|
mat4 rotation_x = mat4_identity();
|
||
|
{
|
||
|
float cos_angle = cosf(transform->rotation.x);
|
||
|
float sin_angle = sinf(transform->rotation.x);
|
||
|
|
||
|
rotation_x.e11 = cos_angle;
|
||
|
rotation_x.e12 = -sin_angle;
|
||
|
rotation_x.e21 = sin_angle;
|
||
|
rotation_x.e22 = cos_angle;
|
||
|
}
|
||
|
|
||
|
mat4 rotation_y = mat4_identity();
|
||
|
{
|
||
|
float cos_angle = cosf(transform->rotation.y);
|
||
|
float sin_angle = sinf(transform->rotation.y);
|
||
|
|
||
|
rotation_y.e00 = cos_angle;
|
||
|
rotation_y.e02 = sin_angle;
|
||
|
rotation_y.e20 = -sin_angle;
|
||
|
rotation_y.e22 = cos_angle;
|
||
|
}
|
||
|
|
||
|
mat4 rotation_z = mat4_identity();
|
||
|
{
|
||
|
float cos_angle = cosf(transform->rotation.z);
|
||
|
float sin_angle = sinf(transform->rotation.z);
|
||
|
|
||
|
rotation_z.e00 = cos_angle;
|
||
|
rotation_z.e01 = -sin_angle;
|
||
|
rotation_z.e10 = sin_angle;
|
||
|
rotation_z.e11 = cos_angle;
|
||
|
}
|
||
|
|
||
|
rotation = mat4_multm(rotation_z, mat4_multm(rotation_y, rotation_x));
|
||
|
}
|
||
|
|
||
|
mat4 scale = mat4_identity();
|
||
|
scale.e00 = transform->scale.x;
|
||
|
scale.e11 = transform->scale.y;
|
||
|
scale.e22 = transform->scale.z;
|
||
|
|
||
|
mat4 result = mat4_multm(translation, mat4_multm(rotation, scale));
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
update_transform(transform_t* transform)
|
||
|
{
|
||
|
transform_t* parent = get_transform(transform->parent);
|
||
|
transform->matrix = mat4_multm(parent->matrix, calc_matrix(transform));
|
||
|
|
||
|
for (size_t i = 0; i < transform->child_count; i++)
|
||
|
{
|
||
|
transform_t* child = get_transform(transform->children[i]);
|
||
|
update_transform(child);
|
||
|
}
|
||
|
}
|
||
|
|