Decompose pose mat4 into offset, quaternion rotation, and scale

main
parent c3738d2f8c
commit e203dcc88b

@ -28,6 +28,17 @@ def write_u64(f, number: int):
def write_i32(f, number: int): def write_i32(f, number: int):
f.write(bytes(struct.pack("i", number))) f.write(bytes(struct.pack("i", number)))
def write_v3(f, vector):
write_f32(f, vector.x)
write_f32(f, vector.y)
write_f32(f, vector.z)
def write_quat(f, quat):
write_f32(f, quat.x)
write_f32(f, quat.y)
write_f32(f, quat.z)
write_f32(f, quat.w)
def write_string(f, s: str): def write_string(f, s: str):
encoded = s.encode("utf8") encoded = s.encode("utf8")
write_u64(f, len(encoded)) write_u64(f, len(encoded))
@ -99,7 +110,12 @@ for o in D.objects:
write_string(f, pose_bone.bone.name) write_string(f, pose_bone.bone.name)
write_i32(f, parent_index) write_i32(f, parent_index)
#parent_space_pose = mapping @ pose_bone.matrix #parent_space_pose = mapping @ pose_bone.matrix
write_4x4matrix(f, parent_space_pose) translation = parent_space_pose.to_translation()
rotation = parent_space_pose.to_quaternion()
scale = parent_space_pose.to_scale()
write_v3(f, translation)
write_quat(f, rotation)
write_v3(f, scale)
elif o.type == "MESH": elif o.type == "MESH":
@ -200,4 +216,4 @@ with open(bpy.path.abspath(f"//{EXPORT_DIRECTORY}/{LEVEL_EXPORT_NAME}.bin"), "wb
write_f32(f, blender_scale.x) write_f32(f, blender_scale.x)
write_f32(f, blender_scale.y) write_f32(f, blender_scale.y)
write_f32(f, blender_scale.z) write_f32(f, blender_scale.z)

@ -796,6 +796,14 @@ SER_MAKE_FOR_TYPE(AnimKind);
SER_MAKE_FOR_TYPE(EntityRef); SER_MAKE_FOR_TYPE(EntityRef);
SER_MAKE_FOR_TYPE(NPCPlayerStanding); SER_MAKE_FOR_TYPE(NPCPlayerStanding);
void ser_Quat(SerState *ser, Quat *q)
{
ser_float(ser, &q->x);
ser_float(ser, &q->y);
ser_float(ser, &q->z);
ser_float(ser, &q->w);
}
typedef struct typedef struct
{ {
Vec3 pos; Vec3 pos;
@ -911,6 +919,16 @@ Mat4 blender_to_handmade_mat(BlenderMat b)
memcpy(&to_return, &b, sizeof(to_return)); memcpy(&to_return, &b, sizeof(to_return));
return TransposeM4(to_return); return TransposeM4(to_return);
} }
Mat4 transform_to_mat(Transform t)
{
Mat4 to_return = M4D(1.0f);
to_return = MulM4(Scale(t.scale), to_return);
to_return = MulM4(QToM4(t.rotation), to_return);
to_return = MulM4(Translate(t.offset), to_return);
return to_return;
}
typedef struct PoseBone typedef struct PoseBone
{ {
@ -969,14 +987,16 @@ Armature load_armature(MD_Arena *arena, MD_String8 binary_file, MD_String8 armat
{ {
PoseBone *next_pose_bone = &to_return.poses[i]; PoseBone *next_pose_bone = &to_return.poses[i];
BlenderMat parent_space_pose;
MD_i32 parent_index; MD_i32 parent_index;
ser_MD_String8(&ser, &next_pose_bone->name, arena); ser_MD_String8(&ser, &next_pose_bone->name, arena);
ser_int(&ser, &parent_index); ser_int(&ser, &parent_index);
ser_BlenderMat(&ser, &parent_space_pose); Transform t;
ser_Vec3(&ser, &t.offset);
ser_Quat(&ser, &t.rotation);
ser_Vec3(&ser, &t.scale);
next_pose_bone->parent_space_pose = blender_to_handmade_mat(parent_space_pose); next_pose_bone->parent_space_pose = transform_to_mat(t);
if(parent_index != -1) if(parent_index != -1)
{ {
if(parent_index < 0 || parent_index >= to_return.poses_length) if(parent_index < 0 || parent_index >= to_return.poses_length)
@ -2548,17 +2568,14 @@ static struct
int num_draw_calls = 0; int num_draw_calls = 0;
int num_vertices = 0; int num_vertices = 0;
void draw_placed(Mat4 view, Mat4 projection, PlacedMesh *cur) void draw_placed(Mat4 view, Mat4 projection, PlacedMesh *cur)
{ {
Mesh *drawing = cur->draw_with; Mesh *drawing = cur->draw_with;
state.threedee_bind.vertex_buffers[0] = drawing->loaded_buffer; state.threedee_bind.vertex_buffers[0] = drawing->loaded_buffer;
sg_apply_bindings(&state.threedee_bind); sg_apply_bindings(&state.threedee_bind);
Mat4 model = M4D(1.0f); Mat4 model = transform_to_mat(cur->t);
model = MulM4(Scale(cur->t.scale), model);
model = MulM4(QToM4(cur->t.rotation), model);
model = MulM4(Translate(cur->t.offset), model);
threedee_vs_params_t params = {0}; threedee_vs_params_t params = {0};
memcpy(params.model, (float*)&model, sizeof(model)); memcpy(params.model, (float*)&model, sizeof(model));

Loading…
Cancel
Save