better light
This commit is contained in:
@@ -22,8 +22,8 @@ public class Renderer {
|
|||||||
private VertexArray boardVao;
|
private VertexArray boardVao;
|
||||||
private final PieceModel models;
|
private final PieceModel models;
|
||||||
|
|
||||||
private static final Vector3f BLACK = new Vector3f(0.1f, 0.1f, 0.1f);
|
private static final Vector3f BLACK = new Vector3f(0.3f, 0.3f, 0.3f);
|
||||||
private static final Vector3f WHITE = new Vector3f(0.7f, 0.7f, 0.7f);
|
private static final Vector3f WHITE = new Vector3f(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
public Renderer() {
|
public Renderer() {
|
||||||
this.boardShader = new BoardShader();
|
this.boardShader = new BoardShader();
|
||||||
|
|||||||
@@ -12,16 +12,26 @@ public class BoardShader extends ShaderProgram {
|
|||||||
|
|
||||||
uniform mat4 viewMatrix;
|
uniform mat4 viewMatrix;
|
||||||
uniform mat4 projectionMatrix;
|
uniform mat4 projectionMatrix;
|
||||||
uniform vec3 lightPosition;
|
uniform vec3 lightPosition = vec3(0, 10, 0);
|
||||||
|
|
||||||
flat out vec3 pass_color;
|
flat out vec3 pass_color;
|
||||||
|
|
||||||
out vec3 toLightVector;
|
out vec3 toLightVector;
|
||||||
|
out vec3 toCameraVector;
|
||||||
|
out vec3 surfaceNormal;
|
||||||
|
|
||||||
void main(void){
|
void main(void){
|
||||||
|
const vec4 normal = vec4(0.0, 1.0, 0.0, 1.0);
|
||||||
|
|
||||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||||
|
|
||||||
|
vec3 camPos = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
|
||||||
|
|
||||||
toLightVector = lightPosition - position;
|
toLightVector = lightPosition - position;
|
||||||
|
|
||||||
|
toCameraVector = camPos - position;
|
||||||
|
surfaceNormal = (normal).xyz;
|
||||||
|
|
||||||
pass_color = color;
|
pass_color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,6 +43,8 @@ public class BoardShader extends ShaderProgram {
|
|||||||
flat in vec3 pass_color;
|
flat in vec3 pass_color;
|
||||||
|
|
||||||
in vec3 toLightVector;
|
in vec3 toLightVector;
|
||||||
|
in vec3 toCameraVector;
|
||||||
|
in vec3 surfaceNormal;
|
||||||
|
|
||||||
out vec4 out_color;
|
out vec4 out_color;
|
||||||
|
|
||||||
@@ -42,18 +54,22 @@ public class BoardShader extends ShaderProgram {
|
|||||||
|
|
||||||
float lightDistance = length(toLightVector);
|
float lightDistance = length(toLightVector);
|
||||||
|
|
||||||
const vec3 attenuation = vec3(0.3, 0.03, 0);
|
const vec3 attenuation = vec3(0.2, 0.1, 0.0);
|
||||||
float attenuationFactor = attenuation.x + attenuation.y * lightDistance + attenuation.z * lightDistance * lightDistance;
|
float attenuationFactor = attenuation.x + attenuation.y * lightDistance + attenuation.z * lightDistance * lightDistance;
|
||||||
|
|
||||||
vec3 unitNormal = vec3(0, 1, 0);
|
vec3 unitNormal = normalize(surfaceNormal);
|
||||||
vec3 unitLightVector = normalize(toLightVector);
|
vec3 unitLightVector = normalize(toLightVector);
|
||||||
|
vec3 unitCamVector = normalize(toCameraVector);
|
||||||
|
|
||||||
vec3 lightDirection = -unitLightVector;
|
vec3 lightDirection = -unitLightVector;
|
||||||
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
|
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
|
||||||
|
|
||||||
float diffuse = max(0.2, dot(unitNormal, unitLightVector));
|
float diffuse = max(0.2, dot(unitNormal, unitLightVector));
|
||||||
|
|
||||||
float brightness = diffuse / attenuationFactor;
|
float specularFactor = max(0.0, dot(reflectedLightDirection, unitCamVector));
|
||||||
|
float specular = pow(specularFactor, shineDamper) * reflectivity;
|
||||||
|
|
||||||
|
float brightness = (diffuse + specular) / attenuationFactor;
|
||||||
|
|
||||||
out_color = brightness * vec4(pass_color, 1.0);
|
out_color = brightness * vec4(pass_color, 1.0);
|
||||||
out_color.w = 1.0;
|
out_color.w = 1.0;
|
||||||
|
|||||||
@@ -17,43 +17,68 @@ public class PieceShader extends ShaderProgram {
|
|||||||
uniform mat4 projectionMatrix;
|
uniform mat4 projectionMatrix;
|
||||||
uniform mat4 viewMatrix;
|
uniform mat4 viewMatrix;
|
||||||
uniform mat4 modelTransform;
|
uniform mat4 modelTransform;
|
||||||
uniform vec3 lightPosition = vec3(0, 1, 0);
|
uniform vec3 lightPosition = vec3(0, 10, 0);
|
||||||
|
|
||||||
out vec3 toLightVector;
|
out vec3 toLightVector;
|
||||||
|
out vec3 toCameraVector;
|
||||||
out vec3 surfaceNormal;
|
out vec3 surfaceNormal;
|
||||||
|
|
||||||
void main(void){
|
void main(void){
|
||||||
vec4 worldPos = modelTransform * vec4(position, 1.0);
|
|
||||||
|
|
||||||
toLightVector = lightPosition - worldPos.xyz;
|
vec4 modelPos = modelTransform * vec4(position, 1.0);
|
||||||
surfaceNormal = (modelTransform * vec4(normal, 0.0)).xyz;
|
vec4 globalNormal = modelTransform * vec4(normal, 1.0);
|
||||||
|
|
||||||
gl_Position = projectionMatrix * viewMatrix * worldPos;
|
gl_Position = projectionMatrix * viewMatrix * modelPos;
|
||||||
|
|
||||||
|
vec3 camPos = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
|
||||||
|
|
||||||
|
toLightVector = lightPosition - modelPos.xyz;
|
||||||
|
|
||||||
|
toCameraVector = camPos - position;
|
||||||
|
surfaceNormal = normalize(globalNormal.xyz);
|
||||||
}
|
}
|
||||||
|
|
||||||
""";
|
""";
|
||||||
|
|
||||||
private static String fragmentShader = """
|
private static String fragmentShader = """
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
in vec3 toLightVector;
|
in vec3 toLightVector;
|
||||||
|
in vec3 toCameraVector;
|
||||||
in vec3 surfaceNormal;
|
in vec3 surfaceNormal;
|
||||||
|
|
||||||
uniform vec3 modelColor = vec3(1, 1, 1);
|
uniform vec3 modelColor;
|
||||||
|
|
||||||
out vec4 out_color;
|
out vec4 out_color;
|
||||||
|
|
||||||
void main(void){
|
void main(void){
|
||||||
|
const float shineDamper = 10.0;
|
||||||
|
const float reflectivity = 1.0;
|
||||||
|
|
||||||
|
float lightDistance = length(toLightVector);
|
||||||
|
|
||||||
|
const vec3 attenuation = vec3(0.2, 0.1, 0.0);
|
||||||
|
float attenuationFactor = attenuation.x + attenuation.y * lightDistance + attenuation.z * lightDistance * lightDistance;
|
||||||
|
|
||||||
vec3 unitNormal = normalize(surfaceNormal);
|
vec3 unitNormal = normalize(surfaceNormal);
|
||||||
vec3 unitLightVector = normalize(toLightVector);
|
vec3 unitLightVector = normalize(toLightVector);
|
||||||
|
vec3 unitCamVector = normalize(toCameraVector);
|
||||||
|
|
||||||
float diffuse = max(0.5, dot(unitNormal, unitLightVector));
|
vec3 lightDirection = -unitLightVector;
|
||||||
|
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
|
||||||
|
|
||||||
float brightness = diffuse;
|
float diffuse = max(0.2, dot(unitNormal, unitLightVector));
|
||||||
|
|
||||||
out_color = vec4(modelColor, 1.0) * brightness;
|
float specularFactor = max(0.0, dot(reflectedLightDirection, unitCamVector));
|
||||||
|
float specular = pow(specularFactor, shineDamper) * reflectivity;
|
||||||
|
|
||||||
|
float brightness = (diffuse + specular) / attenuationFactor;
|
||||||
|
|
||||||
|
out_color = brightness * vec4(modelColor, 1.0);
|
||||||
out_color.w = 1.0;
|
out_color.w = 1.0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
""";
|
""";
|
||||||
|
|
||||||
private int location_ProjectionMatrix = 0;
|
private int location_ProjectionMatrix = 0;
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ public abstract class ShaderProgram {
|
|||||||
|
|
||||||
if (compileSuccesful.get() != 1) {
|
if (compileSuccesful.get() != 1) {
|
||||||
System.out.println("Shader did not compile !");
|
System.out.println("Shader did not compile !");
|
||||||
|
System.err.println(GL30.glGetShaderInfoLog(shaderId));
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return shaderId;
|
return shaderId;
|
||||||
@@ -68,7 +68,7 @@ public abstract class ShaderProgram {
|
|||||||
protected int GetUniformLocation(String uniformName) {
|
protected int GetUniformLocation(String uniformName) {
|
||||||
int location = GL30.glGetUniformLocation(programId, uniformName);
|
int location = GL30.glGetUniformLocation(programId, uniformName);
|
||||||
if (location == -1) {
|
if (location == -1) {
|
||||||
System.out.println("Uniform value not found !");
|
System.out.println("Uniform value \"" + uniformName + "\" not found !");
|
||||||
}
|
}
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user