[DEV] tutorial 26 'light point' implementation
This commit is contained in:
parent
6309551af2
commit
d3dce20747
BIN
res/health.png
Normal file
BIN
res/health.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.3 KiB |
11
res/lamp.mtl
Normal file
11
res/lamp.mtl
Normal 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
1006
res/lamp.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
res/lamp.png
Normal file
BIN
res/lamp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
1228
res/pine.obj
Normal file
1228
res/pine.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
res/pine.png
Normal file
BIN
res/pine.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 474 KiB |
@ -50,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),
|
||||
@ -73,10 +75,33 @@ public class MainGameLoop {
|
||||
fernModel.getTexture().setHasTransparency(true);
|
||||
fernModel.getTexture().setNumberOfRows(2);
|
||||
|
||||
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(1,1,1)));
|
||||
lights.add(new Light(new Vector3f(-200,10,-200), new Vector3f(10,0,0)));
|
||||
lights.add(new Light(new Vector3f(200,10,200), new Vector3f(0,0,10)));
|
||||
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"));
|
||||
@ -89,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);
|
||||
@ -100,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);
|
||||
@ -113,13 +144,13 @@ 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("socuwan"), new Vector2f(0.5f, 0.5f), new Vector2f(0.25f, 0.25f));
|
||||
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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ public class StaticShader extends ShaderProgram {
|
||||
private int location_viewMatrix;
|
||||
private int location_lightPosition[];
|
||||
private int location_lightColour[];
|
||||
private int location_lightAttenuation[];
|
||||
private int location_reflectivity;
|
||||
private int location_shineDamper;
|
||||
private int location_useFakeLighting;
|
||||
@ -52,9 +53,11 @@ public class StaticShader extends ShaderProgram {
|
||||
|
||||
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+ "]");
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,9 +91,11 @@ public class StaticShader extends ShaderProgram {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ public class TerrainShader extends ShaderProgram {
|
||||
private int location_viewMatrix;
|
||||
private int location_lightPosition[];
|
||||
private int location_lightColour[];
|
||||
private int location_lightAttenuation[];
|
||||
private int location_reflectivity;
|
||||
private int location_shineDamper;
|
||||
private int location_skyColor;
|
||||
@ -54,9 +55,11 @@ public class TerrainShader extends ShaderProgram {
|
||||
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+ "]");
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,9 +89,11 @@ public class TerrainShader extends ShaderProgram {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ out vec4 out_Color;
|
||||
|
||||
uniform sampler2D textureSampler;
|
||||
uniform vec3 lightColour[4];
|
||||
uniform vec3 lightAttenuation[4];
|
||||
uniform float reflectivity;
|
||||
uniform float shineDamper;
|
||||
uniform vec3 skyColor;
|
||||
@ -23,6 +24,8 @@ void main(void) {
|
||||
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);
|
||||
@ -31,8 +34,8 @@ void main(void) {
|
||||
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
|
||||
specularFactor = max(specularFactor, 0.0);
|
||||
float damperFactor = pow(specularFactor, shineDamper);
|
||||
vec3 diffuse = brightness * lightColour[i];
|
||||
vec3 finalSpecular = damperFactor * reflectivity * lightColour[i];
|
||||
vec3 diffuse = (brightness * lightColour[i]) / attenuationFactor;;
|
||||
vec3 finalSpecular = (damperFactor * reflectivity * lightColour[i]) / attenuationFactor;;
|
||||
totalDiffuse = totalDiffuse + diffuse;
|
||||
totalSpecular = totalSpecular + finalSpecular;
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ uniform sampler2D bTexture;
|
||||
uniform sampler2D blendMap;
|
||||
|
||||
uniform vec3 lightColour[4];
|
||||
uniform vec3 lightAttenuation[4];
|
||||
uniform float reflectivity;
|
||||
uniform float shineDamper;
|
||||
uniform vec3 skyColor;
|
||||
@ -39,6 +40,8 @@ void main(void) {
|
||||
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);
|
||||
@ -51,8 +54,8 @@ void main(void) {
|
||||
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
|
||||
specularFactor = max(specularFactor, 0.0);
|
||||
float damperFactor = pow(specularFactor, shineDamper);
|
||||
vec3 diffuse = brightness * lightColour[i];
|
||||
vec3 finalSpecular = damperFactor * reflectivity * lightColour[i];
|
||||
vec3 diffuse = (brightness * lightColour[i]) / attenuationFactor;
|
||||
vec3 finalSpecular = (damperFactor * reflectivity * lightColour[i]) / attenuationFactor;
|
||||
totalDiffuse = totalDiffuse + diffuse;
|
||||
totalSpecular = totalSpecular + finalSpecular;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user