From be92b31d04502a21bf5b5895f62a575d89fc7cfb Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 31 May 2018 20:59:40 +0200 Subject: [PATCH] Humanilation --- CGII/src/Bone.cpp | 51 +++++++++++++++++++++++++++++-------- CGII/src/SkeletonViewer.cpp | 12 ++++++--- CGII/src/SkeletonViewer.h | 1 + 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/CGII/src/Bone.cpp b/CGII/src/Bone.cpp index dcd65a2..e3eef41 100644 --- a/CGII/src/Bone.cpp +++ b/CGII/src/Bone.cpp @@ -50,23 +50,47 @@ void Bone::calculate_matrices() { orientationTransformGlobalToLocal.identity(); std::for_each(orientation.begin(), orientation.end(), [&](AtomicTransform *t) { - std::cout << name << " orientation " << t->get_name() << ": " << t->get_value() << std::endl; + //std::cout << name << " orientation " << t->get_name() << ": " << t->get_value() << std::endl; orientationTransformGlobalToLocal = orientationTransformGlobalToLocal * t->calculate_matrix(); }); orientationTransformLocalToGlobal = cgv::math::inv(orientationTransformGlobalToLocal); - std::cout << name << " direction " << from_Vec3(direction_in_world_space) << std::endl; - std::cout << name << " length " << length << std::endl; + //std::cout << name << " direction " << from_Vec3(direction_in_world_space) << std::endl; + //std::cout << name << " length " << length << std::endl; /*Task 2.1: Implement matrix calculation */ Vec3 vector = direction_in_world_space * length; - Mat4 matrix; - matrix.identity(); - + //translationTransformCurrentJointToNext = orientationTransformLocalToGlobal * translate(vector) * orientationTransformGlobalToLocal; translationTransformCurrentJointToNext = translate(vector); - orientationTransformPrevJointToCurrent = matrix; + + if (parent == nullptr) + { + orientationTransformPrevJointToCurrent.identity(); + } + else + { + Vec3 prev_dir = parent->get_direction_in_world_space(); + Vec3 &curr_dir = direction_in_world_space; + + if (prev_dir.x() == 0.0f && prev_dir.y() == 0.0f && prev_dir.z() == 0.0f) + { + orientationTransformPrevJointToCurrent.identity(); + } + else if (prev_dir == curr_dir) + { + orientationTransformPrevJointToCurrent.identity(); + } + else + { + + float angle = calc_angle(curr_dir, prev_dir); + Vec3 axis = cross(curr_dir, prev_dir); + + orientationTransformPrevJointToCurrent = rotate(axis, angle); + } + } /*Task 4.6: Implement matrix calculation */ } @@ -74,6 +98,13 @@ void Bone::calculate_matrices() Mat4 Bone::calculate_transform_prev_to_current_with_dofs() { //Task 2.1: Implement matrix calculation + if (parent == NULL) + { + Mat4 t; + t.identity(); + return t; + } + Mat4 dofs_matrix; dofs_matrix.identity(); @@ -81,15 +112,13 @@ Mat4 Bone::calculate_transform_prev_to_current_with_dofs() dofs_matrix = dofs_matrix * t->calculate_matrix(); }); - Mat4 t = translationTransformCurrentJointToNext * orientationTransformGlobalToLocal * dofs_matrix * orientationTransformPrevJointToCurrent * orientationTransformLocalToGlobal; - return t; + return parent->get_translation_transform_current_joint_to_next() * orientationTransformPrevJointToCurrent * dofs_matrix; } Mat4 Bone::calculate_transform_prev_to_current_without_dofs() { //Task 2.1: Implement matrix calculation - Mat4 t = translationTransformCurrentJointToNext * orientationTransformLocalToGlobal * orientationTransformPrevJointToCurrent * orientationTransformGlobalToLocal; - return t; + return parent->get_translation_transform_current_joint_to_next() * orientationTransformPrevJointToCurrent; } Mat4 Bone::get_total_orientation() diff --git a/CGII/src/SkeletonViewer.cpp b/CGII/src/SkeletonViewer.cpp index 0851e5c..3396477 100644 --- a/CGII/src/SkeletonViewer.cpp +++ b/CGII/src/SkeletonViewer.cpp @@ -35,6 +35,7 @@ SkeletonViewer::SkeletonViewer(DataStore *data) } frame = 0; + inc_frame = false; } void draw_dof(Bone *node, Vec4 start_point) @@ -135,7 +136,7 @@ void SkeletonViewer::draw_skeleton_subtree(Bone *node, const Mat4 &global_to_par } } - Mat4 local_transform = node->calculate_transform_prev_to_current_with_dofs() * global_to_parent_local; + Mat4 local_transform = global_to_parent_local * node->calculate_transform_prev_to_current_with_dofs(); Vec4 zero = Vec4(0.0f, 0.0f, 0.0f, 1.0f); @@ -165,11 +166,14 @@ void SkeletonViewer::timer_event(double, double dt) void SkeletonViewer::start_animation() { - //Task: Implement animation + frame = 0; + inc_frame = true; + post_redraw(); } void SkeletonViewer::stop_animation() { + inc_frame = false; //Task: Implement animation } @@ -458,7 +462,9 @@ void SkeletonViewer::draw(context &c) if (data->get_skeleton() != nullptr) draw_skeleton_subtree(data->get_skeleton()->get_root(), data->get_skeleton()->get_origin(), c, 0); - frame++; + if (inc_frame) + frame++; + if (frame >= animations.size()) frame = 0; } \ No newline at end of file diff --git a/CGII/src/SkeletonViewer.h b/CGII/src/SkeletonViewer.h index c5f64be..23f6185 100644 --- a/CGII/src/SkeletonViewer.h +++ b/CGII/src/SkeletonViewer.h @@ -39,6 +39,7 @@ class SkeletonViewer : public node, public drawable, public provider // Maps gui elements in the tree view to a specific bone std::map gui_to_bone; + bool inc_frame; int frame; // slot for the signal