Browse Source

Sun rendering added

Tankernn 7 years ago
parent
commit
5c4d848146

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/TankernnGame3D.java

@@ -44,7 +44,7 @@ public class TankernnGame3D extends TankernnGame {
 	protected List<Projectile> projectiles = new ArrayList<>();
 	protected List<Light> lights = new ArrayList<>();
 	protected List<FloatingTexture> floatTextures = new ArrayList<>();
-	private Light sun;
+	protected Light sun;
 	protected TerrainPack terrainPack;
 	protected Player player;
 	

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/loader/textures/TextureAtlas.java

@@ -21,7 +21,7 @@ public class TextureAtlas {
 		this.type = type;
 		this.rows = rows;
 	}
-	
+
 	public void bindToUnit(int unit) {
 		GL13.glActiveTexture(GL13.GL_TEXTURE0 + unit);
 		GL11.glBindTexture(type, textureId);

+ 16 - 0
src/main/java/eu/tankernn/gameEngine/particles/IParticle.java

@@ -0,0 +1,16 @@
+package eu.tankernn.gameEngine.particles;
+
+import org.lwjgl.util.vector.Vector2f;
+
+import eu.tankernn.gameEngine.entities.Camera;
+import eu.tankernn.gameEngine.util.IPositionable;
+
+public interface IParticle extends IPositionable {
+	ParticleTexture getTexture();
+	float getScale();
+	float getRotation();
+	float getBlend();
+	Vector2f getTexOffset1();
+	Vector2f getTexOffset2();
+	boolean update(Camera camera);
+}

+ 2 - 3
src/main/java/eu/tankernn/gameEngine/particles/Particle.java

@@ -6,10 +6,9 @@ import org.lwjgl.util.vector.Vector3f;
 import eu.tankernn.gameEngine.entities.Camera;
 import eu.tankernn.gameEngine.renderEngine.DisplayManager;
 import eu.tankernn.gameEngine.settings.Physics;
-import eu.tankernn.gameEngine.util.IPositionable;
 
 
-public class Particle implements IPositionable {
+public class Particle implements IParticle {
 	private Vector3f position;
 	private Vector3f velocity;
 	private float gravityEffect;
@@ -70,7 +69,7 @@ public class Particle implements IPositionable {
 		return distance;
 	}
 
-	protected boolean update(Camera camera) {
+	public boolean update(Camera camera) {
 		velocity.y += Physics.GRAVITY * gravityEffect * DisplayManager.getFrameTimeSeconds();
 		change.set(velocity);
 		change.scale(DisplayManager.getFrameTimeSeconds());

+ 9 - 9
src/main/java/eu/tankernn/gameEngine/particles/ParticleMaster.java

@@ -22,7 +22,7 @@ import eu.tankernn.gameEngine.util.DistanceSorter;
 public class ParticleMaster {
 	private Loader loader;
 
-	private Map<ParticleTexture, List<Particle>> particles = new HashMap<>();
+	private Map<ParticleTexture, List<IParticle>> particles = new HashMap<>();
 	private List<ParticleSystem> systems = new ArrayList<>();
 	private ParticleRenderer renderer;
 	private FontRenderer fontRenderer = new FontRenderer();
@@ -39,13 +39,13 @@ public class ParticleMaster {
 			}
 		}
 
-		Iterator<Entry<ParticleTexture, List<Particle>>> mapIterator = particles.entrySet().iterator();
+		Iterator<Entry<ParticleTexture, List<IParticle>>> mapIterator = particles.entrySet().iterator();
 		while (mapIterator.hasNext()) {
-			Entry<ParticleTexture, List<Particle>> entry = mapIterator.next();
-			List<Particle> list = entry.getValue();
-			Iterator<Particle> iterator = list.iterator();
+			Entry<ParticleTexture, List<IParticle>> entry = mapIterator.next();
+			List<IParticle> list = entry.getValue();
+			Iterator<IParticle> iterator = list.iterator();
 			while (iterator.hasNext()) {
-				Particle p = iterator.next();
+				IParticle p = iterator.next();
 				boolean stillAlive = p.update(camera);
 				if (!stillAlive) {
 					iterator.remove();
@@ -81,10 +81,10 @@ public class ParticleMaster {
 				0.1f, 4, 0, font.size));
 	}
 
-	public void addParticle(Particle particle) {
-		List<Particle> list = particles.get(particle.getTexture());
+	public void addParticle(IParticle particle) {
+		List<IParticle> list = particles.get(particle.getTexture());
 		if (list == null) {
-			list = new ArrayList<Particle>();
+			list = new ArrayList<IParticle>();
 			particles.put(particle.getTexture(), list);
 		}
 		list.add(particle);

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

@@ -42,15 +42,15 @@ public class ParticleRenderer {
 		shader.stop();
 	}
 	
-	protected void render(Map<ParticleTexture, List<Particle>> particles, Camera camera) {
+	protected void render(Map<ParticleTexture, List<IParticle>> particles, Camera camera) {
 		Matrix4f viewMatrix = camera.getViewMatrix();
 		prepare();
 		for (ParticleTexture texture: particles.keySet()) {
 			bindTexture(texture);
-			List<Particle> particleList = particles.get(texture);
+			List<IParticle> particleList = particles.get(texture);
 			pointer = 0;
 			float[] vboData = new float[particleList.size() * INSTANCE_DATA_LENGTH];
-			for (Particle p: particleList) {
+			for (IParticle p: particleList) {
 				updateModelViewMatrix(p.getPosition(), p.getRotation(), p.getScale(), viewMatrix, vboData);
 				updateTexCoordInfo(p, vboData);
 			}
@@ -65,12 +65,12 @@ public class ParticleRenderer {
 		shader.finalize();
 	}
 	
-	private void updateTexCoordInfo(Particle particle, float[] data) {
-		data[pointer++] = particle.getTexOffset1().x;
-		data[pointer++] = particle.getTexOffset1().y;
-		data[pointer++] = particle.getTexOffset2().x;
-		data[pointer++] = particle.getTexOffset2().y;
-		data[pointer++] = particle.getBlend();
+	private void updateTexCoordInfo(IParticle p, float[] data) {
+		data[pointer++] = p.getTexOffset1().x;
+		data[pointer++] = p.getTexOffset1().y;
+		data[pointer++] = p.getTexOffset2().x;
+		data[pointer++] = p.getTexOffset2().y;
+		data[pointer++] = p.getBlend();
 	}
 	
 	private void bindTexture(ParticleTexture texture) {

+ 90 - 0
src/main/java/eu/tankernn/gameEngine/particles/Sun.java

@@ -0,0 +1,90 @@
+package eu.tankernn.gameEngine.particles;
+
+import org.lwjgl.util.vector.Vector2f;
+import org.lwjgl.util.vector.Vector3f;
+
+import eu.tankernn.gameEngine.entities.Camera;
+
+public class Sun implements IParticle {
+
+	private static final float SUN_DIS = 50;// fairly arbitrary - but make sure
+											// it doesn't go behind skybox
+
+	private final ParticleTexture texture;
+
+	private Vector3f lightDirection = new Vector3f(0, -1, 0);
+	private Vector3f position;
+	private float scale;
+
+	public Sun(ParticleTexture texture, float scale) {
+		this.texture = texture;
+		this.scale = scale;
+	}
+
+	public void setScale(float scale) {
+		this.scale = scale;
+	}
+
+	public void setDirection(float x, float y, float z) {
+		lightDirection.set(x, y, z);
+		lightDirection.normalise();
+	}
+
+	public ParticleTexture getTexture() {
+		return texture;
+	}
+
+	public Vector3f getLightDirection() {
+		return lightDirection;
+	}
+
+	public float getScale() {
+		return scale;
+	}
+	
+	public Vector3f getPosition() {
+		return position;
+	}
+
+	/**
+	 * Calculates a position for the sun, based on the light direction. The
+	 * distance of the sun from the camera is fairly arbitrary, although care
+	 * should be taken to ensure it doesn't get rendered outside the skybox.
+	 * 
+	 * @param camPos - The camera's position.
+	 * @return The 3D world position of the sun.
+	 */
+	public Vector3f calculateWorldPosition(Vector3f camPos) {
+		Vector3f sunPos = new Vector3f(lightDirection);
+		sunPos.negate();
+		sunPos.scale(SUN_DIS);
+		return Vector3f.add(camPos, sunPos, null);
+	}
+
+	@Override
+	public float getRotation() {
+		return 0;
+	}
+
+	@Override
+	public float getBlend() {
+		return 0;
+	}
+
+	@Override
+	public Vector2f getTexOffset1() {
+		return new Vector2f(0, 0);
+	}
+
+	@Override
+	public Vector2f getTexOffset2() {
+		return new Vector2f(0, 0);
+	}
+
+	@Override
+	public boolean update(Camera camera) {
+		this.position = calculateWorldPosition(camera.getPosition());
+		return true;
+	}
+
+}