Browse Source

Finished water rendering and added texture brightness support

Tankernn 8 years ago
parent
commit
c1374a8ed1

+ 4 - 1
src/main/java/eu/tankernn/gameEngine/normalMapping/renderer/normalMapFShader.glsl

@@ -5,7 +5,8 @@ in vec3 toLightVector[4];
 in vec3 toCameraVector;
 in float visibility;
 
-out vec4 out_Color;
+layout (location = 0) out vec4 out_Color;
+layout (location = 1) out vec4 out_BrightColor;
 
 uniform sampler2D modelTexture;
 uniform sampler2D normalMap;
@@ -58,4 +59,6 @@ void main(void){
 
 	out_Color =  vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular,1.0);
 	out_Color = mix(vec4(skyColour,1.0),out_Color, visibility);
+	
+	out_BrightColor = vec4(0.0);
 }

+ 40 - 31
src/main/java/eu/tankernn/gameEngine/postProcessing/Fbo.java

@@ -1,45 +1,48 @@
 package eu.tankernn.gameEngine.postProcessing;
 
 import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
 
+import org.lwjgl.BufferUtils;
 import org.lwjgl.opengl.Display;
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL12;
 import org.lwjgl.opengl.GL14;
+import org.lwjgl.opengl.GL20;
 import org.lwjgl.opengl.GL30;
 
 public class Fbo {
-	
-	public static final int NONE = 0;
-	public static final int DEPTH_TEXTURE = 1;
-	public static final int DEPTH_RENDER_BUFFER = 2;
-	
+
+	public static final int NONE = 0, DEPTH_TEXTURE = 1, DEPTH_RENDER_BUFFER = 2;
+
 	protected final int width;
 	protected final int height;
-	
+
 	protected int frameBuffer;
-	
+
 	private int colourTexture;
 	private int depthTexture;
-	
+
 	protected int depthBuffer;
-	protected int colorBuffer;
-	
+
 	/**
 	 * Creates an FBO of a specified width and height, with the desired type of
 	 * depth buffer attachment.
 	 * 
-	 * @param width - the width of the FBO.
-	 * @param height - the height of the FBO.
-	 * @param depthBufferType - an int indicating the type of depth buffer
-	 *        attachment that this FBO should use.
+	 * @param width
+	 *            - the width of the FBO.
+	 * @param height
+	 *            - the height of the FBO.
+	 * @param depthBufferType
+	 *            - an int indicating the type of depth buffer attachment that
+	 *            this FBO should use.
 	 */
 	public Fbo(int width, int height, int depthBufferType) {
 		this.width = width;
 		this.height = height;
 		initialiseFrameBuffer(depthBufferType);
 	}
-	
+
 	/**
 	 * Deletes the frame buffer and its attachments when the game closes.
 	 */
@@ -48,9 +51,8 @@ public class Fbo {
 		GL11.glDeleteTextures(colourTexture);
 		GL11.glDeleteTextures(depthTexture);
 		GL30.glDeleteRenderbuffers(depthBuffer);
-		GL30.glDeleteRenderbuffers(colorBuffer);
 	}
-	
+
 	/**
 	 * Binds the frame buffer, setting it as the current render target. Anything
 	 * rendered after this will be rendered to this FBO, and not to the screen.
@@ -59,7 +61,7 @@ public class Fbo {
 		GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer);
 		GL11.glViewport(0, 0, width, height);
 	}
-	
+
 	/**
 	 * Unbinds the frame buffer, setting the default frame buffer as the current
 	 * render target. Anything rendered after this will be rendered to the
@@ -69,7 +71,7 @@ public class Fbo {
 		GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
 		GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
 	}
-	
+
 	/**
 	 * Binds the current FBO to be read from (not used in tutorial 43).
 	 */
@@ -78,32 +80,32 @@ public class Fbo {
 		GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, frameBuffer);
 		GL11.glReadBuffer(GL30.GL_COLOR_ATTACHMENT0);
 	}
-	
+
 	/**
 	 * @return The ID of the texture containing the colour buffer of the FBO.
 	 */
 	public int getColourTexture() {
 		return colourTexture;
 	}
-	
+
 	/**
 	 * @return The texture containing the FBOs depth buffer.
 	 */
 	public int getDepthTexture() {
 		return depthTexture;
 	}
-	
+
 	/**
 	 * Creates the FBO along with a colour buffer texture attachment, and
 	 * possibly a depth buffer.
 	 * 
-	 * @param type - the type of depth buffer attachment to be attached to the
-	 *        FBO.
+	 * @param type
+	 *            - the type of depth buffer attachment to be attached to the
+	 *            FBO.
 	 */
 	protected void initialiseFrameBuffer(int type) {
 		createFrameBuffer();
 		createTextureAttachment();
-		createTextureAttachment();
 		if (type == DEPTH_RENDER_BUFFER) {
 			createDepthBufferAttachment();
 		} else if (type == DEPTH_TEXTURE) {
@@ -112,17 +114,24 @@ public class Fbo {
 		unbindFrameBuffer();
 	}
 	
+	protected void determineDrawBuffers() {
+		IntBuffer drawBuffers = BufferUtils.createIntBuffer(2);
+		drawBuffers.put(GL30.GL_COLOR_ATTACHMENT0);
+		drawBuffers.flip();
+		GL20.glDrawBuffers(drawBuffers);
+	}
+
 	/**
 	 * Creates a new frame buffer object and sets the buffer to which drawing
 	 * will occur - colour attachment 0. This is the attachment where the colour
 	 * buffer texture is.
 	 */
-	private void createFrameBuffer() {
+	protected void createFrameBuffer() {
 		frameBuffer = GL30.glGenFramebuffers();
 		GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
-		GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
+		determineDrawBuffers();
 	}
-	
+
 	/**
 	 * Creates a texture and sets it as the colour buffer attachment for this
 	 * FBO.
@@ -139,7 +148,7 @@ public class Fbo {
 		GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colourTexture,
 				0);
 	}
-	
+
 	/**
 	 * Adds a depth buffer to the FBO in the form of a texture, which can later
 	 * be sampled.
@@ -153,7 +162,7 @@ public class Fbo {
 		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
 		GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthTexture, 0);
 	}
-	
+
 	/**
 	 * Adds a depth buffer to the FBO in the form of a render buffer. This can't
 	 * be used for sampling in the shaders.
@@ -165,5 +174,5 @@ public class Fbo {
 		GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
 				depthBuffer);
 	}
-	
+
 }

+ 46 - 7
src/main/java/eu/tankernn/gameEngine/postProcessing/MultisampleFbo.java → src/main/java/eu/tankernn/gameEngine/postProcessing/MultisampleMultitargetFbo.java

@@ -1,26 +1,36 @@
 package eu.tankernn.gameEngine.postProcessing;
 
+import java.nio.IntBuffer;
+
+import org.lwjgl.BufferUtils;
 import org.lwjgl.opengl.Display;
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL14;
+import org.lwjgl.opengl.GL20;
 import org.lwjgl.opengl.GL30;
 
 import eu.tankernn.gameEngine.renderEngine.DisplayManager;
 
-public class MultisampleFbo extends Fbo {
+public class MultisampleMultitargetFbo extends Fbo {
+	
+
+	private int colorBuffer;
+	private int colorBuffer2;
+	
 	/**
 	 * Creates a multisampled FBO of a specified width and height.
 	 * 
 	 * @param width - the width of the FBO.
 	 * @param height - the height of the FBO.
 	 */
-	public MultisampleFbo(int width, int height) {
+	public MultisampleMultitargetFbo(int width, int height) {
 		super(width, height, DEPTH_RENDER_BUFFER);
 	}
 	
-	public void resolveToFbo(Fbo outputFbo) {
+	public void resolveToFbo(int readBuffer, Fbo outputFbo) {
 		GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, outputFbo.frameBuffer);
 		GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, this.frameBuffer);
+		GL11.glReadBuffer(readBuffer);
 		GL30.glBlitFramebuffer(0, 0, width, height, 0, 0, outputFbo.width, outputFbo.height, GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT, GL11.GL_NEAREST);
 		this.unbindFrameBuffer();
 	}
@@ -33,13 +43,13 @@ public class MultisampleFbo extends Fbo {
 		this.unbindFrameBuffer();
 	}
 	
-	@Override
-	protected void createTextureAttachment() {
-		colorBuffer = GL30.glGenRenderbuffers();
+	protected int createMultisampleColorAttachment(int attachment) {
+		int colorBuffer = GL30.glGenRenderbuffers();
 		GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, colorBuffer);
 		GL30.glRenderbufferStorageMultisample(GL30.GL_RENDERBUFFER, DisplayManager.MULTISAMPLING, GL11.GL_RGBA8, width, height);
-		GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL30.GL_RENDERBUFFER,
+		GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, attachment, GL30.GL_RENDERBUFFER,
 				colorBuffer);
+		return colorBuffer;
 	}
 	
 	@Override
@@ -50,4 +60,33 @@ public class MultisampleFbo extends Fbo {
 		GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
 				depthBuffer);
 	}
+	
+	@Override
+	protected void initialiseFrameBuffer(int type) {
+		createFrameBuffer();
+		colorBuffer = createMultisampleColorAttachment(GL30.GL_COLOR_ATTACHMENT0);
+		colorBuffer2 = createMultisampleColorAttachment(GL30.GL_COLOR_ATTACHMENT1);
+		if (type == DEPTH_RENDER_BUFFER) {
+			createDepthBufferAttachment();
+		} else if (type == DEPTH_TEXTURE) {
+			createDepthTextureAttachment();
+		}
+		unbindFrameBuffer();
+	}
+	
+	@Override
+	protected void determineDrawBuffers() {
+		IntBuffer drawBuffers = BufferUtils.createIntBuffer(2);
+		drawBuffers.put(GL30.GL_COLOR_ATTACHMENT0);
+		drawBuffers.put(GL30.GL_COLOR_ATTACHMENT1);
+		drawBuffers.flip();
+		GL20.glDrawBuffers(drawBuffers);
+	};
+	
+	@Override
+	public void cleanUp() {
+		super.cleanUp();
+		GL30.glDeleteRenderbuffers(colorBuffer);
+		GL30.glDeleteRenderbuffers(colorBuffer2);
+	}
 }

+ 3 - 3
src/main/java/eu/tankernn/gameEngine/postProcessing/PostProcessing.java

@@ -36,10 +36,10 @@ public class PostProcessing {
 		combineFilter = new CombineFilter();
 	}
 	
-	public static void doPostProcessing(int colorTexture) {
+	public static void doPostProcessing(int colorTexture, int brightTexture) {
 		start();
-		brightFilter.render(colorTexture);
-		int bloomTexture = brightFilter.getOutputTexture();
+		//brightFilter.render(colorTexture);
+		int bloomTexture = brightTexture;
 		for (int i = 0; i < blurFactor; i++) {
 			hBlur[i].render(bloomTexture);
 			vBlur[i].render(hBlur[i].getOutputTexture());

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/settings/Settings.java

@@ -1,7 +1,7 @@
 package eu.tankernn.gameEngine.settings;
 
 public class Settings {
-	public static final boolean DEBUG = true;
+	public static final boolean DEBUG = false;
 	public static final String GAME_NAME = "Tankernn game engine tester";
 	
 	//Display settings

+ 6 - 1
src/main/java/eu/tankernn/gameEngine/shaders/fragmentShader.glsl

@@ -7,7 +7,8 @@ in vec3 toCameraVector;
 in float visibility;
 in vec4 shadowCoords;
 
-out vec4 out_Color;
+layout (location = 0) out vec4 out_Color;
+layout (location = 1) out vec4 out_BrightColor;
 
 uniform sampler2D shadowMap;
 uniform sampler2D textureSampler;
@@ -61,14 +62,18 @@ void main(void){
 		discard;
 	}
 	
+	out_BrightColor = vec4(0.0);
 	if (usesSpecularMap > 0.5) {
 		vec4 mapInfo = texture(specularMap, pass_textureCoords);
 		totalSpecular *= mapInfo.r;
 		if (mapInfo.g > 0.5) {
+			out_BrightColor = textureColor + vec4(totalSpecular, 1.0);
 			totalDiffuse = vec3(1.0);
 		}
 	}
 	
 	out_Color = vec4(totalDiffuse, 1.0) * textureColor + vec4(totalSpecular, 1.0);
 	out_Color = mix(vec4(skyColor, 1.0), out_Color, visibility);
+	
+	
 }

+ 4 - 1
src/main/java/eu/tankernn/gameEngine/shaders/terrainFragmentShader.glsl

@@ -7,7 +7,8 @@ in vec3 toCameraVector;
 in float visibility;
 in vec4 shadowCoords;
 
-out vec4 out_Color;
+layout (location = 0) out vec4 out_Color;
+layout (location = 1) out vec4 out_BrightColor;
 
 uniform sampler2D backgroundTexture;
 uniform sampler2D rTexture;
@@ -81,4 +82,6 @@ void main(void) {
 	
 	out_Color = vec4(totalDiffuse, 1.0) * totalColor + vec4(totalSpecular, 1.0);
 	out_Color = mix(vec4(skyColor, 1.0), out_Color, visibility);
+	
+	out_BrightColor = vec4(0.0);
 }

+ 28 - 20
src/main/java/eu/tankernn/gameEngine/tester/MainLoop.java

@@ -1,13 +1,12 @@
 package eu.tankernn.gameEngine.tester;
 
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 import java.util.Random;
 
 import org.lwjgl.input.Mouse;
 import org.lwjgl.opengl.Display;
+import org.lwjgl.opengl.GL30;
 import org.lwjgl.util.vector.Vector2f;
 import org.lwjgl.util.vector.Vector3f;
 import org.lwjgl.util.vector.Vector4f;
@@ -31,19 +30,19 @@ import eu.tankernn.gameEngine.particles.ParticleMaster;
 import eu.tankernn.gameEngine.particles.ParticleSystem;
 import eu.tankernn.gameEngine.particles.ParticleTexture;
 import eu.tankernn.gameEngine.postProcessing.Fbo;
-import eu.tankernn.gameEngine.postProcessing.MultisampleFbo;
+import eu.tankernn.gameEngine.postProcessing.MultisampleMultitargetFbo;
 import eu.tankernn.gameEngine.postProcessing.PostProcessing;
 import eu.tankernn.gameEngine.renderEngine.DisplayManager;
 import eu.tankernn.gameEngine.renderEngine.Loader;
 import eu.tankernn.gameEngine.renderEngine.MasterRenderer;
 import eu.tankernn.gameEngine.renderEngine.Scene;
+import eu.tankernn.gameEngine.settings.Settings;
 import eu.tankernn.gameEngine.terrains.Terrain;
 import eu.tankernn.gameEngine.terrains.TerrainPack;
 import eu.tankernn.gameEngine.textures.ModelTexture;
 import eu.tankernn.gameEngine.textures.TerrainTexture;
 import eu.tankernn.gameEngine.textures.TerrainTexturePack;
 import eu.tankernn.gameEngine.util.DistanceSorter;
-import eu.tankernn.gameEngine.util.Maths;
 import eu.tankernn.gameEngine.util.MousePicker;
 import eu.tankernn.gameEngine.util.NativesExporter;
 import eu.tankernn.gameEngine.water.WaterMaster;
@@ -150,20 +149,24 @@ public class MainLoop {
 
 		// #### Gui rendering ####
 		List<GuiTexture> guis = new ArrayList<GuiTexture>();
-		GuiTexture depth = new GuiTexture(waterMaster.getBuffers().getRefractionDepthTexture(),
-				new Vector2f(0.5f, 0.5f), new Vector2f(0.25f, 0.25f));
-		GuiTexture refraction = new GuiTexture(waterMaster.getBuffers().getRefractionTexture(),
-				new Vector2f(-0.5f, 0.5f), new Vector2f(0.25f, 0.25f));
-		guis.add(depth);
-		guis.add(refraction);
+		if (Settings.DEBUG) {
+			GuiTexture depth = new GuiTexture(waterMaster.getBuffers().getRefractionDepthTexture(),
+					new Vector2f(0.5f, 0.5f), new Vector2f(0.25f, 0.25f));
+			GuiTexture refraction = new GuiTexture(waterMaster.getBuffers().getRefractionTexture(),
+					new Vector2f(-0.5f, 0.5f), new Vector2f(0.25f, 0.25f));
+			guis.add(depth);
+			guis.add(refraction);
+		}
 
 		GuiRenderer guiRenderer = new GuiRenderer(loader);
 
 		ParticleTexture particleTexture = new ParticleTexture(loader.loadTexture("particles/cosmic"), 4, true);
 		ParticleSystem ps = new ParticleSystem(particleTexture, 50, 10, 0.3f, 4);
 
-		MultisampleFbo multisampleFbo = new MultisampleFbo(Display.getWidth(), Display.getHeight());
+		MultisampleMultitargetFbo multisampleFbo = new MultisampleMultitargetFbo(Display.getWidth(),
+				Display.getHeight());
 		Fbo outputFbo = new Fbo(Display.getWidth(), Display.getHeight(), Fbo.DEPTH_TEXTURE);
+		Fbo outputFbo2 = new Fbo(Display.getWidth(), Display.getHeight(), Fbo.DEPTH_TEXTURE);
 
 		PostProcessing.init(loader);
 
@@ -191,13 +194,16 @@ public class MainLoop {
 			}
 
 			// Update debug info
-
-			Terrain currentTerrain = terrainPack.getTerrainByWorldPos(player.getPosition().x, player.getPosition().z);
-			if (currentTerrain != null) {
-				text.remove();
-				Vector3f pos = player.getPosition();
-				String textString = "X: " + Math.floor(pos.x) + " Y: " + Math.floor(pos.y) + " Z: " + Math.floor(pos.z);
-				text = new GUIText(textString, 1, font, new Vector2f(0.5f, 0f), 0.5f, false);
+			if (Settings.DEBUG) {
+				Terrain currentTerrain = terrainPack.getTerrainByWorldPos(player.getPosition().x,
+						player.getPosition().z);
+				if (currentTerrain != null) {
+					text.remove();
+					Vector3f pos = player.getPosition();
+					String textString = "X: " + Math.floor(pos.x) + " Y: " + Math.floor(pos.y) + " Z: "
+							+ Math.floor(pos.z);
+					text = new GUIText(textString, 1, font, new Vector2f(0.5f, 0f), 0.5f, false);
+				}
 			}
 
 			// Sort list of lights
@@ -220,8 +226,9 @@ public class MainLoop {
 
 			multisampleFbo.unbindFrameBuffer();
 
-			multisampleFbo.resolveToFbo(outputFbo);
-			PostProcessing.doPostProcessing(outputFbo.getColourTexture());
+			multisampleFbo.resolveToFbo(GL30.GL_COLOR_ATTACHMENT0, outputFbo);
+			multisampleFbo.resolveToFbo(GL30.GL_COLOR_ATTACHMENT1, outputFbo2);
+			PostProcessing.doPostProcessing(outputFbo.getColourTexture(), outputFbo2.getColourTexture());
 
 			guiRenderer.render(guis);
 			TextMaster.render();
@@ -231,6 +238,7 @@ public class MainLoop {
 
 		PostProcessing.cleanUp();
 		outputFbo.cleanUp();
+		outputFbo2.cleanUp();
 		multisampleFbo.cleanUp();
 		ParticleMaster.cleanUp();
 		TextMaster.cleanUp();

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/water/WaterMaster.java

@@ -44,7 +44,7 @@ public class WaterMaster {
 		
 		// Refraction
 		buffers.bindRefractionFrameBuffer();
-		renderer.renderScene(scene, new Vector4f(0, -1, 0, waterHeight));
+		renderer.renderScene(scene, new Vector4f(0, -1, 0, waterHeight + 1f));
 		
 		// Screen
 		GL11.glDisable(GL30.GL_CLIP_DISTANCE0);

+ 4 - 0
src/main/java/eu/tankernn/gameEngine/water/WaterRenderer.java

@@ -74,9 +74,13 @@ public class WaterRenderer {
 		GL11.glBindTexture(GL11.GL_TEXTURE_2D, normalMap);
 		GL13.glActiveTexture(GL13.GL_TEXTURE4);
 		GL11.glBindTexture(GL11.GL_TEXTURE_2D, buffers.getRefractionDepthTexture());
+		
+		GL11.glEnable(GL11.GL_BLEND);
+		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
 	}
 	
 	private void unbind(){
+		GL11.glDisable(GL11.GL_BLEND);
 		GL20.glDisableVertexAttribArray(0);
 		GL30.glBindVertexArray(0);
 		shader.stop();

+ 14 - 12
src/main/java/eu/tankernn/gameEngine/water/waterFragment.glsl

@@ -5,7 +5,8 @@ in vec2 textureCoords;
 in vec3 toCameraVector;
 in vec3 fromLightVector[4]; //4 max light sources
 
-out vec4 out_Color;
+layout (location = 0) out vec4 out_Color;
+layout (location = 1) out vec4 out_BrightColor;
 
 uniform sampler2D reflectionTexture;
 uniform sampler2D refractionTexture;
@@ -16,11 +17,11 @@ uniform vec3 lightColor[4]; //4 max light sources
 uniform vec3 attenuation[4];
 uniform float moveFactor;
 
-const float waveStrength = 0.02;
+const float waveStrength = 0.04;
 const float shineDamper = 20.0;
 const float reflectivity = 0.5;
-const float farPlane = 0.1;
-const float nearPlane = 1000.0;
+const float nearPlane = 0.1;
+const float farPlane = 1000.0;
 
 void main(void) {
 	
@@ -37,7 +38,7 @@ void main(void) {
 	
 	vec2 distortedTexCoords = texture(dudvMap, vec2(textureCoords.x + moveFactor, textureCoords.y)).rg * 0.1;
 	distortedTexCoords = textureCoords + vec2(distortedTexCoords.x, distortedTexCoords.y + moveFactor);
-	vec2 totalDistortion = (texture(dudvMap, distortedTexCoords).rg * 2.0 -1.0) * waveStrength;
+	vec2 totalDistortion = (texture(dudvMap, distortedTexCoords).rg * 2.0 -1.0) * waveStrength * clamp(waterDepth/20.0, 0.0, 1.0);
 	
 	refractTexCoords += totalDistortion;
 	refractTexCoords = clamp(refractTexCoords, 0.001, 0.999);
@@ -49,15 +50,15 @@ void main(void) {
 	vec4 reflectColor = texture(reflectionTexture, reflectTexCoords);
 	vec4 refractColor = texture(refractionTexture, refractTexCoords);
 	
+	vec4 normalMapColor = texture(normalMap, distortedTexCoords);
+	vec3 normal = vec3(normalMapColor.r * 2.0 - 1.0, normalMapColor.b * 3.0, normalMapColor.g * 2.0 - 1.0);
+	normal = normalize(normal);
+	
 	vec3 viewVector = normalize(toCameraVector);
-	float refractiveFactor = dot(viewVector, vec3(0.0, 1.0, 0.0));
+	float refractiveFactor = dot(viewVector, normal);
 	refractiveFactor = pow(refractiveFactor, 1.0); // More or less reflective
 	refractiveFactor = clamp(refractiveFactor, 0.0, 1.0);
 	
-	vec4 normalMapColor = texture(normalMap, distortedTexCoords);
-	vec3 normal = vec3(normalMapColor.r * 2.0 - 1.0, normalMapColor.b, normalMapColor.g * 2.0 - 1.0);
-	normal = normalize(normal);
-	
 	vec3 specularHighlights = vec3(0.0);
 	for (int i = 0; i < 4; i++) {
 		float distance = length(fromLightVector[i]);
@@ -65,11 +66,12 @@ void main(void) {
 		vec3 reflectedLight = reflect(normalize(fromLightVector[i]), normal);
 		float specular = max(dot(reflectedLight, viewVector), 0.0);
 		specular = pow(specular, shineDamper);
-		specularHighlights = specularHighlights + (lightColor[i] * specular * reflectivity)/attFactor;
+		specularHighlights = specularHighlights + (lightColor[i] * specular * reflectivity * clamp(waterDepth/20.0, 0.0, 1.0))/attFactor;
 	}
 	
 	out_Color = mix(reflectColor, refractColor, refractiveFactor);
 	out_Color = mix(out_Color, vec4(0.0, 0.3, 0.5, 1.0), 0.2) + vec4(specularHighlights, 0.0);
+	out_Color.a = clamp(waterDepth/5.0, 0.0, 1.0);
 	
-	//out_Color = vec4(waterDepth/50.0);
+	out_BrightColor = vec4(0.0);
 }

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/water/waterVertex.glsl

@@ -13,7 +13,7 @@ uniform mat4 modelMatrix;
 uniform vec3 cameraPosition;
 uniform vec3 lightPosition[4]; //4 max light sources
 
-const float tiling = 10.0;
+const float tiling = 4.0;
 
 void main(void) {