103 lines
2.5 KiB
Java
103 lines
2.5 KiB
Java
package chess.view.DDDrender.shader;
|
|
|
|
import chess.view.DDDrender.Camera;
|
|
|
|
public class BoardShader extends ShaderProgram {
|
|
|
|
private static String vertexShader = """
|
|
#version 330
|
|
|
|
layout(location = 0) in vec3 position;
|
|
layout(location = 1) in vec3 color;
|
|
|
|
uniform mat4 viewMatrix;
|
|
uniform mat4 projectionMatrix;
|
|
uniform vec3 lightPosition = vec3(0, 10, 0);
|
|
|
|
flat out vec3 pass_color;
|
|
|
|
out vec3 toLightVector;
|
|
out vec3 toCameraVector;
|
|
out vec3 surfaceNormal;
|
|
|
|
void main(void){
|
|
const vec4 normal = vec4(0.0, 1.0, 0.0, 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;
|
|
|
|
toCameraVector = camPos - position;
|
|
surfaceNormal = (normal).xyz;
|
|
|
|
pass_color = color;
|
|
}
|
|
|
|
""";
|
|
|
|
private static String fragmentShader = """
|
|
#version 330
|
|
|
|
flat in vec3 pass_color;
|
|
|
|
in vec3 toLightVector;
|
|
in vec3 toCameraVector;
|
|
in vec3 surfaceNormal;
|
|
|
|
layout(location = 0) out vec4 out_color;
|
|
|
|
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 unitLightVector = normalize(toLightVector);
|
|
vec3 unitCamVector = normalize(toCameraVector);
|
|
|
|
vec3 lightDirection = -unitLightVector;
|
|
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
|
|
|
|
float diffuse = max(0.2, dot(unitNormal, unitLightVector));
|
|
|
|
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.w = 1.0;
|
|
|
|
}
|
|
|
|
""";
|
|
|
|
private int location_ProjectionMatrix = 0;
|
|
private int location_ViewMatrix = 0;
|
|
|
|
public BoardShader() {
|
|
|
|
}
|
|
|
|
public void LoadShader() {
|
|
super.LoadProgram(vertexShader, fragmentShader);
|
|
}
|
|
|
|
@Override
|
|
protected void GetAllUniformLocation() {
|
|
location_ProjectionMatrix = GetUniformLocation("projectionMatrix");
|
|
location_ViewMatrix = GetUniformLocation("viewMatrix");
|
|
}
|
|
|
|
public void SetCamMatrix(Camera camera) {
|
|
LoadMat4(location_ProjectionMatrix, camera.getPerspectiveMatrix());
|
|
LoadMat4(location_ViewMatrix, camera.getViewMatrix());
|
|
}
|
|
}
|