AnimatedMesh.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 true;
  55. return currentFrame <= 1 || currentFrame >= currentAction.numFrames - 1;
  56. /*
  57. if (reverseAction)
  58. return currentFrame <= 0;
  59. else
  60. return currentFrame >= currentAction.numFrames - 1;
  61. */
  62. }
  63. private class Bone {
  64. @SuppressWarnings("unused")
  65. public String name; // 15 bytes
  66. public int parentIndex; // 1 byte
  67. public float[] invBindPose; // 64 bytes
  68. Bone(ByteBuffer data) {
  69. name = "";
  70. boolean stop = false;
  71. for (int i = 0; i < 15; i++) {
  72. char c;
  73. if ((c = (char)data.get()) == '\0') stop = true;
  74. if (!stop) name += c;
  75. }
  76. // Log.i("bone", name);
  77. parentIndex = (int)data.get();
  78. invBindPose = new float[16];
  79. data.asFloatBuffer().get(invBindPose);
  80. data.position(data.position() + 64);
  81. }
  82. }
  83. private class Action {
  84. public String name; // 16 bytes
  85. public int numFrames;
  86. public ArrayList<Track> tracks;
  87. public static final int kHeaderSize = 28;
  88. Action(ByteBuffer data) {
  89. name = "";
  90. boolean stop = false;
  91. for (int i = 0; i < 16; i++) {
  92. char c;
  93. if ((c = (char)data.get()) == '\0') stop = true;
  94. if (!stop) name += c;
  95. }
  96. Log.i("action", name);
  97. numFrames = data.getInt();
  98. int trackOffset = data.getInt();
  99. int trackCount = data.getInt();
  100. tracks = new ArrayList<Track>();
  101. for (int i = 0; i < trackCount; i++) {
  102. data.position(trackOffset + i * Track.kHeaderSize);
  103. tracks.add(new Track(data));
  104. }
  105. }
  106. }
  107. private class Track {
  108. @SuppressWarnings("unused")
  109. public int boneIndex;
  110. public ArrayList<JointPose> poses;
  111. public static final int kHeaderSize = 12;
  112. Track(ByteBuffer data) {
  113. boneIndex = data.getInt();
  114. int jointPoseOffset = data.getInt();
  115. int jointPoseCount = data.getInt();
  116. poses = new ArrayList<JointPose>();
  117. data.position(jointPoseOffset);
  118. for (int i = 0; i < jointPoseCount; i++) {
  119. //data.position(jointPoseOffset + i * JointPose::kSize); // joint pose size == 32
  120. poses.add(new JointPose(data));
  121. }
  122. }
  123. }
  124. private class JointPose {
  125. public Quaternion rotation;
  126. public float[] translation;
  127. @SuppressWarnings("unused")
  128. float scale;
  129. public static final int kSize = 32;
  130. JointPose() { // empty pose == identity
  131. rotation = new Quaternion();
  132. translation = new float[3];
  133. translation[0] = translation[1] = translation[2] = 0.0f;
  134. scale = 1.0f;
  135. }
  136. JointPose(ByteBuffer data) {
  137. FloatBuffer floatData = data.asFloatBuffer();
  138. data.position(data.position() + kSize);
  139. // quat data is x y z w, because of glm
  140. float x = floatData.get();
  141. float y = floatData.get();
  142. float z = floatData.get();
  143. float w = floatData.get();
  144. rotation = new Quaternion(w, x, y, z);
  145. translation = new float[3];
  146. floatData.get(translation);
  147. scale = floatData.get();
  148. }
  149. float[] toMatrix() { // TODO: scale
  150. float[] matrix = new float[16];
  151. Matrix.setIdentityM(matrix, 0);
  152. Quaternion q = rotation;
  153. matrix[0 * 4 + 0] = 1.0f - 2.0f * q.y * q.y - 2.0f * q.z * q.z;
  154. matrix[0 * 4 + 1] = 2.0f * q.x * q.y + 2.0f * q.w * q.z;
  155. matrix[0 * 4 + 2] = 2.0f * q.x * q.z - 2.0f * q.w * q.y;
  156. matrix[1 * 4 + 0] = 2.0f * q.x * q.y - 2.0f * q.w * q.z;
  157. matrix[1 * 4 + 1] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.z * q.z;
  158. matrix[1 * 4 + 2] = 2.0f * q.y * q.z + 2.0f * q.w * q.x;
  159. matrix[2 * 4 + 0] = 2.0f * q.x * q.z + 2 * q.w * q.y;
  160. matrix[2 * 4 + 1] = 2.0f * q.y * q.z - 2 * q.w * q.x;
  161. matrix[2 * 4 + 2] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.y * q.y;
  162. matrix[3 * 4 + 0] = translation[0];
  163. matrix[3 * 4 + 1] = translation[1];
  164. matrix[3 * 4 + 2] = translation[2];
  165. return matrix;
  166. }
  167. }
  168. public AnimatedMesh(InputStream is) {
  169. ByteArrayOutputStream out = new ByteArrayOutputStream();
  170. try {
  171. final int EOF = -1;
  172. int len;
  173. byte[] buffer = new byte[1 << 12];
  174. while (EOF != (len = is.read(buffer)))
  175. out.write(buffer, 0, len);
  176. } catch (IOException e) {
  177. e.printStackTrace();
  178. return;
  179. }
  180. //data = ByteBuffer.wrap(out.toByteArray()); // doesn't work data needs to be direct
  181. data = ByteBuffer.allocateDirect(out.size());
  182. data.order(ByteOrder.nativeOrder());
  183. data.put(out.toByteArray());
  184. data.position(0);
  185. // header
  186. int magicNum = data.getInt();
  187. int version = data.getInt();
  188. //assert(magicNum == ('A' << 24 | 'M' << 16 | 'S' << 8 | 'H') && version == 1);
  189. vertexSize = 48;
  190. vertexOffset = data.getInt();
  191. int vertexCount = data.getInt();
  192. triangleOffset = data.getInt();
  193. triangleCount = data.getInt();
  194. int boneOffset = data.getInt();
  195. int boneCount = data.getInt();
  196. int actionOffset = data.getInt();
  197. int actionCount = data.getInt();
  198. // vertices and indices data
  199. IntBuffer buffers = IntBuffer.allocate(2);
  200. GLES20.glGenBuffers(2, buffers); // buffer names
  201. vertexBuffer = buffers.get();
  202. indexBuffer = buffers.get();
  203. data.position(vertexOffset);
  204. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
  205. GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexSize * vertexCount, data.asFloatBuffer(), GLES20.GL_STATIC_DRAW);
  206. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  207. // convert int indices to short
  208. // TODO: more efficient way?
  209. data.position(triangleOffset);
  210. ShortBuffer indexBufferData = ShortBuffer.allocate(3 * triangleCount);
  211. for (int i = 0; i < 3 * triangleCount; i++)
  212. indexBufferData.put((short)data.getInt());
  213. indexBufferData.position(0);
  214. GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  215. GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 6 * triangleCount, indexBufferData, GLES20.GL_STATIC_DRAW);
  216. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  217. // bones
  218. bones = new ArrayList<Bone>();
  219. data.position(boneOffset);
  220. for (int i = 0; i < boneCount; i++)
  221. bones.add(new Bone(data));
  222. matrices = new float[16 * boneCount];
  223. // actions
  224. actions = new ArrayList<Action>();
  225. for (int i = 0; i < actionCount; i++) {
  226. data.position(actionOffset + i * Action.kHeaderSize); // action header size == 28
  227. actions.add(new Action(data));
  228. }
  229. currentAction = null;
  230. currentFrame = 0;
  231. loopAction = false;
  232. }
  233. public static float[] addVec3(final float[] v1, final float[] v2) {
  234. float[] v3 = new float[3];
  235. v3[0] = v1[0] + v2[0];
  236. v3[1] = v1[1] + v2[1];
  237. v3[2] = v1[2] + v2[2];
  238. return v3;
  239. }
  240. /**
  241. * animate the mesh for one frame
  242. */
  243. public void tick() {
  244. // empty pose
  245. ArrayList<JointPose> pose = new ArrayList<JointPose>();
  246. for (int i = 0; i < bones.size(); i++)
  247. pose.add(new JointPose());
  248. if (currentAction != null) {
  249. // fill pose with action
  250. for (int i = 0; i < currentAction.tracks.size(); i++) {
  251. // TODO: do lerp or something nice
  252. pose.get(i).rotation = currentAction.tracks.get(i).poses.get(currentFrame).rotation;
  253. pose.get(i).translation = currentAction.tracks.get(i).poses.get(currentFrame).translation;
  254. }
  255. // advance one frame
  256. if (reverseAction) {
  257. if (currentFrame > 0) {
  258. currentFrame--;
  259. } else if (loopAction) {
  260. currentFrame = currentAction.numFrames - 1;
  261. }
  262. } else {
  263. if (currentFrame < currentAction.numFrames - 1) {
  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. }