Ver Fonte

Merge branch 'animation' of
https://github.com/Tankernn/TankernnGameEngine into animation

Conflicts:
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

Tankernn há 8 anos atrás
pai
commit
6cb5158bc7

+ 15 - 0
src/main/java/eu/tankernn/gameEngine/animation/animatedModel/Joint.java

@@ -6,6 +6,7 @@ import java.util.List;
 import org.lwjgl.util.vector.Matrix4f;
 
 import eu.tankernn.gameEngine.animation.animation.Animator;
+import eu.tankernn.gameEngine.loader.colladaLoader.JointData;
 
 /**
  * 
@@ -65,6 +66,20 @@ public class Joint {
 		for (Joint j : joint.children)
 			this.children.add(new Joint(j));
 	}
+	
+	/**
+	 * Constructs the joint-hierarchy skeleton from the data extracted from the
+	 * collada file.
+	 * 
+	 * @param data
+	 *            - the joints data from the collada file for the head joint.
+	 * @return The created joint, with all its descendants added.
+	 */
+	public Joint(JointData data) {
+		this(data.index, data.nameId, data.bindLocalTransform);
+		for (JointData child : data.children)
+			this.addChild(new Joint(child));
+	}
 
 	/**
 	 * Adds a child joint to this joint. Used during the creation of the joint

+ 0 - 72
src/main/java/eu/tankernn/gameEngine/animation/loaders/AnimatedModelLoader.java

@@ -1,72 +0,0 @@
-package eu.tankernn.gameEngine.animation.loaders;
-
-import eu.tankernn.gameEngine.animation.animatedModel.AnimatedModel;
-import eu.tankernn.gameEngine.animation.animatedModel.Joint;
-import eu.tankernn.gameEngine.loader.colladaLoader.AnimatedModelData;
-import eu.tankernn.gameEngine.loader.colladaLoader.ColladaLoader;
-import eu.tankernn.gameEngine.loader.colladaLoader.JointData;
-import eu.tankernn.gameEngine.loader.colladaLoader.JointsData;
-import eu.tankernn.gameEngine.loader.colladaLoader.MeshData;
-import eu.tankernn.gameEngine.loader.textures.ModelTexture;
-import eu.tankernn.gameEngine.renderEngine.Vao;
-import eu.tankernn.gameEngine.util.InternalFile;
-
-public class AnimatedModelLoader {
-	
-	public static final int MAX_WEIGHTS = 3;
-
-	/**
-	 * Creates an AnimatedEntity from the data in an entity file. It loads up
-	 * the collada model data, stores the extracted data in a VAO, sets up the
-	 * joint heirarchy, and loads up the entity's texture.
-	 * 
-	 * @param entityFile
-	 *            - the file containing the data for the entity.
-	 * @return The animated entity (no animation applied though)
-	 */
-	public static AnimatedModel loadEntity(InternalFile modelFile, ModelTexture texture) {
-		AnimatedModelData entityData = ColladaLoader.loadColladaModel(modelFile, MAX_WEIGHTS);
-		Vao model = createVao(entityData.getMeshData());
-		JointsData skeletonData = entityData.getJointsData();
-		Joint headJoint = createJoints(skeletonData.headJoint);
-		return new AnimatedModel(model, texture, headJoint, skeletonData.jointCount);
-	}
-
-	/**
-	 * Constructs the joint-hierarchy skeleton from the data extracted from the
-	 * collada file.
-	 * 
-	 * @param data
-	 *            - the joints data from the collada file for the head joint.
-	 * @return The created joint, with all its descendants added.
-	 */
-	private static Joint createJoints(JointData data) {
-		Joint joint = new Joint(data.index, data.nameId, data.bindLocalTransform);
-		for (JointData child : data.children) {
-			joint.addChild(createJoints(child));
-		}
-		return joint;
-	}
-
-	/**
-	 * Stores the mesh data in a VAO.
-	 * 
-	 * @param data
-	 *            - all the data about the mesh that needs to be stored in the
-	 *            VAO.
-	 * @return The VAO containing all the mesh data for the model.
-	 */
-	private static Vao createVao(MeshData data) {
-		Vao vao = Vao.create();
-		vao.bind();
-		vao.createIndexBuffer(data.getIndices());
-		vao.createAttribute(0, data.getVertices(), 3);
-		vao.createAttribute(1, data.getTextureCoords(), 2);
-		vao.createAttribute(2, data.getNormals(), 3);
-		vao.createIntAttribute(4, data.getJointIds(), 3);
-		vao.createAttribute(5, data.getVertexWeights(), 3);
-		vao.unbind();
-		return vao;
-	}
-
-}

+ 41 - 11
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

@@ -13,8 +13,12 @@ import org.json.JSONArray;
 import org.json.JSONObject;
 
 import eu.tankernn.gameEngine.animation.animatedModel.AnimatedModel;
-import eu.tankernn.gameEngine.animation.loaders.AnimatedModelLoader;
+import eu.tankernn.gameEngine.animation.animatedModel.Joint;
 import eu.tankernn.gameEngine.animation.loaders.AnimationLoader;
+import eu.tankernn.gameEngine.loader.colladaLoader.AnimatedModelData;
+import eu.tankernn.gameEngine.loader.colladaLoader.ColladaLoader;
+import eu.tankernn.gameEngine.loader.colladaLoader.JointsData;
+import eu.tankernn.gameEngine.loader.colladaLoader.MeshData;
 import eu.tankernn.gameEngine.loader.models.AABB;
 import eu.tankernn.gameEngine.loader.models.TexturedModel;
 import eu.tankernn.gameEngine.loader.obj.ModelData;
@@ -30,16 +34,18 @@ import eu.tankernn.gameEngine.util.InternalFile;
  * @author Frans
  */
 public class Loader {
+	public static final int MAX_WEIGHTS = 3;
+	
 	private List<Vao> vaos = new ArrayList<>();
 	private List<Texture> textures = new ArrayList<>();
 	private Map<Integer, TexturedModel> models = new HashMap<>();
-	private List<AABB> boundingBoxes = new ArrayList<>();
+	private Map<Integer, AABB> boundingBoxes = new HashMap<>();
 	
 	public Vao loadToVAO(float[] vertices, float[] textureCoords, float[] normals, int[] indices) {
-		return loadToVAO(vertices, textureCoords, normals, null, indices);
+		return loadToVAO(vertices, textureCoords, normals, null, null, null, indices);
 	}
 	
-	public Vao loadToVAO(float[] vertices, float[] textureCoords, float[] normals, float[] tangents, int[] indices) {
+	public Vao loadToVAO(float[] vertices, float[] textureCoords, float[] normals, float[] tangents, int[] jointIds, float[] weights, int[] indices) {
 		Vao model = Vao.create();
 		model.bind();
 		model.createIndexBuffer(indices);
@@ -48,6 +54,10 @@ public class Loader {
 		model.createAttribute(2, normals, 3);
 		if (tangents != null)
 			model.createAttribute(3, tangents, 3);
+		if (jointIds != null)
+			model.createIntAttribute(4, jointIds, 3);
+		if (weights != null)
+			model.createAttribute(5, weights, 3);
 		model.unbind();
 		vaos.add(model);
 		return model;
@@ -73,7 +83,11 @@ public class Loader {
 	}
 	
 	public Vao loadToVAO(ModelData data) {
-		return loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(), data.getIndices());
+		return loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(), null, null, data.getIndices());
+	}
+	
+	public Vao loadToVAO(MeshData data) {
+		return loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(), data.getJointIds(), data.getVertexWeights(), data.getIndices());
 	}
 	
 	/**
@@ -134,8 +148,25 @@ public class Loader {
 	
 	public Vao loadOBJ(InternalFile objFile) {
 		ModelData data = ObjLoader.loadOBJ(objFile);
-		boundingBoxes.add(new AABB(data));
-		return this.loadToVAO(data);
+		Vao vao = this.loadToVAO(data);
+		boundingBoxes.put(vao.id, new AABB(data));
+		return vao;
+	}
+	
+	/**
+	 * Creates an AnimatedEntity from the data in an entity file. It loads up
+	 * the collada model data, stores the extracted data in a VAO, sets up the
+	 * joint heirarchy, and loads up the entity's texture.
+	 * 
+	 * @param entityFile - the file containing the data for the entity.
+	 * @return The animated entity (no animation applied though)
+	 */
+	public AnimatedModel loadDAE(InternalFile modelFile, ModelTexture texture) {
+		AnimatedModelData entityData = ColladaLoader.loadColladaModel(modelFile, MAX_WEIGHTS);
+		Vao model = loadToVAO(entityData.getMeshData());
+		JointsData skeletonData = entityData.getJointsData();
+		Joint headJoint = new Joint(skeletonData.headJoint);
+		return new AnimatedModel(model, texture, headJoint, skeletonData.jointCount);
 	}
 	
 	public void readModelSpecification(InternalFile file) throws IOException {
@@ -202,10 +233,9 @@ public class Loader {
 					models.put(id, new TexturedModel(model, modelTexture));
 					break;
 				case "dae":
-					AnimatedModel animatedModel = AnimatedModelLoader.loadEntity(modelFile, modelTexture);
+					AnimatedModel animatedModel = loadDAE(modelFile, modelTexture);
 					String animations = spec.getString("animations");
 					animatedModel.registerAnimations(AnimationLoader.loadAnimations(modelFile, new InternalFile("models/" + animations)));
-					
 					models.put(id, animatedModel);
 					break;
 				default:
@@ -231,7 +261,7 @@ public class Loader {
 			return model;
 	}
 	
-	public AABB getBoundingBox(int id) {
-		return boundingBoxes.get(id);
+	public AABB getBoundingBox(int vaoId) {
+		return boundingBoxes.get(vaoId);
 	}
 }

+ 6 - 1
src/main/java/eu/tankernn/gameEngine/loader/colladaLoader/GeometryLoader.java

@@ -25,6 +25,7 @@ public class GeometryLoader {
 	private float[] verticesArray;
 	private float[] normalsArray;
 	private float[] texturesArray;
+	private float[] tangentsArray;
 	private int[] indicesArray;
 	private int[] jointIdsArray;
 	private float[] weightsArray;
@@ -48,7 +49,7 @@ public class GeometryLoader {
 		initArrays();
 		convertDataToArrays();
 		convertIndicesListToArray();
-		return new MeshData(verticesArray, texturesArray, normalsArray, indicesArray, jointIdsArray, weightsArray, 1);
+		return new MeshData(verticesArray, texturesArray, normalsArray, tangentsArray, indicesArray, jointIdsArray, weightsArray, 1);
 	}
 
 	private void readRawData() {
@@ -144,6 +145,7 @@ public class GeometryLoader {
 			Vector3f position = currentVertex.getPosition();
 			Vector2f textureCoord = textures.get(currentVertex.getTextureIndex());
 			Vector3f normalVector = normals.get(currentVertex.getNormalIndex());
+			Vector3f tangent = currentVertex.getAverageTangent();
 			verticesArray[i * 3] = position.x;
 			verticesArray[i * 3 + 1] = position.y;
 			verticesArray[i * 3 + 2] = position.z;
@@ -152,6 +154,9 @@ public class GeometryLoader {
 			normalsArray[i * 3] = normalVector.x;
 			normalsArray[i * 3 + 1] = normalVector.y;
 			normalsArray[i * 3 + 2] = normalVector.z;
+			tangentsArray[i * 3] = tangent.x;
+			tangentsArray[i * 3 + 1] = tangent.y;
+			tangentsArray[i * 3 + 2] = tangent.z;
 			VertexSkinData weights = currentVertex.getWeightsData();
 			jointIdsArray[i * 3] = weights.jointIds.get(0);
 			jointIdsArray[i * 3 + 1] = weights.jointIds.get(1);

+ 6 - 33
src/main/java/eu/tankernn/gameEngine/loader/colladaLoader/MeshData.java

@@ -1,26 +1,19 @@
 package eu.tankernn.gameEngine.loader.colladaLoader;
 
-public class MeshData {
+import eu.tankernn.gameEngine.loader.obj.ModelData;
+
+public class MeshData extends ModelData {
 
 	private static final int DIMENSIONS = 3;
 
-	private float[] vertices;
-	private float[] textureCoords;
-	private float[] normals;
-	private int[] indices;
 	private int[] jointIds;
 	private float[] vertexWeights;
-	private float furthestPoint;
 
-	public MeshData(float[] vertices, float[] textureCoords, float[] normals, int[] indices,
+	public MeshData(float[] vertices, float[] textureCoords, float[] normals, float[] tangents, int[] indices,
 			int[] jointIds, float[] vertexWeights, float furthestPoint) {
-		this.vertices = vertices;
-		this.textureCoords = textureCoords;
-		this.normals = normals;
-		this.indices = indices;
+		super(vertices, textureCoords, normals, tangents, indices, furthestPoint);
 		this.jointIds = jointIds;
 		this.vertexWeights = vertexWeights;
-		this.furthestPoint = furthestPoint;
 	}
 
 	public int[] getJointIds() {
@@ -32,27 +25,7 @@ public class MeshData {
 	}
 
 	public int getVertexCount() {
-		return vertices.length / DIMENSIONS;
-	}
-
-	public float[] getVertices() {
-		return vertices;
-	}
-
-	public float[] getTextureCoords() {
-		return textureCoords;
-	}
-
-	public float[] getNormals() {
-		return normals;
-	}
-
-	public int[] getIndices() {
-		return indices;
-	}
-
-	public float getFurthestPoint() {
-		return furthestPoint;
+		return getVertices().length / DIMENSIONS;
 	}
 
 }