skeleton.cpp 3.39 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3
4
5
6
7
8
9
10
11
12

#include "../scene/skeleton.h"

Vec3 closest_on_line_segment(Vec3 start, Vec3 end, Vec3 point) {

    // TODO(Animation): Task 3

    // Return the closest point to 'point' on the line segment from start to end
    return Vec3{};
}

Mat4 Joint::joint_to_bind() const {
TheNumbat's avatar
TheNumbat committed
13

TheNumbat's avatar
TheNumbat committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    // TODO(Animation): Task 2

    // Return a matrix transforming points in the space of this joint
    // to points in mesh space in bind position.

    // Bind position implies that all joints have pose = Vec3{0.0f}

    // You will need to traverse the joint heirarchy. This should
    // not take into account Skeleton::base_pos
    return Mat4::I;
}

Mat4 Joint::joint_to_posed() const {

    // TODO(Animation): Task 2

    // Return a matrix transforming points in the space of this joint
    // to points in mesh space, taking into account joint poses.

    // You will need to traverse the joint heirarchy. This should
    // not take into account Skeleton::base_pos
    return Mat4::I;
}

TheNumbat's avatar
TheNumbat committed
38
Vec3 Skeleton::end_of(Joint *j) {
TheNumbat's avatar
TheNumbat committed
39
40

    // TODO(Animation): Task 2
TheNumbat's avatar
TheNumbat committed
41

TheNumbat's avatar
TheNumbat committed
42
43
44
45
46
    // Return the position of the endpoint of joint j in mesh space in bind position.
    // This should take into account Skeleton::base_pos.
    return Vec3{};
}

TheNumbat's avatar
TheNumbat committed
47
Vec3 Skeleton::posed_end_of(Joint *j) {
TheNumbat's avatar
TheNumbat committed
48
49

    // TODO(Animation): Task 2
TheNumbat's avatar
TheNumbat committed
50

TheNumbat's avatar
TheNumbat committed
51
52
53
54
55
    // Return the position of the endpoint of joint j in mesh space with poses.
    // This should take into account Skeleton::base_pos.
    return Vec3{};
}

TheNumbat's avatar
TheNumbat committed
56
Mat4 Skeleton::joint_to_bind(const Joint *j) const {
TheNumbat's avatar
TheNumbat committed
57
58

    // TODO(Animation): Task 2
TheNumbat's avatar
TheNumbat committed
59

TheNumbat's avatar
TheNumbat committed
60
61
62
63
64
    // Return a matrix transforming points in joint j's space to mesh space in
    // bind position. This should take into account Skeleton::base_pos.
    return Mat4::I;
}

TheNumbat's avatar
TheNumbat committed
65
Mat4 Skeleton::joint_to_posed(const Joint *j) const {
TheNumbat's avatar
TheNumbat committed
66
67

    // TODO(Animation): Task 2
TheNumbat's avatar
TheNumbat committed
68

TheNumbat's avatar
TheNumbat committed
69
70
71
72
73
    // Return a matrix transforming points in joint j's space to mesh space with
    // poses. This should take into account Skeleton::base_pos.
    return Mat4::I;
}

TheNumbat's avatar
TheNumbat committed
74
75
void Skeleton::find_joints(const GL::Mesh &mesh,
                           std::unordered_map<unsigned int, std::vector<Joint *>> &map) {
TheNumbat's avatar
TheNumbat committed
76
77
78
79
80
81
82

    // TODO(Animation): Task 3

    // Construct a mapping from vertex indices in 'mesh' to lists of joints in this skeleton
    // that should effect the vertex. A joint should effect a vertex if it is within Joint::radius
    // distance of the bone segment in bind position.

TheNumbat's avatar
TheNumbat committed
83
    const std::vector<GL::Mesh::Vert> &verts = mesh.verts();
TheNumbat's avatar
TheNumbat committed
84

TheNumbat's avatar
TheNumbat committed
85
    for_joints([&](Joint *j) {
TheNumbat's avatar
TheNumbat committed
86
87
88
        // What vertices does joint j effect?
    });

TheNumbat's avatar
TheNumbat committed
89
    // For each i in [0,verts.size()), map[i] should contain the list of joints that effect vertex i
TheNumbat's avatar
TheNumbat committed
90
91
92
    (void)verts;
}

TheNumbat's avatar
TheNumbat committed
93
94
void Skeleton::skin(const GL::Mesh &input, GL::Mesh &output,
                    const std::unordered_map<unsigned int, std::vector<Joint *>> &map) {
TheNumbat's avatar
TheNumbat committed
95
96
97
98
99
100

    // TODO(Animation): Task 3

    // Apply bone poses & weights to the vertices of the input (bind position) mesh
    // and store the result in the output mesh. See the task description for details.
    // map was computed by find_joints, hence gives a mapping from vertex index to
TheNumbat's avatar
TheNumbat committed
101
102
    // the list of bones the vertex should be effected by.

TheNumbat's avatar
TheNumbat committed
103
104
    // Currently, this just copies the input to the output without modification.

TheNumbat's avatar
TheNumbat committed
105
106
107
    std::vector<GL::Mesh::Vert> verts = input.verts();
    for (size_t i = 0; i < verts.size(); i++) {

TheNumbat's avatar
TheNumbat committed
108
109
110
        // Skin vertex i.
    }

TheNumbat's avatar
TheNumbat committed
111
112
    std::vector<GL::Mesh::Index> idxs = input.indices();
    output.recreate(std::move(verts), std::move(idxs));
TheNumbat's avatar
TheNumbat committed
113
}