-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAnimatedModel.java
More file actions
152 lines (136 loc) · 4.74 KB
/
AnimatedModel.java
File metadata and controls
152 lines (136 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package animatedModel;
import org.lwjgl.util.vector.Matrix4f;
import animation.Animation;
import animation.Animator;
import openglObjects.Vao;
import textures.Texture;
/**
*
* This class represents an entity in the world that can be animated. It
* contains the model's VAO which contains the mesh data, the texture, and the
* root joint of the joint hierarchy, or "skeleton". It also holds an int which
* represents the number of joints that the model's skeleton contains, and has
* its own {@link Animator} instance which can be used to apply animations to
* this entity.
*
* @author Karl
*
*/
public class AnimatedModel {
// skin
private final Vao model;
private final Texture texture;
// skeleton
private final Joint rootJoint;
private final int jointCount;
private final Animator animator;
/**
* Creates a new entity capable of animation. The inverse bind transform for
* all joints is calculated in this constructor. The bind transform is
* simply the original (no pose applied) transform of a joint in relation to
* the model's origin (model-space). The inverse bind transform is simply
* that but inverted.
*
* @param model
* - the VAO containing the mesh data for this entity. This
* includes vertex positions, normals, texture coords, IDs of
* joints that affect each vertex, and their corresponding
* weights.
* @param texture
* - the diffuse texture for the entity.
* @param rootJoint
* - the root joint of the joint hierarchy which makes up the
* "skeleton" of the entity.
* @param jointCount
* - the number of joints in the joint hierarchy (skeleton) for
* this entity.
*
*/
public AnimatedModel(Vao model, Texture texture, Joint rootJoint, int jointCount) {
this.model = model;
this.texture = texture;
this.rootJoint = rootJoint;
this.jointCount = jointCount;
this.animator = new Animator(this);
rootJoint.calcInverseBindTransform(new Matrix4f());
}
/**
* @return The VAO containing all the mesh data for this entity.
*/
public Vao getModel() {
return model;
}
/**
* @return The diffuse texture for this entity.
*/
public Texture getTexture() {
return texture;
}
/**
* @return The root joint of the joint hierarchy. This joint has no parent,
* and every other joint in the skeleton is a descendant of this
* joint.
*/
public Joint getRootJoint() {
return rootJoint;
}
/**
* Deletes the OpenGL objects associated with this entity, namely the model
* (VAO) and texture.
*/
public void delete() {
model.delete();
texture.delete();
}
/**
* Instructs this entity to carry out a given animation. To do this it
* basically sets the chosen animation as the current animation in the
* {@link Animator} object.
*
* @param animation
* - the animation to be carried out.
*/
public void doAnimation(Animation animation) {
animator.doAnimation(animation);
}
/**
* Updates the animator for this entity, basically updating the animated
* pose of the entity. Must be called every frame.
*/
public void update() {
animator.update();
}
/**
* Gets an array of the all important model-space transforms of all the
* joints (with the current animation pose applied) in the entity. The
* joints are ordered in the array based on their joint index. The position
* of each joint's transform in the array is equal to the joint's index.
*
* @return The array of model-space transforms of the joints in the current
* animation pose.
*/
public Matrix4f[] getJointTransforms() {
Matrix4f[] jointMatrices = new Matrix4f[jointCount+1];
System.out.println("AnimatedModel jointCount" +jointCount);
addJointsToArray(rootJoint, jointMatrices);
return jointMatrices;
}
/**
* This adds the current model-space transform of a joint (and all of its
* descendants) into an array of transforms. The joint's transform is added
* into the array at the position equal to the joint's index.
*
* @param headJoint
* - the current joint being added to the array. This method also
* adds the transforms of all the descendents of this joint too.
* @param jointMatrices
* - the array of joint transforms that is being filled.
*/
private void addJointsToArray(Joint headJoint, Matrix4f[] jointMatrices) {
System.out.println("AnimatedModel headJoint " +headJoint.children.size());
jointMatrices[headJoint.index] = headJoint.getAnimatedTransform();
for (Joint childJoint : headJoint.children) {
addJointsToArray(childJoint, jointMatrices);
}
}
}