Per Pixel Lightning

This commit is contained in:
Sebastian 2023-08-11 20:04:01 +02:00
parent 9b5adeab00
commit 666d3e1ea2
8 changed files with 72 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package core.engine;
import core.engine.entity.Camera; import core.engine.entity.Camera;
import core.engine.entity.Entity; import core.engine.entity.Entity;
import core.engine.entity.Light;
import core.engine.model.RawModel; import core.engine.model.RawModel;
import core.engine.model.TexturedModel; import core.engine.model.TexturedModel;
import core.engine.shader.StaticShader; import core.engine.shader.StaticShader;
@ -138,8 +139,9 @@ public class Engine {
}); });
Renderer renderer = new Renderer(shader); Renderer renderer = new Renderer(shader);
RawModel model = OBJLoader.loadOBJModel("stall", loader); Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1));
ModelTexture modelTexture = new ModelTexture(loader.loadTexture("stallTexture")); RawModel model = OBJLoader.loadOBJModel("dragon", loader);
ModelTexture modelTexture = new ModelTexture(loader.loadTexture("white"));
TexturedModel texturedModel = new TexturedModel(model, modelTexture); TexturedModel texturedModel = new TexturedModel(model, modelTexture);
Entity entity = new Entity(texturedModel, new Vector3f(0,0,-50), 0,0,0,1); Entity entity = new Entity(texturedModel, new Vector3f(0,0,-50), 0,0,0,1);
// Run the rendering loop until the user has attempted to close // Run the rendering loop until the user has attempted to close
@ -148,6 +150,7 @@ public class Engine {
entity.increaseRotation(0,1,0); entity.increaseRotation(0,1,0);
renderer.prepare(); renderer.prepare();
shader.start(); shader.start();
shader.loadLight(light);
shader.loadViewMatrix(camera); shader.loadViewMatrix(camera);
renderer.render(entity, shader); renderer.render(entity, shader);
shader.stop(); shader.stop();

View File

@ -19,11 +19,12 @@ public class Loader {
private List<Integer> vbos = new ArrayList<>(); private List<Integer> vbos = new ArrayList<>();
private List<Integer> textureIDs = new ArrayList<>(); private List<Integer> textureIDs = new ArrayList<>();
public RawModel loadToVAO(float[] positions, float[] textureCoords, int[] indices) { public RawModel loadToVAO(float[] positions, float[] textureCoords, float[] normals, int[] indices) {
int vaoID = createVAO(); int vaoID = createVAO();
bindIndicesBuffer(indices); bindIndicesBuffer(indices);
storeDataInAttributeList(0, 3,positions); storeDataInAttributeList(0, 3,positions);
storeDataInAttributeList(1, 2, textureCoords); storeDataInAttributeList(1, 2, textureCoords);
storeDataInAttributeList(2, 3, normals);
unbindVAO(); unbindVAO();
return new RawModel(vaoID, indices.length); return new RawModel(vaoID, indices.length);
} }

View File

@ -94,7 +94,7 @@ public class OBJLoader {
indicesArray[i] = indices.get(i); indicesArray[i] = indices.get(i);
} }
return loader.loadToVAO(verticesArray, texturesArray, indicesArray); return loader.loadToVAO(verticesArray, texturesArray, normalsArray, indicesArray);
} else { } else {
return null; return null;
} }

View File

@ -38,6 +38,7 @@ public class Renderer {
GL30.glBindVertexArray(rawModel.getVaoID()); GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(entity.getPosition(), entity.getRotX(), Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
entity.getRotY(), entity.getRotZ(), entity.getScale()); entity.getRotY(), entity.getRotZ(), entity.getScale());
shader.loadTransformationMatrix(transformationMatrix); shader.loadTransformationMatrix(transformationMatrix);
@ -46,6 +47,7 @@ public class Renderer {
GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0); GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1); GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0); GL30.glBindVertexArray(0);
} }

View File

@ -0,0 +1,30 @@
package core.engine.entity;
import utils.vectors.Vector3f;
public class Light {
private Vector3f position;
private Vector3f color;
public Light(Vector3f position, Vector3f color) {
this.position = position;
this.color = color;
}
public Vector3f getPosition() {
return position;
}
public void setPosition(Vector3f position) {
this.position = position;
}
public Vector3f getColor() {
return color;
}
public void setColor(Vector3f color) {
this.color = color;
}
}

View File

@ -1,6 +1,7 @@
package core.engine.shader; package core.engine.shader;
import core.engine.entity.Camera; import core.engine.entity.Camera;
import core.engine.entity.Light;
import utils.MatrixGraphicUtils; import utils.MatrixGraphicUtils;
import utils.vectors.Matrix4f; import utils.vectors.Matrix4f;
@ -12,6 +13,8 @@ public class StaticShader extends ShaderProgram{
private int location_transformationMatrix; private int location_transformationMatrix;
private int location_projectionMatrix; private int location_projectionMatrix;
private int location_viewMatrix; private int location_viewMatrix;
private int location_lightPosition;
private int location_lightColor;
public StaticShader() { public StaticShader() {
super(VERTEX_FILE, FRAGMENT_FILE); super(VERTEX_FILE, FRAGMENT_FILE);
} }
@ -21,12 +24,15 @@ public class StaticShader extends ShaderProgram{
this.location_transformationMatrix = super.getUniformLocation("transformationMatrix"); this.location_transformationMatrix = super.getUniformLocation("transformationMatrix");
this.location_projectionMatrix = super.getUniformLocation("projectionMatrix"); this.location_projectionMatrix = super.getUniformLocation("projectionMatrix");
this.location_viewMatrix = super.getUniformLocation("viewMatrix"); this.location_viewMatrix = super.getUniformLocation("viewMatrix");
this.location_lightColor = super.getUniformLocation("lightColor");
this.location_lightPosition = super.getUniformLocation("lightPosition");
} }
@Override @Override
protected void bindAttributes() { protected void bindAttributes() {
super.bindAttribute(0, "position"); super.bindAttribute(0, "position");
super.bindAttribute(1, "textureCoords"); super.bindAttribute(1, "textureCoords");
super.bindAttribute(2, "normal");
} }
public void loadTransformationMatrix(Matrix4f matrix) { public void loadTransformationMatrix(Matrix4f matrix) {
@ -41,4 +47,9 @@ public class StaticShader extends ShaderProgram{
Matrix4f viewMatrix = MatrixGraphicUtils.createViewMatrix(camera); Matrix4f viewMatrix = MatrixGraphicUtils.createViewMatrix(camera);
super.loadMatrix(location_viewMatrix, viewMatrix); super.loadMatrix(location_viewMatrix, viewMatrix);
} }
public void loadLight(Light light) {
super.loadVector(location_lightPosition, light.getPosition());
super.loadVector(location_lightColor, light.getColor());
}
} }

View File

@ -1,11 +1,21 @@
#version 400 core #version 400 core
in vec2 pass_textureCoords; in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
out vec4 out_Color; out vec4 out_Color;
uniform sampler2D textureSampler; uniform sampler2D textureSampler;
uniform vec3 lightColor;
void main(void) { void main(void) {
out_Color = texture(textureSampler, pass_textureCoords); vec3 unitNormal = normalize(surfaceNormal);
vec3 unitToLightVector = normalize(toLightVector);
float nDotl = dot(unitNormal, unitToLightVector);
float brightness = max(nDotl, 0.0);
vec3 diffusde = brightness * lightColor;
out_Color = vec4(diffusde, 1.0) * texture(textureSampler, pass_textureCoords);
} }

View File

@ -2,14 +2,23 @@
in vec3 position; in vec3 position;
in vec2 textureCoords; in vec2 textureCoords;
in vec3 normal;
uniform mat4 transformationMatrix; uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix; uniform mat4 projectionMatrix;
uniform mat4 viewMatrix; uniform mat4 viewMatrix;
uniform vec3 lightPosition;
out vec2 pass_textureCoords; out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
void main(void) { 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; pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
} }