Compare commits

...

3 Commits

24 changed files with 2594 additions and 70 deletions

BIN
res/health.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

11
res/lamp.mtl Normal file
View File

@ -0,0 +1,11 @@
# Blender MTL File: 'newLamp.blend'
# Material Count: 1
newmtl None
Ns 0
Ka 0.000000 0.000000 0.000000
Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8
d 1
illum 2
map_Kd C:\Users\Karl\workspace\JGame\src\res\newLamp.png

1006
res/lamp.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
res/lamp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

1228
res/pine.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
res/pine.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 474 KiB

BIN
res/socuwan.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector3f;
import org.lwjgl.openvr.Texture;
@ -12,6 +13,8 @@ import entities.Camera;
import entities.Entity;
import entities.Light;
import entities.Player;
import guis.GuiRenderer;
import guis.GuiTexture;
import models.RawModel;
import models.TexturedModel;
import renderEngine.DisplayManager;
@ -47,6 +50,8 @@ public class MainGameLoop {
Loader loader = new Loader();
manager.init();
List<Entity> entities = new ArrayList<Entity>();
Random random = new Random();
TexturedModel staticModel = new TexturedModel(OBJLoader.loadObjModel("tree", loader),
@ -69,9 +74,35 @@ public class MainGameLoop {
new ModelTexture(loader.loadTexture("fern_atlas")));
fernModel.getTexture().setHasTransparency(true);
fernModel.getTexture().setNumberOfRows(2);
Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1));
TexturedModel lampModel = new TexturedModel(OBJLoader.loadObjModel("lamp", loader),
new ModelTexture(loader.loadTexture("lamp")));
//lampModel.getTexture().setHasTransparency(true);
lampModel.getTexture().setUseFakeLighting(true); // this permit to the light to glow
TexturedModel pineModel = new TexturedModel(OBJLoader.loadObjModel("pine", loader),
new ModelTexture(loader.loadTexture("pine")));
List<Light> lights = new ArrayList<Light>();
lights.add(new Light(new Vector3f(0,10000,-7000), new Vector3f(0.4f,0.4f,0.4f)));
lights.add(new Light(new Vector3f(185,10,-293), new Vector3f(2,0,0), new Vector3f(1,0.01f,0.002f)));
lights.add(new Light(new Vector3f(370,17,-300), new Vector3f(0,2,2), new Vector3f(1,0.01f,0.002f)));
lights.add(new Light(new Vector3f(293,7,-305), new Vector3f(2,2,0), new Vector3f(1,0.01f,0.002f)));
entities.add(new Entity(lampModel,
new Vector3f(185, -4.7f, -293),
new Vector3f(0,0,0),
1));
entities.add(new Entity(lampModel,
new Vector3f(370, 4.2f, -300),
new Vector3f(0,0,0),
1));
entities.add(new Entity(lampModel,
new Vector3f(293, -6.8f, -305),
new Vector3f(0,0,0),
1));
TerrainTexture backgroundTexture = new TerrainTexture(loader.loadTexture("grass"));
TerrainTexture rTexture = new TerrainTexture(loader.loadTexture("dirt"));
TerrainTexture gTexture = new TerrainTexture(loader.loadTexture("grassFlowers"));
@ -83,10 +114,8 @@ public class MainGameLoop {
Terrain terrain = new Terrain(0,-1,loader, texturePack, blendMap, "heightmap");
List<Entity> entities = new ArrayList<Entity>();
Random random = new Random();
for (int iii=0; iii<500; iii++) {
for (int iii=0; iii<250; iii++) {
float x = random.nextFloat()*800 - 400;
float z = random.nextFloat() * -600;
float y = terrain.getHeightOfTerrain(x, z);
@ -94,7 +123,15 @@ public class MainGameLoop {
new Vector3f(x, y, z),
new Vector3f(0,0,0),3));
}
for (int iii=0; iii<5000; iii++) {
for (int iii=0; iii<250; iii++) {
float x = random.nextFloat()*800 - 400;
float z = random.nextFloat() * -600;
float y = terrain.getHeightOfTerrain(x, z);
entities.add(new Entity(pineModel,
new Vector3f(x, y, z),
new Vector3f(0,0,0),0.5f));
}
for (int iii=0; iii<500; iii++) {
float x = random.nextFloat()*800 - 400;
float z = random.nextFloat() * -600;
float y = terrain.getHeightOfTerrain(x, z);
@ -107,29 +144,38 @@ public class MainGameLoop {
TexturedModel playerModel = new TexturedModel(OBJLoader.loadObjModel("person", loader),
new ModelTexture(loader.loadTexture("playerTexture")));
Player player = new Player(playerModel, new Vector3f(0,terrain.getHeightOfTerrain(0, -50),-50), new Vector3f(0,3.14f,0), 0.4f);
Player player = new Player(playerModel, new Vector3f(180,terrain.getHeightOfTerrain(180, -250),-250), new Vector3f(0,3.14f,0), 0.4f);
Camera camera = new Camera(player);
List<GuiTexture> guis = new ArrayList<GuiTexture>();
GuiTexture gui = new GuiTexture(loader.loadTexture("health"), new Vector2f(-0.75f, 0.9f), new Vector2f(0.25f, 0.25f));
guis.add(gui);
GuiRenderer guiRenderer = new GuiRenderer(loader);
MasterRenderer renderer = new MasterRenderer();
manager.setDrawer(new DisplayManagerDraw() {
@Override
public void draw() {
//entity.increasePosition(0.0f, 0, -0.01f);
//entity.increaseRotation(0, 0, 0.01f);
//entity.increaseRotation(0.01f, 0.02f, 0.0f);
camera.move();
player.move(terrain);
camera.move();
renderer.processTerrain(terrain);
renderer.processEntity(player);
for (Entity entity : entities) {
entity.increaseRotation(0, 0.01f, 0.0f);
renderer.processEntity(entity);
}
renderer.render(light, camera);
}
renderer.render(lights, camera);
guiRenderer.render(guis);
}
});
manager.loop();
guiRenderer.cleanUp();
renderer.cleanUp();
loader.cleanUp();
manager.unInit();

View File

@ -5,11 +5,16 @@ import org.atriaSoft.etk.math.Vector3f;
public class Light {
private Vector3f position;
private Vector3f colour;
private Vector3f attenuation = new Vector3f(1, 0, 0);
public Light(Vector3f position, Vector3f colour) {
super();
this.position = position;
this.colour = colour;
}
public Light(Vector3f position, Vector3f colour, Vector3f attenuation) {
this.position = position;
this.colour = colour;
this.setAttenuation(attenuation);
}
public Vector3f getPosition() {
return position;
}
@ -22,5 +27,11 @@ public class Light {
public void setColour(Vector3f colour) {
this.colour = colour;
}
public Vector3f getAttenuation() {
return attenuation;
}
public void setAttenuation(Vector3f attenuation) {
this.attenuation = attenuation;
}
}

52
src/guis/GuiRenderer.java Normal file
View File

@ -0,0 +1,52 @@
package guis;
import java.util.List;
import org.atriaSoft.etk.math.Matrix4f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import models.RawModel;
import renderEngine.Loader;
import toolbox.Maths;
public class GuiRenderer {
private final RawModel quad;
private GuiShader shader;
public GuiRenderer(Loader loader) {
float[] positions = {-1, 1, -1, -1, 1, 1, 1, -1};
quad = loader.loadToVAO(positions);
shader = new GuiShader();
}
public void render(List<GuiTexture> guis) {
shader.start();
//System.out.println("Render Gui " + guis.size());
GL30.glBindVertexArray(quad.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glDisable(GL11.GL_DEPTH_TEST);
for (GuiTexture gui: guis) {
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, gui.getTexture());
Matrix4f matrix = Maths.createTransformationMatrix(gui.getPosition(), gui.getScale());
shader.loadTransformation(matrix);
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getVertexCount());
}
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
shader.stop();
}
public void cleanUp() {
shader.cleanUp();
}
}

35
src/guis/GuiShader.java Normal file
View File

@ -0,0 +1,35 @@
package guis;
import org.atriaSoft.etk.math.Matrix4f;
import shaders.ShaderProgram;
public class GuiShader extends ShaderProgram {
private static final String VERTEX_FILE = "src/guis/guiVertexShader.txt";
private static final String FRAGMENT_FILE = "src/guis/guiFragmentShader.txt";
private int location_transformationMatrix;
public GuiShader() {
super(VERTEX_FILE, FRAGMENT_FILE);
}
public void loadTransformation(Matrix4f matrix){
super.loadMatrix(location_transformationMatrix, matrix);
}
@Override
protected void getAllUniformLocations() {
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
}
@Override
protected void bindAttributes() {
super.bindAttribute(0, "position");
}
}

33
src/guis/GuiTexture.java Normal file
View File

@ -0,0 +1,33 @@
package guis;
import org.atriaSoft.etk.math.Vector2f;
public class GuiTexture {
private int texture;
private Vector2f position;
private Vector2f scale;
public GuiTexture(int texture, Vector2f position, Vector2f scale) {
this.texture = texture;
this.position = position;
this.scale = scale;
}
public int getTexture() {
return texture;
}
public void setTexture(int texture) {
this.texture = texture;
}
public Vector2f getPosition() {
return position;
}
public void setPosition(Vector2f position) {
this.position = position;
}
public Vector2f getScale() {
return scale;
}
public void setScale(Vector2f scale) {
this.scale = scale;
}
}

View File

@ -0,0 +1,11 @@
#version 400 core
in vec2 textureCoords;
out vec4 out_Color;
uniform sampler2D guiTexture;
void main(void){
out_Color = texture(guiTexture,textureCoords);
}

View File

@ -0,0 +1,13 @@
#version 400 core
in vec2 position;
out vec2 textureCoords;
uniform mat4 transformationMatrix;
void main(void){
gl_Position = transformationMatrix * vec4(position, 0.0, 1.0);
textureCoords = vec2((position.x+1.0)/2.0, 1 - (position.y+1.0)/2.0);
}

View File

@ -50,8 +50,17 @@ public class Loader {
return new RawModel(vaoID, indices.length);
}
public RawModel loadToVAO(float[] positions) {
int vaoID = createVAO();
this.storeDataInAttributeList(0, 2, positions);
unbindVAO();
return new RawModel(vaoID, positions.length/2);
}
public int loadTexture(String filename) {
int textureId = loadPNGTexture("res/" + filename + ".png", GL13.GL_TEXTURE0);
String fullname = "res/" + filename + ".png";
System.out.println("Load texture : " + fullname);
int textureId = loadPNGTexture(fullname, GL13.GL_TEXTURE0);
this.textures.add(textureId);
return textureId;
}

View File

@ -54,18 +54,18 @@ public class MasterRenderer {
GL11.glDisable(GL11.GL_BACK);
}
public void render(Light sun, Camera camera) {
public void render(List<Light> lights, Camera camera) {
prepare();
shader.start();
shader.loadSkyColour(SKY_COLOUR);
shader.loadLight(sun);
shader.loadLights(lights);
shader.loadViewMatrix(camera);
renderer.render(entities);
shader.stop();
entities.clear();
terrainShader.start();
terrainShader.loadSkyColour(SKY_COLOUR);
terrainShader.loadLight(sun);
terrainShader.loadLights(lights);
terrainShader.loadViewMatrix(camera);
terrainRenderer.render(terrains);
terrainShader.stop();

View File

@ -90,6 +90,7 @@ public abstract class ShaderProgram {
}
private static int loadShader(String file, int type) {
System.out.println("Load shader: '" + file + "'");
StringBuilder shaderSource = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));

View File

@ -1,5 +1,7 @@
package shaders;
import java.util.List;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector3f;
@ -9,14 +11,16 @@ import entities.Light;
import toolbox.Maths;
public class StaticShader extends ShaderProgram {
private static final int MAX_LIGHTS = 4;
private static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";
private int location_transformationMatrix;
private int location_projectionMatrix;
private int location_viewMatrix;
private int location_lightPosition;
private int location_lightColour;
private int location_lightPosition[];
private int location_lightColour[];
private int location_lightAttenuation[];
private int location_reflectivity;
private int location_shineDamper;
private int location_useFakeLighting;
@ -40,14 +44,21 @@ public class StaticShader extends ShaderProgram {
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
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");
location_useFakeLighting = super.getUniformLocation("useFakeLighting");
location_skyColor = super.getUniformLocation("skyColor");
location_numberOfRows = super.getUniformLocation("numberOfRows");
location_offset = super.getUniformLocation("offset");
location_lightPosition = new int[MAX_LIGHTS];
location_lightColour = new int[MAX_LIGHTS];
location_lightAttenuation = new int[MAX_LIGHTS];
for (int iii=0; iii<MAX_LIGHTS; iii++) {
location_lightPosition[iii] = super.getUniformLocation("lightPosition[" + iii+ "]");
location_lightColour[iii] = super.getUniformLocation("lightColour[" + iii+ "]");
location_lightAttenuation[iii] = super.getUniformLocation("lightAttenuation[" + iii+ "]");
}
}
@ -75,9 +86,18 @@ public class StaticShader extends ShaderProgram {
public void loadTransformationMatrix(Matrix4f matrix) {
super.loadMatrix(location_transformationMatrix, matrix);
}
public void loadLight(Light light) {
super.loadVector(location_lightPosition, light.getPosition());
super.loadVector(location_lightColour, light.getColour());
public void loadLights(List<Light> lights) {
for (int iii=0; iii<MAX_LIGHTS; iii++) {
if (iii<lights.size()) {
super.loadVector(location_lightPosition[iii], lights.get(iii).getPosition());
super.loadVector(location_lightColour[iii], lights.get(iii).getColour());
super.loadVector(location_lightAttenuation[iii], lights.get(iii).getAttenuation());
} else {
super.loadVector(location_lightPosition[iii], new Vector3f(0,0,0));
super.loadVector(location_lightColour[iii], new Vector3f(0,0,0));
super.loadVector(location_lightAttenuation[iii], new Vector3f(1,0,0));
}
}
}
public void loadProjectionMatrix(Matrix4f projection) {
super.loadMatrix(location_projectionMatrix, projection);

View File

@ -1,5 +1,7 @@
package shaders;
import java.util.List;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
@ -8,14 +10,16 @@ import entities.Light;
import toolbox.Maths;
public class TerrainShader extends ShaderProgram {
private static final int MAX_LIGHTS = 4;
private static final String VERTEX_FILE = "src/shaders/terrainVertexShader.txt";
private static final String FRAGMENT_FILE = "src/shaders/terrainFragmentShader.txt";
private int location_transformationMatrix;
private int location_projectionMatrix;
private int location_viewMatrix;
private int location_lightPosition;
private int location_lightColour;
private int location_lightPosition[];
private int location_lightColour[];
private int location_lightAttenuation[];
private int location_reflectivity;
private int location_shineDamper;
private int location_skyColor;
@ -41,8 +45,6 @@ public class TerrainShader extends ShaderProgram {
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
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");
location_skyColor = super.getUniformLocation("skyColor");
@ -51,6 +53,14 @@ public class TerrainShader extends ShaderProgram {
location_gTexture = super.getUniformLocation("gTexture");
location_bTexture = super.getUniformLocation("bTexture");
location_blendMap = super.getUniformLocation("blendMap");
location_lightPosition = new int[MAX_LIGHTS];
location_lightColour = new int[MAX_LIGHTS];
location_lightAttenuation = new int[MAX_LIGHTS];
for (int iii=0; iii<MAX_LIGHTS; iii++) {
location_lightPosition[iii] = super.getUniformLocation("lightPosition[" + iii+ "]");
location_lightColour[iii] = super.getUniformLocation("lightColour[" + iii+ "]");
location_lightAttenuation[iii] = super.getUniformLocation("lightAttenuation[" + iii+ "]");
}
}
public void connectTextureUnits() {
@ -73,10 +83,19 @@ public class TerrainShader extends ShaderProgram {
public void loadTransformationMatrix(Matrix4f matrix) {
super.loadMatrix(location_transformationMatrix, matrix);
}
public void loadLight(Light light) {
super.loadVector(location_lightPosition, light.getPosition());
super.loadVector(location_lightColour, light.getColour());
public void loadLights(List<Light> lights) {
for (int iii=0; iii<MAX_LIGHTS; iii++) {
if (iii<lights.size()) {
super.loadVector(location_lightPosition[iii], lights.get(iii).getPosition());
super.loadVector(location_lightColour[iii], lights.get(iii).getColour());
super.loadVector(location_lightAttenuation[iii], lights.get(iii).getAttenuation());
} else {
super.loadVector(location_lightPosition[iii], new Vector3f(0,0,0));
super.loadVector(location_lightColour[iii], new Vector3f(0,0,0));
super.loadVector(location_lightAttenuation[iii], new Vector3f(1,0,0));
}
}
}
public void loadProjectionMatrix(Matrix4f projection) {

View File

@ -2,7 +2,7 @@
in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toLightVector[4];
in vec3 toCameraVector;
// FOW: Fog Of War result calculation
in float visibility;
@ -10,7 +10,8 @@ in float visibility;
out vec4 out_Color;
uniform sampler2D textureSampler;
uniform vec3 lightColour;
uniform vec3 lightColour[4];
uniform vec3 lightAttenuation[4];
uniform float reflectivity;
uniform float shineDamper;
uniform vec3 skyColor;
@ -19,21 +20,27 @@ uniform vec3 skyColor;
void main(void) {
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal, unitLightVector);
// the 0.2 represent the anbiant lightning ==> maybe set an uniform for this
float brightness = max(nDot1, 0.2);
vec3 diffuse = brightness * lightColour;
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;
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i=0;i<4;i++) {
float distance = length(toLightVector[i]);
float attenuationFactor = lightAttenuation[i].x + (lightAttenuation[i].y * distance) + (lightAttenuation[i].z * distance * distance);
vec3 unitLightVector = normalize(toLightVector[i]);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
specularFactor = max(specularFactor, 0.0);
float damperFactor = pow(specularFactor, shineDamper);
vec3 diffuse = (brightness * lightColour[i]) / attenuationFactor;;
vec3 finalSpecular = (damperFactor * reflectivity * lightColour[i]) / attenuationFactor;;
totalDiffuse = totalDiffuse + diffuse;
totalSpecular = totalSpecular + finalSpecular;
}
// the 0.2 represent the ambiant lightning ==> maybe set an uniform for this
totalDiffuse = max (totalDiffuse, 0.2);
// disable transparency elements in the texture ...
// Can be set at the start of the shader ...
@ -42,7 +49,7 @@ void main(void) {
discard;
}
out_Color = vec4(diffuse,1.0) * textureColour + vec4(finalSpecular, 1.0);
out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular, 1.0);
out_Color = mix(vec4(skyColor,1.0), out_Color, visibility);
}

View File

@ -2,7 +2,7 @@
in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toLightVector[4];
in vec3 toCameraVector;
// FOW: Fog Of War result calculation
in float visibility;
@ -15,7 +15,8 @@ uniform sampler2D gTexture;
uniform sampler2D bTexture;
uniform sampler2D blendMap;
uniform vec3 lightColour;
uniform vec3 lightColour[4];
uniform vec3 lightAttenuation[4];
uniform float reflectivity;
uniform float shineDamper;
uniform vec3 skyColor;
@ -35,23 +36,33 @@ void main(void) {
vec4 totalColour = backgroundTextureColour + rTextureColour + gTextureColour + bTextureColour;
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal, unitLightVector);
// the 0.2 represent the anbiant lightning ==> maybe set an uniform for this
float brightness = max(nDot1, 0.2);
vec3 diffuse = brightness * lightColour;
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i=0;i<4;i++) {
float distance = length(toLightVector[i]);
float attenuationFactor = lightAttenuation[i].x + (lightAttenuation[i].y * distance) + (lightAttenuation[i].z * distance * distance);
vec3 unitLightVector = normalize(toLightVector[i]);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.0);
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 diffuse = (brightness * lightColour[i]) / attenuationFactor;
vec3 finalSpecular = (damperFactor * reflectivity * lightColour[i]) / attenuationFactor;
totalDiffuse = totalDiffuse + diffuse;
totalSpecular = totalSpecular + finalSpecular;
}
// the 0.2 represent the ambiant lightning ==> maybe set an uniform for this
totalDiffuse = max (totalDiffuse, 0.2);
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) * totalColour + vec4(finalSpecular, 1.0);
out_Color = vec4(totalDiffuse,1.0) * totalColour + vec4(totalSpecular, 1.0);
out_Color = mix(vec4(skyColor,1.0), out_Color, visibility);
}

View File

@ -6,7 +6,7 @@ in vec3 normal;
out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toLightVector[4];
out vec3 toCameraVector;
// FOW: Fog Of War result calculation
out float visibility;
@ -14,7 +14,7 @@ out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;
uniform vec3 lightPosition[4];
const float density = 0.007;
const float gradient = 2.5;
@ -26,7 +26,9 @@ void main(void) {
pass_textureCoordinates = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
for(int i=0;i<4;i++) {
toLightVector[i] = lightPosition[i] - worldPosition.xyz;
}
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
float distance = length(positionRelativeToCam.xyz);

View File

@ -6,7 +6,7 @@ in vec3 normal;
out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toLightVector[4];
out vec3 toCameraVector;
// FOW: Fog Of War result calculation
out float visibility;
@ -14,7 +14,7 @@ out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;
uniform vec3 lightPosition[4];
uniform float useFakeLighting;
@ -36,7 +36,9 @@ void main(void) {
actualNormal = vec3(0.0, 1.0, 0.0);
}
surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
for(int i=0;i<4;i++) {
toLightVector[i] = lightPosition[i] - worldPosition.xyz;
}
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
float distance = length(positionRelativeToCam.xyz);

View File

@ -38,4 +38,11 @@ public class Maths {
matrix.translate(new Vector3f(-camarePos.x,-camarePos.y,-camarePos.z));
return matrix;
}
public static Matrix4f createTransformationMatrix(Vector2f translation, Vector2f scale) {
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
matrix.translate(new Vector3f(translation.x, translation.y, 0));
matrix.scale(new Vector3f(scale.x, scale.y, 1f));
return matrix;
}
}