AnimatedMesh.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.InputStream;
  4. import java.io.IOException;
  5. import java.nio.ByteBuffer;
  6. import java.nio.ByteOrder;
  7. import java.nio.FloatBuffer;
  8. import java.nio.IntBuffer;
  9. import java.nio.ShortBuffer;
  10. import java.util.ArrayList;
  11. import android.opengl.GLES20;
  12. import android.opengl.Matrix;
  13. import android.util.Log;
  14. public class AnimatedMesh {
  15. private ByteBuffer data;
  16. private int vertexOffset;
  17. private int vertexSize;
  18. private int triangleOffset;
  19. private int triangleCount;
  20. private int vertexBuffer; // vbo
  21. private int indexBuffer;
  22. private ArrayList<Bone> bones;
  23. private float[] matrices; // matrix palette for skinning
  24. private ArrayList<Action> actions;
  25. private Action currentAction;
  26. private int currentFrame;
  27. private class Bone {
  28. public String name; // 15 bytes
  29. public int parentIndex; // 1 byte
  30. public float[] invBindPose; // 64 bytes
  31. Bone(ByteBuffer data) {
  32. name = "";
  33. boolean stop = false;
  34. for (int i = 0; i < 15; i++) {
  35. char c;
  36. if ((c = (char)data.get()) == '\0') stop = true;
  37. if (!stop) name += c;
  38. }
  39. Log.i("bone", name);
  40. parentIndex = (int)data.get();
  41. invBindPose = new float[16];
  42. data.asFloatBuffer().get(invBindPose);
  43. data.position(data.position() + 64);
  44. }
  45. }
  46. private class Action {
  47. public String name; // 16 bytes
  48. public int numFrames;
  49. public ArrayList<Track> tracks;
  50. Action(ByteBuffer data) {
  51. name = "";
  52. boolean stop = false;
  53. for (int i = 0; i < 16; i++) {
  54. char c;
  55. if ((c = (char)data.get()) == '\0') stop = true;
  56. if (!stop) name += c;
  57. }
  58. Log.i("action", name);
  59. numFrames = data.getInt();
  60. int trackOffset = data.getInt();
  61. int trackCount = data.getInt();
  62. tracks = new ArrayList<Track>();
  63. for (int i = 0; i < trackCount; i++) {
  64. data.position(trackOffset + i * 12); // track header size == 12
  65. tracks.add(new Track(data));
  66. }
  67. }
  68. }
  69. private class Track {
  70. public int boneIndex;
  71. public ArrayList<JointPose> poses;
  72. Track(ByteBuffer data) {
  73. boneIndex = data.getInt();
  74. int jointPoseOffset = data.getInt();
  75. int jointPoseCount = data.getInt();
  76. poses = new ArrayList<JointPose>();
  77. data.position(jointPoseOffset);
  78. for (int i = 0; i < jointPoseCount; i++) {
  79. //data.position(jointPoseOffset + i * 32); // joint pose size == 32
  80. poses.add(new JointPose(data));
  81. }
  82. }
  83. }
  84. private class JointPose {
  85. public Quaternion rotation;
  86. public float[] translation;
  87. float scale;
  88. JointPose() { // empty pose == identity
  89. rotation = new Quaternion();
  90. translation = new float[3];
  91. translation[0] = translation[1] = translation[2] = 0.0f;
  92. scale = 1.0f;
  93. }
  94. JointPose(ByteBuffer data) {
  95. FloatBuffer floatData = data.asFloatBuffer();
  96. data.position(data.position() + 32);
  97. // quat data is x y z w, because of glm
  98. float x = floatData.get();
  99. float y = floatData.get();
  100. float z = floatData.get();
  101. float w = floatData.get();
  102. rotation = new Quaternion(w, x, y, z);
  103. translation = new float[3];
  104. floatData.get(translation);
  105. scale = floatData.get();
  106. }
  107. float[] toMatrix() { // TODO: scale
  108. float[] matrix = new float[16];
  109. Matrix.setIdentityM(matrix, 0);
  110. Quaternion q = rotation;
  111. matrix[0 * 4 + 0] = 1.0f - 2.0f * q.y * q.y - 2.0f * q.z * q.z;
  112. matrix[0 * 4 + 1] = 2.0f * q.x * q.y + 2.0f * q.w * q.z;
  113. matrix[0 * 4 + 2] = 2.0f * q.x * q.z - 2.0f * q.w * q.y;
  114. matrix[1 * 4 + 0] = 2.0f * q.x * q.y - 2.0f * q.w * q.z;
  115. matrix[1 * 4 + 1] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.z * q.z;
  116. matrix[1 * 4 + 2] = 2.0f * q.y * q.z + 2.0f * q.w * q.x;
  117. matrix[2 * 4 + 0] = 2.0f * q.x * q.z + 2 * q.w * q.y;
  118. matrix[2 * 4 + 1] = 2.0f * q.y * q.z - 2 * q.w * q.x;
  119. matrix[2 * 4 + 2] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.y * q.y;
  120. matrix[3 * 4 + 0] = translation[0];
  121. matrix[3 * 4 + 1] = translation[1];
  122. matrix[3 * 4 + 2] = translation[2];
  123. return matrix;
  124. }
  125. }
  126. public AnimatedMesh(InputStream is) {
  127. ByteArrayOutputStream out = new ByteArrayOutputStream();
  128. try {
  129. final int EOF = -1;
  130. int len;
  131. byte[] buffer = new byte[1 << 12];
  132. while (EOF != (len = is.read(buffer)))
  133. out.write(buffer, 0, len);
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. return;
  137. }
  138. //data = ByteBuffer.wrap(out.toByteArray()); // doesn't work data needs to be direct
  139. data = ByteBuffer.allocateDirect(out.size());
  140. data.order(ByteOrder.nativeOrder());
  141. data.put(out.toByteArray());
  142. data.position(0);
  143. // header
  144. int magicNum = data.getInt();
  145. int version = data.getInt();
  146. assert(magicNum == ('A' << 24 | 'M' << 16 | 'S' << 8 | 'H') && version == 1);
  147. vertexSize = 48;
  148. vertexOffset = data.getInt();
  149. int vertexCount = data.getInt();
  150. triangleOffset = data.getInt();
  151. triangleCount = data.getInt();
  152. int boneOffset = data.getInt();
  153. int boneCount = data.getInt();
  154. int actionOffset = data.getInt();
  155. int actionCount = data.getInt();
  156. // vertices and indices data
  157. IntBuffer buffers = IntBuffer.allocate(2);
  158. GLES20.glGenBuffers(2, buffers); // buffer names
  159. vertexBuffer = buffers.get();
  160. indexBuffer = buffers.get();
  161. data.position(vertexOffset);
  162. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
  163. GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexSize * vertexCount, data.asFloatBuffer(), GLES20.GL_STATIC_DRAW);
  164. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  165. // convert int indices to short
  166. // TODO: more efficient way?
  167. data.position(triangleOffset);
  168. ShortBuffer indexBufferData = ShortBuffer.allocate(3 * triangleCount);
  169. for (int i = 0; i < 3 * triangleCount; i++)
  170. indexBufferData.put((short)data.getInt());
  171. indexBufferData.position(0);
  172. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  173. GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 6 * triangleCount, indexBufferData, GLES20.GL_STATIC_DRAW);
  174. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  175. // bones
  176. bones = new ArrayList<Bone>();
  177. data.position(boneOffset);
  178. for (int i = 0; i < boneCount; i++)
  179. bones.add(new Bone(data));
  180. matrices = new float[16 * boneCount];
  181. // actions
  182. actions = new ArrayList<Action>();
  183. for (int i = 0; i < actionCount; i++) {
  184. data.position(actionOffset + i * 28); // action header size == 28
  185. actions.add(new Action(data));
  186. }
  187. currentAction = actions.get(0);
  188. currentFrame = 0;
  189. }
  190. public static float[] addVec3(final float[] v1, final float[] v2) {
  191. float[] v3 = new float[3];
  192. v3[0] = v1[0] + v2[0];
  193. v3[1] = v1[1] + v2[1];
  194. v3[2] = v1[2] + v2[2];
  195. return v3;
  196. }
  197. public void tick() {
  198. currentFrame++;
  199. if (currentFrame >= currentAction.numFrames)
  200. currentFrame = 0;
  201. // empty pose
  202. ArrayList<JointPose> pose = new ArrayList<JointPose>();
  203. for (int i = 0; i < bones.size(); i++)
  204. pose.add(new JointPose());
  205. // fill pose with action
  206. for (int i = 0; i < currentAction.tracks.size(); i++) {
  207. // TODO: do lerp or something nice
  208. pose.get(i).rotation = currentAction.tracks.get(i).poses.get(currentFrame).rotation;
  209. pose.get(i).translation = currentAction.tracks.get(i).poses.get(currentFrame).translation;
  210. }
  211. // convert pose to skinning matrices
  212. for (int i = 0; i < bones.size(); i++) {
  213. int parentIndex = bones.get(i).parentIndex;
  214. if (parentIndex != -1) { // bone has parent
  215. JointPose parentPose = pose.get(parentIndex);
  216. pose.get(i).rotation = parentPose.rotation.multiply(pose.get(i).rotation);
  217. pose.get(i).translation = addVec3(parentPose.translation, parentPose.rotation.multiply(pose.get(i).translation));
  218. }
  219. Matrix.multiplyMM(matrices, i * 16, pose.get(i).toMatrix(), 0, bones.get(i).invBindPose, 0);
  220. }
  221. }
  222. public void draw(int program) {
  223. GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(program, "boneMatrices"), bones.size(), false, matrices, 0);
  224. // TODO: cache attrib locations
  225. int positionIndex = GLES20.glGetAttribLocation(program, "position");
  226. int normalIndex = GLES20.glGetAttribLocation(program, "normal");
  227. int texCoordIndex = GLES20.glGetAttribLocation(program, "texCoord");
  228. int boneIndicesIndex = GLES20.glGetAttribLocation(program, "boneIndices");
  229. int boneWeightsIndex = GLES20.glGetAttribLocation(program, "boneWeights");
  230. GLES20.glEnableVertexAttribArray(positionIndex);
  231. GLES20.glEnableVertexAttribArray(normalIndex);
  232. GLES20.glEnableVertexAttribArray(texCoordIndex);
  233. GLES20.glEnableVertexAttribArray(boneIndicesIndex);
  234. GLES20.glEnableVertexAttribArray(boneWeightsIndex);
  235. //data.position(vertexOffset);
  236. //GLES20.glVertexAttribPointer(positionIndex, 3, GLES20.GL_FLOAT, false, vertexSize, data.asFloatBuffer());
  237. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
  238. GLES20.glVertexAttribPointer(positionIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 0);
  239. GLES20.glVertexAttribPointer(normalIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 12);
  240. GLES20.glVertexAttribPointer(texCoordIndex, 2, GLES20.GL_FLOAT, false, vertexSize, 24);
  241. GLES20.glVertexAttribPointer(boneIndicesIndex, 4, GLES20.GL_UNSIGNED_BYTE, false, vertexSize, 32);
  242. GLES20.glVertexAttribPointer(boneWeightsIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 36);
  243. //data.position(triangleOffset);
  244. //GLES20.glDrawElements(GLES20.GL_TRIANGLES, 3 * triangleCount, GLES20.GL_UNSIGNED_INT, data.asIntBuffer());
  245. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  246. GLES20.glDrawElements(GLES20.GL_TRIANGLES, 3 * triangleCount, GLES20.GL_UNSIGNED_SHORT, 0);
  247. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
  248. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  249. GLES20.glDisableVertexAttribArray(positionIndex);
  250. GLES20.glDisableVertexAttribArray(normalIndex);
  251. GLES20.glDisableVertexAttribArray(texCoordIndex);
  252. GLES20.glDisableVertexAttribArray(boneIndicesIndex);
  253. GLES20.glDisableVertexAttribArray(boneWeightsIndex);
  254. }
  255. }