AnimatedModel.java 4.8 KB

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