AnimatedMesh.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /**
  15. * @author Fabio Arnold
  16. *
  17. * Animated Mesh
  18. * This class reads a mesh in the AMSH binary format and creates the necessary OpenGL objects for drawing
  19. */
  20. public class AnimatedMesh {
  21. private ByteBuffer data;
  22. private int vertexOffset;
  23. private int vertexSize;
  24. private int triangleOffset;
  25. private int triangleCount;
  26. private int vertexBuffer; // vbo
  27. private int indexBuffer;
  28. private ArrayList<Bone> bones;
  29. private float[] matrices; // matrix palette for skinning
  30. private ArrayList<Action> actions;
  31. private Action currentAction;
  32. private int currentFrame;
  33. private boolean loopAction = false;
  34. private boolean reverseAction = false;
  35. public void startAction(String actionName, boolean loop, boolean reverse) {
  36. if (!(currentAction != null && currentAction.name.equals(actionName) && reverse)) // keep the current frame
  37. currentFrame = 0;
  38. loopAction = loop;
  39. reverseAction = reverse;
  40. currentAction = null;
  41. // find the action
  42. for (Action action : actions) {
  43. if (action.name.equals(actionName)) {
  44. currentAction = action;
  45. break;
  46. }
  47. }
  48. if (currentAction != null && reverseAction)
  49. if (!(currentAction != null && currentAction.name.equals(actionName) && reverse)) // keep the current frame
  50. currentFrame = currentAction.numFrames - 1;
  51. }
  52. public boolean isActionDone() {
  53. if (currentAction == null)
  54. return false;
  55. if (reverseAction)
  56. return currentFrame <= 0;
  57. else
  58. return currentFrame >= currentAction.numFrames - 1;
  59. }
  60. private class Bone {
  61. @SuppressWarnings("unused")
  62. public String name; // 15 bytes
  63. public int parentIndex; // 1 byte
  64. public float[] invBindPose; // 64 bytes
  65. Bone(ByteBuffer data) {
  66. name = "";
  67. boolean stop = false;
  68. for (int i = 0; i < 15; i++) {
  69. char c;
  70. if ((c = (char)data.get()) == '\0') stop = true;
  71. if (!stop) name += c;
  72. }
  73. // Log.i("bone", name);
  74. parentIndex = (int)data.get();
  75. invBindPose = new float[16];
  76. data.asFloatBuffer().get(invBindPose);
  77. data.position(data.position() + 64);
  78. }
  79. }
  80. private class Action {
  81. public String name; // 16 bytes
  82. public int numFrames;
  83. public ArrayList<Track> tracks;
  84. public static final int kHeaderSize = 28;
  85. Action(ByteBuffer data) {
  86. name = "";
  87. boolean stop = false;
  88. for (int i = 0; i < 16; i++) {
  89. char c;
  90. if ((c = (char)data.get()) == '\0') stop = true;
  91. if (!stop) name += c;
  92. }
  93. Log.i("action", name);
  94. numFrames = data.getInt();
  95. int trackOffset = data.getInt();
  96. int trackCount = data.getInt();
  97. tracks = new ArrayList<Track>();
  98. for (int i = 0; i < trackCount; i++) {
  99. data.position(trackOffset + i * Track.kHeaderSize);
  100. tracks.add(new Track(data));
  101. }
  102. }
  103. }
  104. private class Track {
  105. @SuppressWarnings("unused")
  106. public int boneIndex;
  107. public ArrayList<JointPose> poses;
  108. public static final int kHeaderSize = 12;
  109. Track(ByteBuffer data) {
  110. boneIndex = data.getInt();
  111. int jointPoseOffset = data.getInt();
  112. int jointPoseCount = data.getInt();
  113. poses = new ArrayList<JointPose>();
  114. data.position(jointPoseOffset);
  115. for (int i = 0; i < jointPoseCount; i++) {
  116. //data.position(jointPoseOffset + i * JointPose::kSize); // joint pose size == 32
  117. poses.add(new JointPose(data));
  118. }
  119. }
  120. }
  121. private class JointPose {
  122. public Quaternion rotation;
  123. public float[] translation;
  124. @SuppressWarnings("unused")
  125. float scale;
  126. public static final int kSize = 32;
  127. JointPose() { // empty pose == identity
  128. rotation = new Quaternion();
  129. translation = new float[3];
  130. translation[0] = translation[1] = translation[2] = 0.0f;
  131. scale = 1.0f;
  132. }
  133. JointPose(ByteBuffer data) {
  134. FloatBuffer floatData = data.asFloatBuffer();
  135. data.position(data.position() + kSize);
  136. // quat data is x y z w, because of glm
  137. float x = floatData.get();
  138. float y = floatData.get();
  139. float z = floatData.get();
  140. float w = floatData.get();
  141. rotation = new Quaternion(w, x, y, z);
  142. translation = new float[3];
  143. floatData.get(translation);
  144. scale = floatData.get();
  145. }
  146. float[] toMatrix() { // TODO: scale
  147. float[] matrix = new float[16];
  148. Matrix.setIdentityM(matrix, 0);
  149. Quaternion q = rotation;
  150. matrix[0 * 4 + 0] = 1.0f - 2.0f * q.y * q.y - 2.0f * q.z * q.z;
  151. matrix[0 * 4 + 1] = 2.0f * q.x * q.y + 2.0f * q.w * q.z;
  152. matrix[0 * 4 + 2] = 2.0f * q.x * q.z - 2.0f * q.w * q.y;
  153. matrix[1 * 4 + 0] = 2.0f * q.x * q.y - 2.0f * q.w * q.z;
  154. matrix[1 * 4 + 1] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.z * q.z;
  155. matrix[1 * 4 + 2] = 2.0f * q.y * q.z + 2.0f * q.w * q.x;
  156. matrix[2 * 4 + 0] = 2.0f * q.x * q.z + 2 * q.w * q.y;
  157. matrix[2 * 4 + 1] = 2.0f * q.y * q.z - 2 * q.w * q.x;
  158. matrix[2 * 4 + 2] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.y * q.y;
  159. matrix[3 * 4 + 0] = translation[0];
  160. matrix[3 * 4 + 1] = translation[1];
  161. matrix[3 * 4 + 2] = translation[2];
  162. return matrix;
  163. }
  164. }
  165. public AnimatedMesh(InputStream is) {
  166. ByteArrayOutputStream out = new ByteArrayOutputStream();
  167. try {
  168. final int EOF = -1;
  169. int len;
  170. byte[] buffer = new byte[1 << 12];
  171. while (EOF != (len = is.read(buffer)))
  172. out.write(buffer, 0, len);
  173. } catch (IOException e) {
  174. e.printStackTrace();
  175. return;
  176. }
  177. //data = ByteBuffer.wrap(out.toByteArray()); // doesn't work data needs to be direct
  178. data = ByteBuffer.allocateDirect(out.size());
  179. data.order(ByteOrder.nativeOrder());
  180. data.put(out.toByteArray());
  181. data.position(0);
  182. // header
  183. int magicNum = data.getInt();
  184. int version = data.getInt();
  185. assert(magicNum == ('A' << 24 | 'M' << 16 | 'S' << 8 | 'H') && version == 1);
  186. vertexSize = 48;
  187. vertexOffset = data.getInt();
  188. int vertexCount = data.getInt();
  189. triangleOffset = data.getInt();
  190. triangleCount = data.getInt();
  191. int boneOffset = data.getInt();
  192. int boneCount = data.getInt();
  193. int actionOffset = data.getInt();
  194. int actionCount = data.getInt();
  195. // vertices and indices data
  196. IntBuffer buffers = IntBuffer.allocate(2);
  197. GLES20.glGenBuffers(2, buffers); // buffer names
  198. vertexBuffer = buffers.get();
  199. indexBuffer = buffers.get();
  200. data.position(vertexOffset);
  201. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
  202. GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexSize * vertexCount, data.asFloatBuffer(), GLES20.GL_STATIC_DRAW);
  203. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  204. // convert int indices to short
  205. // TODO: more efficient way?
  206. data.position(triangleOffset);
  207. ShortBuffer indexBufferData = ShortBuffer.allocate(3 * triangleCount);
  208. for (int i = 0; i < 3 * triangleCount; i++)
  209. indexBufferData.put((short)data.getInt());
  210. indexBufferData.position(0);
  211. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  212. GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 6 * triangleCount, indexBufferData, GLES20.GL_STATIC_DRAW);
  213. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  214. // bones
  215. bones = new ArrayList<Bone>();
  216. data.position(boneOffset);
  217. for (int i = 0; i < boneCount; i++)
  218. bones.add(new Bone(data));
  219. matrices = new float[16 * boneCount];
  220. // actions
  221. actions = new ArrayList<Action>();
  222. for (int i = 0; i < actionCount; i++) {
  223. data.position(actionOffset + i * Action.kHeaderSize); // action header size == 28
  224. actions.add(new Action(data));
  225. }
  226. currentAction = null;
  227. currentFrame = 0;
  228. loopAction = false;
  229. }
  230. public static float[] addVec3(final float[] v1, final float[] v2) {
  231. float[] v3 = new float[3];
  232. v3[0] = v1[0] + v2[0];
  233. v3[1] = v1[1] + v2[1];
  234. v3[2] = v1[2] + v2[2];
  235. return v3;
  236. }
  237. private boolean halfFramerate = false;
  238. /**
  239. * animate the mesh for one frame
  240. */
  241. public void tick() {
  242. // empty pose
  243. ArrayList<JointPose> pose = new ArrayList<JointPose>();
  244. for (int i = 0; i < bones.size(); i++)
  245. pose.add(new JointPose());
  246. if (currentAction != null) {
  247. // fill pose with action
  248. for (int i = 0; i < currentAction.tracks.size(); i++) {
  249. // TODO: do lerp or something nice
  250. pose.get(i).rotation = currentAction.tracks.get(i).poses.get(currentFrame).rotation;
  251. pose.get(i).translation = currentAction.tracks.get(i).poses.get(currentFrame).translation;
  252. }
  253. // advance one frame
  254. if (reverseAction) {
  255. if (currentFrame > 0) {
  256. if (halfFramerate = !halfFramerate)
  257. currentFrame--;
  258. } else if (loopAction) {
  259. currentFrame = currentAction.numFrames - 1;
  260. }
  261. } else {
  262. if (currentFrame < currentAction.numFrames - 1) {
  263. if (halfFramerate = !halfFramerate)
  264. currentFrame++;
  265. } else if (loopAction) {
  266. currentFrame = 0;
  267. }
  268. }
  269. }
  270. // convert pose to skinning matrices
  271. for (int i = 0; i < bones.size(); i++) {
  272. int parentIndex = bones.get(i).parentIndex;
  273. if (parentIndex != -1) { // bone has parent
  274. JointPose parentPose = pose.get(parentIndex);
  275. pose.get(i).rotation = parentPose.rotation.multiply(pose.get(i).rotation);
  276. pose.get(i).translation = addVec3(parentPose.translation, parentPose.rotation.multiply(pose.get(i).translation));
  277. }
  278. Matrix.multiplyMM(matrices, i * 16, pose.get(i).toMatrix(), 0, bones.get(i).invBindPose, 0);
  279. }
  280. }
  281. /**
  282. * draws the mesh
  283. * @param program the currently bound shader program
  284. */
  285. public void draw(int program) {
  286. GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(program, "boneMatrices"), bones.size(), false, matrices, 0);
  287. // TODO: cache attrib locations
  288. int positionIndex = GLES20.glGetAttribLocation(program, "position");
  289. int normalIndex = GLES20.glGetAttribLocation(program, "normal");
  290. int texCoordIndex = GLES20.glGetAttribLocation(program, "texCoord");
  291. int boneIndicesIndex = GLES20.glGetAttribLocation(program, "boneIndices");
  292. int boneWeightsIndex = GLES20.glGetAttribLocation(program, "boneWeights");
  293. GLES20.glEnableVertexAttribArray(positionIndex);
  294. GLES20.glEnableVertexAttribArray(normalIndex);
  295. GLES20.glEnableVertexAttribArray(texCoordIndex);
  296. GLES20.glEnableVertexAttribArray(boneIndicesIndex);
  297. GLES20.glEnableVertexAttribArray(boneWeightsIndex);
  298. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
  299. GLES20.glVertexAttribPointer(positionIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 0);
  300. GLES20.glVertexAttribPointer(normalIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 12);
  301. GLES20.glVertexAttribPointer(texCoordIndex, 2, GLES20.GL_FLOAT, false, vertexSize, 24);
  302. GLES20.glVertexAttribPointer(boneIndicesIndex, 4, GLES20.GL_UNSIGNED_BYTE, false, vertexSize, 32);
  303. GLES20.glVertexAttribPointer(boneWeightsIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 36);
  304. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  305. GLES20.glDrawElements(GLES20.GL_TRIANGLES, 3 * triangleCount, GLES20.GL_UNSIGNED_SHORT, 0);
  306. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
  307. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  308. GLES20.glDisableVertexAttribArray(positionIndex);
  309. GLES20.glDisableVertexAttribArray(normalIndex);
  310. GLES20.glDisableVertexAttribArray(texCoordIndex);
  311. GLES20.glDisableVertexAttribArray(boneIndicesIndex);
  312. GLES20.glDisableVertexAttribArray(boneWeightsIndex);
  313. }
  314. }