From e203dcc88b1b152bc7e0acfe554644571f826a88 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sat, 1 Jul 2023 20:35:55 -0700 Subject: [PATCH] Decompose pose mat4 into offset, quaternion rotation, and scale --- art/Exporter.py | 20 ++++++++++++++++++-- main.c | 33 +++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/art/Exporter.py b/art/Exporter.py index c5b1e12..67c59b0 100644 --- a/art/Exporter.py +++ b/art/Exporter.py @@ -28,6 +28,17 @@ def write_u64(f, number: int): def write_i32(f, number: int): 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): encoded = s.encode("utf8") write_u64(f, len(encoded)) @@ -99,7 +110,12 @@ for o in D.objects: write_string(f, pose_bone.bone.name) write_i32(f, parent_index) #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": @@ -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.y) write_f32(f, blender_scale.z) - \ No newline at end of file + diff --git a/main.c b/main.c index b8a944a..228cca1 100644 --- a/main.c +++ b/main.c @@ -796,6 +796,14 @@ SER_MAKE_FOR_TYPE(AnimKind); SER_MAKE_FOR_TYPE(EntityRef); 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 { Vec3 pos; @@ -911,6 +919,16 @@ Mat4 blender_to_handmade_mat(BlenderMat b) memcpy(&to_return, &b, sizeof(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 { @@ -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]; - BlenderMat parent_space_pose; MD_i32 parent_index; ser_MD_String8(&ser, &next_pose_bone->name, arena); 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 < 0 || parent_index >= to_return.poses_length) @@ -2548,17 +2568,14 @@ static struct int num_draw_calls = 0; int num_vertices = 0; + void draw_placed(Mat4 view, Mat4 projection, PlacedMesh *cur) { Mesh *drawing = cur->draw_with; state.threedee_bind.vertex_buffers[0] = drawing->loaded_buffer; sg_apply_bindings(&state.threedee_bind); - Mat4 model = M4D(1.0f); - - model = MulM4(Scale(cur->t.scale), model); - model = MulM4(QToM4(cur->t.rotation), model); - model = MulM4(Translate(cur->t.offset), model); + Mat4 model = transform_to_mat(cur->t); threedee_vs_params_t params = {0}; memcpy(params.model, (float*)&model, sizeof(model));