Răsfoiți Sursa

Loader class cleanup

Tankernn 8 ani în urmă
părinte
comite
27e86122a4

+ 18 - 87
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

@@ -3,22 +3,14 @@ package eu.tankernn.gameEngine.loader;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.math.BigDecimal;
-import java.nio.FloatBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
-import org.lwjgl.BufferUtils;
-import org.lwjgl.opengl.GL11;
-import org.lwjgl.opengl.GL15;
-import org.lwjgl.opengl.GL20;
-import org.lwjgl.opengl.GL30;
 
 import eu.tankernn.gameEngine.animation.animatedModel.AnimatedModel;
 import eu.tankernn.gameEngine.animation.loaders.AnimatedModelLoader;
@@ -38,11 +30,9 @@ import eu.tankernn.gameEngine.util.InternalFile;
  * @author Frans
  */
 public class Loader {
-	private List<Integer> vaos = new ArrayList<Integer>();
-	private List<Integer> vbos = new ArrayList<Integer>();
-	private List<Vao> rawModels = new ArrayList<Vao>();
-	private List<Texture> textures = new ArrayList<Texture>();
-	private Map<Integer, TexturedModel> models = new HashMap<Integer, TexturedModel>();
+	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<>();
 	
 	public Vao loadToVAO(float[] vertices, float[] textureCoords, float[] normals, int[] indices) {
@@ -59,23 +49,25 @@ public class Loader {
 		if (tangents != null)
 			model.createAttribute(3, tangents, 3);
 		model.unbind();
-		rawModels.add(model);
+		vaos.add(model);
 		return model;
 	}
 	
-	public int loadToVAO(float[] positions, float[] textureCoords) {
-		int vaoID = createVAO();
-		storeDataInAttributeList(0, 2, positions);
-		storeDataInAttributeList(1, 2, textureCoords);
-		unbindVAO();
-		return vaoID;
+	public Vao loadToVAO(float[] positions, float[] textureCoords) {
+		Vao vao = Vao.create();
+		vao.bind();
+		vao.storeDataInAttributeList(0, 2, positions);
+		vao.storeDataInAttributeList(1, 2, textureCoords);
+		vao.unbind();
+		return vao;
 	}
 	
 	public Vao loadToVAO(float[] positions, int dimensions) {
-		int vaoID = createVAO();
-		storeDataInAttributeList(0, dimensions, positions);
-		unbindVAO();
-		return new Vao(vaoID, positions.length / 2);
+		Vao vao = Vao.create(positions.length / 2);
+		vao.bind();
+		vao.storeDataInAttributeList(0, dimensions, positions);
+		vao.unbind();
+		return vao;
 	}
 	
 	public Vao loadToVAO(ModelData data) {
@@ -123,7 +115,7 @@ public class Loader {
 		vao.createIndexBuffer(CUBE_INDICES);
 		vao.createAttribute(0, getCubeVertexPositions(size), 3);
 		vao.unbind();
-		rawModels.add(vao);
+		vaos.add(vao);
 		return vao;
 	}
 	
@@ -132,54 +124,18 @@ public class Loader {
 	}
 	
 	public void cleanUp() {
-		for (int vao: vaos)
-			GL30.glDeleteVertexArrays(vao);
-		for (int vbo: vbos)
-			GL15.glDeleteBuffers(vbo);
 		for (Texture tex: textures)
 			tex.delete();
-		for (Vao model: rawModels)
+		for (Vao model: vaos)
 			model.delete();
 	}
 	
-	private int createVAO() {
-		int vaoID = GL30.glGenVertexArrays();
-		vaos.add(vaoID);
-		GL30.glBindVertexArray(vaoID);
-		return vaoID;
-	}
-	
-	private void storeDataInAttributeList(int attributeNumber, int coordinateSize, float[] data) {
-		int vboID = GL15.glGenBuffers();
-		vbos.add(vboID);
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
-		FloatBuffer buffer = storeDataInFloatBuffer(data);
-		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
-		GL20.glVertexAttribPointer(attributeNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
-	}
-	
-	private void unbindVAO() {
-		GL30.glBindVertexArray(0);
-	}
-	
-	private FloatBuffer storeDataInFloatBuffer(float[] data) {
-		FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
-		buffer.put(data);
-		buffer.flip();
-		return buffer;
-	}
-	
 	public Vao loadOBJ(InternalFile objFile) {
 		ModelData data = ObjLoader.loadOBJ(objFile);
 		boundingBoxes.add(new AABB(data));
 		return this.loadToVAO(data);
 	}
 	
-	public AnimatedModel loadDAE(InternalFile colladaFile, ModelTexture texture) {
-		return AnimatedModelLoader.loadEntity(colladaFile, texture);
-	}
-	
 	public void readModelSpecification(InternalFile file) throws IOException {
 		Map<InternalFile, Vao> cachedRawModels = new HashMap<InternalFile, Vao>();
 		Map<InternalFile, Texture> cachedTextures = new HashMap<InternalFile, Texture>();
@@ -268,31 +224,6 @@ public class Loader {
 		return spec.has(key) ? spec.getString(key) : spec.get("name") + extension;
 	}
 	
-	public int registerModel(int id, TexturedModel model) throws Exception {
-		if (models.containsKey(id)) {
-			throw new Exception("There is already a model registered for the key " + id + ".");
-		} else {
-			models.put(id, model);
-			return id;
-		}
-	}
-	
-	public int registerModel(TexturedModel model) throws Exception {
-		if (models.containsValue(model)) {
-			Iterator<Entry<Integer, TexturedModel>> i = models.entrySet().iterator();
-			
-			while (i.hasNext()) {
-				Entry<Integer, TexturedModel> e = i.next();
-				if (e.getValue().equals(model)) {
-					return e.getKey();
-				}
-			}
-			// Should be impossible
-			throw new IllegalStateException();
-		} else
-			return registerModel(models.size(), model);
-	}
-	
 	public TexturedModel getModel(int id) {
 		TexturedModel model = models.get(id);
 		if (model instanceof AnimatedModel)

+ 5 - 3
src/main/java/eu/tankernn/gameEngine/loader/font/GUIText.java

@@ -3,6 +3,8 @@ package eu.tankernn.gameEngine.loader.font;
 import org.lwjgl.util.vector.Vector2f;
 import org.lwjgl.util.vector.Vector3f;
 
+import eu.tankernn.gameEngine.renderEngine.Vao;
+
 /**
  * Represents a piece of text in the game.
  * 
@@ -16,7 +18,7 @@ public class GUIText {
 	
 	private float fontSize;
 
-	private int textMeshVao;
+	private Vao textMeshVao;
 	private int vertexCount;
 	private Vector3f colour = new Vector3f(0f, 0f, 0f);
 
@@ -114,7 +116,7 @@ public class GUIText {
 	 * @return the ID of the text's VAO, which contains all the vertex data for
 	 *         the quads on which the text will be rendered.
 	 */
-	public int getMesh() {
+	public Vao getMesh() {
 		return textMeshVao;
 	}
 
@@ -127,7 +129,7 @@ public class GUIText {
 	 * @param verticesCount
 	 *            - the total number of vertices in all of the quads.
 	 */
-	public void setMeshInfo(int vao, int verticesCount) {
+	public void setMeshInfo(Vao vao, int verticesCount) {
 		this.textMeshVao = vao;
 		this.vertexCount = verticesCount;
 	}

+ 31 - 17
src/main/java/eu/tankernn/gameEngine/renderEngine/Vao.java

@@ -17,47 +17,52 @@ public class Vao {
 	private List<Vbo> dataVbos = new ArrayList<Vbo>();
 	private Vbo indexVbo;
 	private int indexCount;
-
+	
 	public static Vao create() {
 		int id = GL30.glGenVertexArrays();
 		return new Vao(id);
 	}
 
+	public static Vao create(int indexCount) {
+		int id = GL30.glGenVertexArrays();
+		return new Vao(id, indexCount);
+	}
+	
 	private Vao(int id) {
 		this.id = id;
 	}
 	
-	public Vao(int id, int indexCount) {
+	private Vao(int id, int indexCount) {
 		this(id);
 		this.indexCount = indexCount;
 	}
-
-	public int getIndexCount(){
+	
+	public int getIndexCount() {
 		return indexCount;
 	}
-
-	public void bind(int... attributes){
+	
+	public void bind(int... attributes) {
 		bind();
-		for (int i : attributes) {
+		for (int i: attributes) {
 			GL20.glEnableVertexAttribArray(i);
 		}
 	}
-
-	public void unbind(int... attributes){
-		for (int i : attributes) {
+	
+	public void unbind(int... attributes) {
+		for (int i: attributes) {
 			GL20.glDisableVertexAttribArray(i);
 		}
 		unbind();
 	}
 	
-	public void createIndexBuffer(int[] indices){
+	public void createIndexBuffer(int[] indices) {
 		this.indexVbo = Vbo.create(GL15.GL_ELEMENT_ARRAY_BUFFER);
 		indexVbo.bind();
 		indexVbo.storeData(indices);
 		this.indexCount = indices.length;
 	}
-
-	public void createAttribute(int attribute, float[] data, int attrSize){
+	
+	public void createAttribute(int attribute, float[] data, int attrSize) {
 		Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER);
 		dataVbo.bind();
 		dataVbo.storeData(data);
@@ -66,7 +71,7 @@ public class Vao {
 		dataVbos.add(dataVbo);
 	}
 	
-	public void createIntAttribute(int attribute, int[] data, int attrSize){
+	public void createIntAttribute(int attribute, int[] data, int attrSize) {
 		Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER);
 		dataVbo.bind();
 		dataVbo.storeData(data);
@@ -84,18 +89,27 @@ public class Vao {
 		this.unbind();
 	}
 	
+	public void storeDataInAttributeList(int attributeNumber, int coordinateSize, float[] data) {
+		Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER, GL15.GL_STATIC_DRAW);
+		dataVbo.bind();
+		dataVbo.storeData(data);
+		GL20.glVertexAttribPointer(attributeNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);
+		dataVbo.unbind();
+		dataVbos.add(dataVbo);
+	}
+	
 	public void delete() {
 		GL30.glDeleteVertexArrays(id);
-		for(Vbo vbo : dataVbos){
+		for (Vbo vbo: dataVbos) {
 			vbo.delete();
 		}
 		indexVbo.delete();
 	}
-
+	
 	private void bind() {
 		GL30.glBindVertexArray(id);
 	}
-
+	
 	private void unbind() {
 		GL30.glBindVertexArray(0);
 	}

+ 2 - 8
src/main/java/eu/tankernn/gameEngine/renderEngine/font/FontRenderer.java

@@ -4,8 +4,6 @@ import java.util.List;
 import java.util.Map;
 
 import org.lwjgl.opengl.GL11;
-import org.lwjgl.opengl.GL20;
-import org.lwjgl.opengl.GL30;
 
 import eu.tankernn.gameEngine.loader.font.FontType;
 import eu.tankernn.gameEngine.loader.font.GUIText;
@@ -41,15 +39,11 @@ public class FontRenderer {
 	}
 	
 	private void renderText(GUIText text) {
-		GL30.glBindVertexArray(text.getMesh());
-		GL20.glEnableVertexAttribArray(0);
-		GL20.glEnableVertexAttribArray(1);
+		text.getMesh().bind(0, 1);
 		shader.color.loadVec3(text.getColor());
 		shader.translation.loadVec2(text.getPosition());
 		GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, text.getVertexCount());
-		GL20.glDisableVertexAttribArray(0);
-		GL20.glDisableVertexAttribArray(1);
-		GL30.glBindVertexArray(0);
+		text.getMesh().unbind(0, 1);
 	}
 	
 	private void endRendering() {

+ 2 - 1
src/main/java/eu/tankernn/gameEngine/renderEngine/font/TextMaster.java

@@ -9,6 +9,7 @@ import eu.tankernn.gameEngine.loader.Loader;
 import eu.tankernn.gameEngine.loader.font.FontType;
 import eu.tankernn.gameEngine.loader.font.GUIText;
 import eu.tankernn.gameEngine.loader.font.TextMeshData;
+import eu.tankernn.gameEngine.renderEngine.Vao;
 
 public class TextMaster {
 
@@ -34,7 +35,7 @@ public class TextMaster {
 	public void updateText(GUIText text) {
 		FontType font = text.getFont();
 		TextMeshData data = font.loadText(text);
-		int vao = loader.loadToVAO(data.getVertexPositions(), data.getTextureCoords());
+		Vao vao = loader.loadToVAO(data.getVertexPositions(), data.getTextureCoords());
 		text.setMeshInfo(vao, data.getVertexCount());
 	}