Browse Source

Particle system now uses the Vao and Vbo classes

Also added under-water mode for post-processing system.
Tankernn 8 years ago
parent
commit
e288aaaded

+ 0 - 2
src/main/java/eu/tankernn/gameEngine/TankernnGame3D.java

@@ -1,6 +1,5 @@
 package eu.tankernn.gameEngine;
 
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
@@ -13,7 +12,6 @@ import eu.tankernn.gameEngine.entities.Camera;
 import eu.tankernn.gameEngine.entities.Entity3D;
 import eu.tankernn.gameEngine.entities.Light;
 import eu.tankernn.gameEngine.entities.Player;
-import eu.tankernn.gameEngine.entities.PlayerCamera;
 import eu.tankernn.gameEngine.environmentMap.EnvironmentMapRenderer;
 import eu.tankernn.gameEngine.loader.textures.Texture;
 import eu.tankernn.gameEngine.particles.ParticleMaster;

+ 3 - 39
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

@@ -19,7 +19,6 @@ import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL15;
 import org.lwjgl.opengl.GL20;
 import org.lwjgl.opengl.GL30;
-import org.lwjgl.opengl.GL33;
 
 import eu.tankernn.gameEngine.animation.animatedModel.AnimatedModel;
 import eu.tankernn.gameEngine.animation.loaders.AnimatedModelLoader;
@@ -47,15 +46,7 @@ public class Loader {
 	private List<AABB> boundingBoxes = new ArrayList<>();
 	
 	public Vao loadToVAO(float[] vertices, float[] textureCoords, float[] normals, int[] indices) {
-		Vao model = Vao.create();
-		model.bind();
-		model.createIndexBuffer(indices);
-		model.createAttribute(0, vertices, 3);
-		model.createAttribute(1, textureCoords, 2);
-		model.createAttribute(2, normals, 3);
-		model.unbind();
-		rawModels.add(model);
-		return model;
+		return loadToVAO(vertices, textureCoords, normals, null, indices);
 	}
 	
 	public Vao loadToVAO(float[] vertices, float[] textureCoords, float[] normals, float[] tangents, int[] indices) {
@@ -65,40 +56,13 @@ public class Loader {
 		model.createAttribute(0, vertices, 3);
 		model.createAttribute(1, textureCoords, 2);
 		model.createAttribute(2, normals, 3);
-		model.createAttribute(3, tangents, 3);
+		if (tangents != null)
+			model.createAttribute(3, tangents, 3);
 		model.unbind();
 		rawModels.add(model);
 		return model;
 	}
 	
-	public int createEmptyVBO(int floatCount) {
-		int vboID = GL15.glGenBuffers();
-		vbos.add(vboID);
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
-		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, floatCount * 4, GL15.GL_STREAM_DRAW);
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
-		return vboID;
-	}
-	
-	public void addInstacedAttribute(int vao, int vbo, int attribute, int dataSize, int instancedDataLength, int offset) {
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
-		GL30.glBindVertexArray(vao);
-		GL20.glVertexAttribPointer(attribute, dataSize, GL11.GL_FLOAT, false, instancedDataLength * 4, offset * 4);
-		GL33.glVertexAttribDivisor(attribute, 1);
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
-		GL30.glBindVertexArray(0);
-	}
-	
-	public void updateVBO(int vboID, float[] data, FloatBuffer buffer) {
-		buffer.clear();
-		buffer.put(data);
-		buffer.flip();
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
-		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer.capacity() * 4, GL15.GL_STREAM_DRAW);
-		GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, buffer);
-		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
-	}
-	
 	public int loadToVAO(float[] positions, float[] textureCoords) {
 		int vaoID = createVAO();
 		storeDataInAttributeList(0, 2, positions);

+ 7 - 7
src/main/java/eu/tankernn/gameEngine/particles/ParticleRenderer.java

@@ -6,6 +6,7 @@ import java.util.Map;
 
 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 org.lwjgl.opengl.GL31;
@@ -15,6 +16,7 @@ import org.lwjgl.util.vector.Vector3f;
 import eu.tankernn.gameEngine.entities.Camera;
 import eu.tankernn.gameEngine.loader.Loader;
 import eu.tankernn.gameEngine.renderEngine.Vao;
+import eu.tankernn.gameEngine.renderEngine.Vbo;
 
 public class ParticleRenderer {
 	
@@ -27,17 +29,15 @@ public class ParticleRenderer {
 	private Vao quad;
 	private ParticleShader shader;
 	
-	private Loader loader;
-	private int vbo;
+	private Vbo vbo;
 	private int pointer = 0;
 	
 	protected ParticleRenderer(Loader loader, Matrix4f projectionMatrix) {
-		this.loader = loader;
-		this.vbo = loader.createEmptyVBO(INSTANCE_DATA_LENGTH * MAX_INSTANCES);
+		this.vbo = Vbo.create(GL15.GL_ARRAY_BUFFER, GL15.GL_STREAM_DRAW, INSTANCE_DATA_LENGTH * MAX_INSTANCES);
 		quad = loader.loadToVAO(VERTICES, 2);
 		for (int i = 0; i < 5; i++)
-			loader.addInstacedAttribute(quad.id, vbo, i + 1, 4, INSTANCE_DATA_LENGTH, i * 4);
-		loader.addInstacedAttribute(quad.id, vbo, 6, 1, INSTANCE_DATA_LENGTH, 20);
+			quad.addInstacedAttribute(vbo, i + 1, 4, INSTANCE_DATA_LENGTH, i * 4);
+		quad.addInstacedAttribute(vbo, 6, 1, INSTANCE_DATA_LENGTH, 20);
 		shader = new ParticleShader();
 		shader.start();
 		shader.projectionMatrix.loadMatrix(projectionMatrix);
@@ -56,7 +56,7 @@ public class ParticleRenderer {
 				updateModelViewMatrix(p.getPosition(), p.getRotation(), p.getScale(), viewMatrix, vboData);
 				updateTexCoordInfo(p, vboData);
 			}
-			loader.updateVBO(vbo, vboData, buffer);
+			vbo.updateData(vboData, buffer);
 			GL31.glDrawArraysInstanced(GL11.GL_TRIANGLE_STRIP, 0, quad.getIndexCount(), particleList.size());
 		}
 		finishRendering();

+ 20 - 16
src/main/java/eu/tankernn/gameEngine/postProcessing/PostProcessor.java

@@ -15,29 +15,33 @@ import eu.tankernn.gameEngine.postProcessing.gaussianBlur.VerticalBlur;
 import eu.tankernn.gameEngine.renderEngine.Vao;
 
 public class PostProcessor {
-
-	private final int blurFactor = 0;
-
-	private final float[] POSITIONS = { -1, 1, -1, -1, 1, 1, 1, -1 };
+	
+	private final float[] POSITIONS = {-1, 1, -1, -1, 1, 1, 1, -1};
 	private Vao quad;
 	private List<PostProcessingEffect<?>> effects = new ArrayList<PostProcessingEffect<?>>();
 	private CombineFilter combineFilter;
+	public final int blurFactor;
 	
-	public PostProcessor(Loader loader) {
+	public PostProcessor(Loader loader, boolean underWater) {
+		this.blurFactor = underWater ? 2 : 0;
 		quad = loader.loadToVAO(POSITIONS, 2);
-		effects.add(new ContrastChanger());
-		for (int i = 0; i < blurFactor; i++) {
-			int temp = i + 1;
-			effects.add(new HorizontalBlur(Display.getWidth() / temp, Display.getHeight() / temp));
-			effects.add(new VerticalBlur(Display.getWidth() / temp, Display.getHeight() / temp));
+		if (!underWater)
+			effects.add(new ContrastChanger());
+		for (int i = 1; i < blurFactor + 1; i++) {
+			effects.add(new HorizontalBlur(Display.getWidth() / i, Display.getHeight() / i));
+			effects.add(new VerticalBlur(Display.getWidth() / i, Display.getHeight() / i));
 		}
 		effects.add(new BrightFilter(Display.getWidth() / 2, Display.getHeight() / 2));
 		combineFilter = new CombineFilter();
 	}
-
+	
+	public PostProcessor(Loader loader) {
+		this(loader, false);
+	}
+	
 	public void doPostProcessing(Texture colorTexture, Texture brightTexture) {
 		start();
-		for (PostProcessingEffect<?> effect : effects) {
+		for (PostProcessingEffect<?> effect: effects) {
 			effect.render(colorTexture, brightTexture);
 			colorTexture = effect.getOutputColorTexture();
 			brightTexture = effect.getOutputBrightTexture();
@@ -45,20 +49,20 @@ public class PostProcessor {
 		combineFilter.render(colorTexture, brightTexture);
 		end();
 	}
-
+	
 	public void cleanUp() {
 		effects.forEach(p -> p.cleanUp());
 		combineFilter.cleanUp();
 	}
-
+	
 	private void start() {
 		quad.bind(0);
 		GL11.glDisable(GL11.GL_DEPTH_TEST);
 	}
-
+	
 	private void end() {
 		GL11.glEnable(GL11.GL_DEPTH_TEST);
 		quad.unbind(0);
 	}
-
+	
 }

+ 10 - 0
src/main/java/eu/tankernn/gameEngine/renderEngine/Vao.java

@@ -7,6 +7,7 @@ import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL15;
 import org.lwjgl.opengl.GL20;
 import org.lwjgl.opengl.GL30;
+import org.lwjgl.opengl.GL33;
 
 public class Vao {
 	
@@ -74,6 +75,15 @@ public class Vao {
 		dataVbos.add(dataVbo);
 	}
 	
+	public void addInstacedAttribute(Vbo vbo, int attribute, int dataSize, int instancedDataLength, int offset) {
+		vbo.bind();
+		this.bind();
+		GL20.glVertexAttribPointer(attribute, dataSize, GL11.GL_FLOAT, false, instancedDataLength * 4, offset * 4);
+		GL33.glVertexAttribDivisor(attribute, 1);
+		vbo.unbind();
+		this.unbind();
+	}
+	
 	public void delete() {
 		GL30.glDeleteVertexArrays(id);
 		for(Vbo vbo : dataVbos){

+ 38 - 6
src/main/java/eu/tankernn/gameEngine/renderEngine/Vbo.java

@@ -10,15 +10,33 @@ public class Vbo {
 	
 	private final int vboId;
 	private final int type;
+	private final int usage;
 	
-	private Vbo(int vboId, int type){
+	private Vbo(int vboId, int type, int usage){
 		this.vboId = vboId;
 		this.type = type;
+		this.usage = usage;
 	}
 	
-	public static Vbo create(int type){
+	private Vbo(int vboId, int type, int usage, int size) {
+		this(vboId, type, usage);
+		bind();
+		GL15.glBufferData(type, size * 4, usage);
+		unbind();
+	}
+
+	public static Vbo create(int type, int usage, int size) {
 		int id = GL15.glGenBuffers();
-		return new Vbo(id, type);
+		return new Vbo(id, type, usage, size);
+	}
+	
+	public static Vbo create(int type, int usage){
+		int id = GL15.glGenBuffers();
+		return new Vbo(id, type, usage);
+	}
+	
+	public static Vbo create(int type) {
+		return create(type, GL15.GL_STATIC_DRAW);
 	}
 	
 	public void bind(){
@@ -37,7 +55,7 @@ public class Vbo {
 	}
 	
 	public void storeData(FloatBuffer data){
-		GL15.glBufferData(type, data, GL15.GL_STATIC_DRAW);
+		GL15.glBufferData(type, data, usage);
 	}
 	
 	public void storeData(int[] data){
@@ -48,11 +66,25 @@ public class Vbo {
 	}
 	
 	public void storeData(IntBuffer data){
-		GL15.glBufferData(type, data, GL15.GL_STATIC_DRAW);
+		GL15.glBufferData(type, data, usage);
+	}
+
+	public void updateData(float[] data) {
+		updateData(data, BufferUtils.createFloatBuffer(data.length));
+	}
+	public void updateData(float[] data, FloatBuffer buffer) {
+		bind();
+		buffer.clear();
+		buffer.put(data);
+		buffer.flip();
+		GL15.glBindBuffer(type, vboId);
+		GL15.glBufferData(type, buffer.capacity() * 4, usage);
+		GL15.glBufferSubData(type, 0, buffer);
+		GL15.glBindBuffer(type, 0);
+		unbind();
 	}
 	
 	public void delete(){
 		GL15.glDeleteBuffers(vboId);
 	}
-
 }

+ 0 - 2
src/main/java/eu/tankernn/gameEngine/util/Maths.java

@@ -5,8 +5,6 @@ import org.lwjgl.util.vector.Vector2f;
 import org.lwjgl.util.vector.Vector3f;
 import org.lwjgl.util.vector.Vector4f;
 
-import eu.tankernn.gameEngine.entities.Camera;
-
 public class Maths {
 	public static float distanceBetweenPoints(Vector3f pos1, Vector3f pos2) {
 		float baseWidth = pos1.x - pos2.x;