diff --git a/src/engineTester/MainGameLoop.java b/src/engineTester/MainGameLoop.java index c31cae6..da496fe 100644 --- a/src/engineTester/MainGameLoop.java +++ b/src/engineTester/MainGameLoop.java @@ -46,6 +46,8 @@ public class MainGameLoop { ModelTexture texture = new ModelTexture(loader.loadTexture("white")); TexturedModel staticModel = new TexturedModel(model, texture); + texture.setShineDamper(10); + texture.setReflectivity(1); Entity entity = new Entity(staticModel, new Vector3f(0,0,-25), new Vector3f(0,0,0), 1); Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1)); diff --git a/src/renderEngine/Renderer.java b/src/renderEngine/Renderer.java index 15720eb..db0b014 100644 --- a/src/renderEngine/Renderer.java +++ b/src/renderEngine/Renderer.java @@ -13,6 +13,7 @@ import entities.Entity; import models.RawModel; import models.TexturedModel; import shaders.StaticShader; +import textures.ModelTexture; import toolbox.Maths; /** @@ -50,6 +51,8 @@ public class Renderer { GL20.glEnableVertexAttribArray(2); Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotation(), entity.getScale()); shader.loadTransformationMatrix(transformationMatrix); + ModelTexture texture = texturedModel.getTexture(); + shader.loadShineVariable(texture.getShineDamper(), texture.getReflectivity()); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getTexturedID()); GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0); diff --git a/src/shaders/StaticShader.java b/src/shaders/StaticShader.java index 1621831..dce68e8 100644 --- a/src/shaders/StaticShader.java +++ b/src/shaders/StaticShader.java @@ -15,7 +15,9 @@ public class StaticShader extends ShaderProgram { private int location_viewMatrix; private int location_lightPosition; private int location_lightColour; - + private int location_reflectivity; + private int location_shineDamper; + public StaticShader() { super(VERTEX_FILE, FRAGMENT_FILE); } @@ -34,8 +36,14 @@ public class StaticShader extends ShaderProgram { location_viewMatrix = super.getUniformLocation("viewMatrix"); location_lightPosition = super.getUniformLocation("lightPosition"); location_lightColour = super.getUniformLocation("lightColour"); + location_reflectivity = super.getUniformLocation("reflectivity"); + location_shineDamper = super.getUniformLocation("shineDamper"); } - + public void loadShineVariable(float shineDamper, float reflectivity) { + super.loadFloat(location_reflectivity, reflectivity); + super.loadFloat(location_shineDamper, shineDamper); + } + public void loadTransformationMatrix(Matrix4f matrix) { super.loadMatrix(location_transformationMatrix, matrix); } diff --git a/src/shaders/fragmentShader.txt b/src/shaders/fragmentShader.txt index efe56fb..1aa8cf7 100644 --- a/src/shaders/fragmentShader.txt +++ b/src/shaders/fragmentShader.txt @@ -3,11 +3,14 @@ in vec2 pass_textureCoords; in vec3 surfaceNormal; in vec3 toLightVector; +in vec3 toCameraVector; out vec4 out_Color; uniform sampler2D textureSampler; uniform vec3 lightColour; +uniform float reflectivity; +uniform float shineDamper; void main(void) { @@ -19,6 +22,15 @@ void main(void) { float brightness = max(nDot1, 0.0); vec3 diffuse = brightness * lightColour; - out_Color = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoords); + vec3 unitVectorToCamera = normalize(toCameraVector); + vec3 lightDirection = -unitLightVector; + vec3 reflectedLightDirection = reflect(lightDirection, unitNormal); + + float specularFactor = dot(reflectedLightDirection, unitVectorToCamera); + specularFactor = max(specularFactor, 0.0); + float damperFactor = pow(specularFactor, shineDamper); + vec3 finalSpecular = damperFactor * reflectivity * lightColour; + + out_Color = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoords) + vec4(finalSpecular, 1.0); } diff --git a/src/shaders/vertexShader.txt b/src/shaders/vertexShader.txt index 6c0b1cb..404f7bf 100644 --- a/src/shaders/vertexShader.txt +++ b/src/shaders/vertexShader.txt @@ -7,6 +7,7 @@ in vec3 normal; out vec2 pass_textureCoords; out vec3 surfaceNormal; out vec3 toLightVector; +out vec3 toCameraVector; uniform mat4 transformationMatrix; uniform mat4 projectionMatrix; @@ -20,5 +21,6 @@ void main(void) { surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz; toLightVector = lightPosition - worldPosition.xyz; + toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz; } diff --git a/src/textures/ModelTexture.java b/src/textures/ModelTexture.java index eb3520d..62d5f2e 100644 --- a/src/textures/ModelTexture.java +++ b/src/textures/ModelTexture.java @@ -4,6 +4,11 @@ public class ModelTexture { private int textureID; + // Reflectivity of the light on the surface of the texture + private float reflectivity = 0; + // Distance of whitch the camera must to be to receive the the reflection + private float shineDamper = 1; + public ModelTexture(int id) { this.textureID = id; } @@ -11,5 +16,21 @@ public class ModelTexture { public int getTexturedID() { return textureID; } + + public float getReflectivity() { + return reflectivity; + } + + public void setReflectivity(float reflectivity) { + this.reflectivity = reflectivity; + } + + public float getShineDamper() { + return shineDamper; + } + + public void setShineDamper(float shineDamper) { + this.shineDamper = shineDamper; + } }