[DEV] tutorial 11 'pixel lightning' implementation
This commit is contained in:
parent
ff26151b39
commit
4c4197cc51
BIN
res/DragonBlender.blend
Normal file
BIN
res/DragonBlender.blend
Normal file
Binary file not shown.
200007
res/dragon.obj
Normal file
200007
res/dragon.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
res/white.png
Normal file
BIN
res/white.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.8 KiB |
@ -5,6 +5,7 @@ import javax.vecmath.Vector3f;
|
||||
|
||||
import entities.Camera;
|
||||
import entities.Entity;
|
||||
import entities.Light;
|
||||
import models.RawModel;
|
||||
import models.TexturedModel;
|
||||
import renderEngine.DisplayManager;
|
||||
@ -40,13 +41,14 @@ public class MainGameLoop {
|
||||
|
||||
|
||||
|
||||
RawModel model = OBJLoader.loadObjModel("stall", loader);
|
||||
RawModel model = OBJLoader.loadObjModel("dragon", loader);
|
||||
|
||||
ModelTexture texture = new ModelTexture(loader.loadTexture("stallTexture"));
|
||||
ModelTexture texture = new ModelTexture(loader.loadTexture("white"));
|
||||
|
||||
TexturedModel staticModel = new TexturedModel(model, texture);
|
||||
|
||||
Entity entity = new Entity(staticModel, new Vector3f(0,0,-20), new Vector3f(0,0,0), 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));
|
||||
|
||||
Camera camera = new Camera();
|
||||
|
||||
@ -59,6 +61,7 @@ public class MainGameLoop {
|
||||
camera.move();
|
||||
renderer.prepare();
|
||||
shader.start();
|
||||
shader.loadLight(light);
|
||||
shader.loadViewMatrix(camera);
|
||||
renderer.render(entity, shader);
|
||||
shader.stop();
|
||||
|
26
src/entities/Light.java
Normal file
26
src/entities/Light.java
Normal file
@ -0,0 +1,26 @@
|
||||
package entities;
|
||||
|
||||
import javax.vecmath.Vector3f;
|
||||
|
||||
public class Light {
|
||||
private Vector3f position;
|
||||
private Vector3f colour;
|
||||
public Light(Vector3f position, Vector3f colour) {
|
||||
super();
|
||||
this.position = position;
|
||||
this.colour = colour;
|
||||
}
|
||||
public Vector3f getPosition() {
|
||||
return position;
|
||||
}
|
||||
public void setPosition(Vector3f position) {
|
||||
this.position = position;
|
||||
}
|
||||
public Vector3f getColour() {
|
||||
return colour;
|
||||
}
|
||||
public void setColour(Vector3f colour) {
|
||||
this.colour = colour;
|
||||
}
|
||||
|
||||
}
|
@ -69,11 +69,12 @@ public class Loader {
|
||||
unbindVAO();
|
||||
return new RawModel(vaoID, indices.length);
|
||||
}
|
||||
public RawModel loadToVAO(float[] positions, float[] textureCoordinates, int[] indices) {
|
||||
public RawModel loadToVAO(float[] positions, float[] textureCoordinates, float[] normals, int[] indices) {
|
||||
int vaoID = createVAO();
|
||||
bindIndicesBuffer(indices);
|
||||
storeDataInAttributeList(0, 3, positions);
|
||||
storeDataInAttributeList(1, 2, textureCoordinates);
|
||||
storeDataInAttributeList(2, 3, normals);
|
||||
unbindVAO();
|
||||
return new RawModel(vaoID, indices.length);
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ public class OBJLoader {
|
||||
for (int iii=0; iii<indices.size(); iii++) {
|
||||
indicesArray[iii] = indices.get(iii);
|
||||
}
|
||||
return loader.loadToVAO(verticesArray, texturesArray, indicesArray);
|
||||
return loader.loadToVAO(verticesArray, texturesArray, normalsArray, indicesArray);
|
||||
}
|
||||
|
||||
private static void processVertex(String[] vertexData, List<Integer> indices,
|
||||
|
@ -47,6 +47,7 @@ public class Renderer {
|
||||
GL30.glBindVertexArray(rawModel.getVaoID());
|
||||
GL20.glEnableVertexAttribArray(0);
|
||||
GL20.glEnableVertexAttribArray(1);
|
||||
GL20.glEnableVertexAttribArray(2);
|
||||
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotation(), entity.getScale());
|
||||
shader.loadTransformationMatrix(transformationMatrix);
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
@ -54,6 +55,7 @@ public class Renderer {
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
GL20.glDisableVertexAttribArray(0);
|
||||
GL20.glDisableVertexAttribArray(1);
|
||||
GL20.glDisableVertexAttribArray(2);
|
||||
GL30.glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package shaders;
|
||||
import javax.vecmath.Matrix4f;
|
||||
|
||||
import entities.Camera;
|
||||
import entities.Light;
|
||||
import toolbox.Maths;
|
||||
|
||||
public class StaticShader extends ShaderProgram {
|
||||
@ -12,6 +13,8 @@ public class StaticShader extends ShaderProgram {
|
||||
private int location_transformationMatrix;
|
||||
private int location_projectionMatrix;
|
||||
private int location_viewMatrix;
|
||||
private int location_lightPosition;
|
||||
private int location_lightColour;
|
||||
|
||||
public StaticShader() {
|
||||
super(VERTEX_FILE, FRAGMENT_FILE);
|
||||
@ -21,6 +24,7 @@ public class StaticShader extends ShaderProgram {
|
||||
protected void bindAttributes() {
|
||||
super.bindAttribute(0, "position");
|
||||
super.bindAttribute(1, "textureCoords");
|
||||
super.bindAttribute(2, "normal");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -28,15 +32,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");
|
||||
}
|
||||
|
||||
public void loadTransformationMatrix(Matrix4f value) {
|
||||
super.loadMatrix(location_transformationMatrix, value);
|
||||
public void loadTransformationMatrix(Matrix4f matrix) {
|
||||
super.loadMatrix(location_transformationMatrix, matrix);
|
||||
}
|
||||
public void loadProjectionMatrix(Matrix4f value) {
|
||||
super.loadMatrix(location_projectionMatrix, value);
|
||||
public void loadLight(Light light) {
|
||||
super.loadVector(location_lightPosition, light.getPosition());
|
||||
super.loadVector(location_lightColour, light.getColour());
|
||||
}
|
||||
public void loadViewMatrix(Camera value) {
|
||||
super.loadMatrix(location_viewMatrix, Maths.createViewMatrix(value));
|
||||
public void loadProjectionMatrix(Matrix4f projection) {
|
||||
super.loadMatrix(location_projectionMatrix, projection);
|
||||
}
|
||||
public void loadViewMatrix(Camera camera) {
|
||||
super.loadMatrix(location_viewMatrix, Maths.createViewMatrix(camera));
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,24 @@
|
||||
#version 400 core
|
||||
|
||||
in vec2 pass_textureCoords;
|
||||
in vec3 surfaceNormal;
|
||||
in vec3 toLightVector;
|
||||
|
||||
out vec4 out_Color;
|
||||
|
||||
uniform sampler2D textureSampler;
|
||||
uniform vec3 lightColour;
|
||||
|
||||
|
||||
void main(void) {
|
||||
out_Color = texture(textureSampler,pass_textureCoords);
|
||||
|
||||
vec3 unitNormal = normalize(surfaceNormal);
|
||||
vec3 unitLightVector = normalize(toLightVector);
|
||||
|
||||
float nDot1 = dot(unitNormal, unitLightVector);
|
||||
float brightness = max(nDot1, 0.0);
|
||||
vec3 diffuse = brightness * lightColour;
|
||||
|
||||
out_Color = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoords);
|
||||
}
|
||||
|
||||
|
@ -2,16 +2,23 @@
|
||||
|
||||
in vec3 position;
|
||||
in vec2 textureCoords;
|
||||
in vec3 normal;
|
||||
|
||||
out vec2 pass_textureCoords;
|
||||
out vec3 surfaceNormal;
|
||||
out vec3 toLightVector;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform vec3 lightPosition;
|
||||
|
||||
void main(void) {
|
||||
|
||||
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
|
||||
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
|
||||
gl_Position = projectionMatrix * viewMatrix * worldPosition;
|
||||
pass_textureCoords = textureCoords;
|
||||
|
||||
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
|
||||
toLightVector = lightPosition - worldPosition.xyz;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user