Browse Source

File format for specifying animation keyframe ranges

Tankernn 8 years ago
parent
commit
691b6614d0

+ 5 - 1
formats.md

@@ -13,4 +13,8 @@ Available options:
 - shinedamper (float)
 - reflectivity (float)
 - refractivity (float)
-- transparency (boolean)
+- transparency (boolean)
+
+## *.anim
+
+name(string, no spaces) length(int)

+ 2 - 2
src/main/java/eu/tankernn/gameEngine/animation/animatedModel/AnimatedModel.java

@@ -101,8 +101,8 @@ public class AnimatedModel extends TexturedModel {
 		doAnimation(animations.get(animationId));
 	}
 	
-	public void registerAnimation(String key, Animation animation) {
-		animations.put(key, animation);
+	public void registerAnimations(Map<String, Animation> animations) {
+		this.animations = new HashMap<>(animations);
 	}
 
 	/**

+ 9 - 2
src/main/java/eu/tankernn/gameEngine/animation/animation/Animator.java

@@ -39,6 +39,8 @@ public class Animator {
 	private float animationTime = 0;
 	private Animation currentAnimation;
 
+	private float speed;
+
 	/**
 	 * @param entity
 	 *            - the entity which will by animated by this animator.
@@ -54,12 +56,17 @@ public class Animator {
 	 * @param animation
 	 *            - the new animation to carry out.
 	 */
-	public void doAnimation(Animation animation) {
+	public void doAnimation(Animation animation, float speed) {
 		if (this.currentAnimation != null && this.currentAnimation.equals(animation))
 			return;
+		this.speed = speed;
 		this.animationTime = 0;
 		this.currentAnimation = animation;
 	}
+	
+	public void doAnimation(Animation animation) {
+		this.doAnimation(animation, 1);
+	}
 
 	/**
 	 * This method should be called each frame to update the animation currently
@@ -84,7 +91,7 @@ public class Animator {
 	 * reset, causing the animation to loop.
 	 */
 	private void increaseAnimationTime() {
-		animationTime += DisplayManager.getFrameTimeSeconds();
+		animationTime += DisplayManager.getFrameTimeSeconds() * speed;
 		if (animationTime > currentAnimation.getLength()) {
 			this.animationTime %= currentAnimation.getLength();
 		}

+ 32 - 21
src/main/java/eu/tankernn/gameEngine/animation/loaders/AnimationLoader.java

@@ -1,5 +1,7 @@
 package eu.tankernn.gameEngine.animation.loaders;
 
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -21,50 +23,59 @@ import eu.tankernn.gameEngine.util.InternalFile;
  * and then creates and returns an {@link Animation} from the extracted data.
  * 
  * @author Karl
- *
  */
 public class AnimationLoader {
-
+	
 	/**
 	 * Loads up a collada animation file, and returns and animation created from
 	 * the extracted animation data from the file.
 	 * 
-	 * @param colladaFile
-	 *            - the collada file containing data about the desired
-	 *            animation.
+	 * @param colladaFile - the collada file containing data about the desired
+	 *        animation.
 	 * @return The animation made from the data in the file.
+	 * @throws IOException 
 	 */
-	public static Animation loadAnimation(InternalFile colladaFile) {
+	public static Map<String, Animation> loadAnimations(InternalFile colladaFile, InternalFile animationFile) throws IOException {
+		Map<String, Animation> animations = new HashMap<>();
 		AnimationData animationData = ColladaLoader.loadColladaAnimation(colladaFile);
-		KeyFrame[] frames = new KeyFrame[animationData.keyFrames.length];
-		for (int i = 0; i < frames.length; i++) {
-			frames[i] = createKeyFrame(animationData.keyFrames[i]);
+		int dataIndex = 0;
+		BufferedReader r = animationFile.getReader();
+		String line;
+		while (r.ready()) {
+			line = r.readLine();
+			String[] split = line.split(" ");
+			KeyFrame[] frames = new KeyFrame[Integer.parseInt(split[1])];
+			float animationStart = animationData.keyFrames[dataIndex].time;
+			for (int i = 0; i < frames.length; i++) {
+				frames[i] = createKeyFrame(animationData.keyFrames[dataIndex++], animationStart);
+			}
+			float animationEnd = animationData.keyFrames[dataIndex -1].time;
+			animations.put(split[0], new Animation(animationEnd - animationStart, frames));
 		}
-		return new Animation(animationData.lengthSeconds, frames);
+		
+		return animations;
 	}
-
+	
 	/**
 	 * Creates a keyframe from the data extracted from the collada file.
 	 * 
-	 * @param data
-	 *            - the data about the keyframe that was extracted from the
-	 *            collada file.
+	 * @param data - the data about the keyframe that was extracted from the
+	 *        collada file.
 	 * @return The keyframe.
 	 */
-	private static KeyFrame createKeyFrame(KeyFrameData data) {
+	private static KeyFrame createKeyFrame(KeyFrameData data, float animationStart) {
 		Map<String, JointTransform> map = new HashMap<String, JointTransform>();
-		for (JointTransformData jointData : data.jointTransforms) {
+		for (JointTransformData jointData: data.jointTransforms) {
 			JointTransform jointTransform = createTransform(jointData);
 			map.put(jointData.jointNameId, jointTransform);
 		}
-		return new KeyFrame(data.time, map);
+		return new KeyFrame(data.time - animationStart, map);
 	}
-
+	
 	/**
 	 * Creates a joint transform from the data extracted from the collada file.
 	 * 
-	 * @param data
-	 *            - the data from the collada file.
+	 * @param data - the data from the collada file.
 	 * @return The joint transform.
 	 */
 	private static JointTransform createTransform(JointTransformData data) {
@@ -73,5 +84,5 @@ public class AnimationLoader {
 		Quaternion rotation = new Quaternion(mat);
 		return new JointTransform(translation, rotation);
 	}
-
+	
 }

+ 6 - 7
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

@@ -59,6 +59,7 @@ public class Loader {
 		vao.storeDataInAttributeList(0, 2, positions);
 		vao.storeDataInAttributeList(1, 2, textureCoords);
 		vao.unbind();
+		vaos.add(vao);
 		return vao;
 	}
 	
@@ -67,11 +68,12 @@ public class Loader {
 		vao.bind();
 		vao.storeDataInAttributeList(0, dimensions, positions);
 		vao.unbind();
+		vaos.add(vao);
 		return vao;
 	}
 	
 	public Vao loadToVAO(ModelData data) {
-		return (Vao) loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(), data.getIndices());
+		return loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(), data.getIndices());
 	}
 	
 	/**
@@ -201,12 +203,9 @@ public class Loader {
 					break;
 				case "dae":
 					AnimatedModel animatedModel = AnimatedModelLoader.loadEntity(modelFile, modelTexture);
-					JSONObject animations = spec.getJSONObject("animations");
-					for (Object key: animations.names().toList()) {
-						String name = (String) key;
-						//TODO Create a file format to specify animation frame ranges, then glue all animations together in Blender before exporting
-						animatedModel.registerAnimation(name, AnimationLoader.loadAnimation(modelFile));
-					}
+					String animations = spec.getString("animations");
+					animatedModel.registerAnimations(AnimationLoader.loadAnimations(modelFile, new InternalFile("models/" + animations)));
+					
 					models.put(id, animatedModel);
 					break;
 				default:

+ 3 - 2
src/main/java/eu/tankernn/gameEngine/renderEngine/Vao.java

@@ -22,7 +22,7 @@ public class Vao {
 		int id = GL30.glGenVertexArrays();
 		return new Vao(id);
 	}
-
+	
 	public static Vao create(int indexCount) {
 		int id = GL30.glGenVertexArrays();
 		return new Vao(id, indexCount);
@@ -103,7 +103,8 @@ public class Vao {
 		for (Vbo vbo: dataVbos) {
 			vbo.delete();
 		}
-		indexVbo.delete();
+		if (indexVbo != null)
+			indexVbo.delete();
 	}
 	
 	private void bind() {