117 lines
3.0 KiB
Java
117 lines
3.0 KiB
Java
package chess.view.DDDrender.shader;
|
|
|
|
import org.joml.Matrix4f;
|
|
import org.joml.Vector3f;
|
|
|
|
import chess.view.DDDrender.Camera;
|
|
|
|
public class PieceShader extends ShaderProgram {
|
|
|
|
private static String vertexShader = """
|
|
#version 330
|
|
|
|
layout(location = 0) in vec3 position;
|
|
layout(location = 1) in vec3 normal;
|
|
|
|
uniform mat4 projectionMatrix;
|
|
uniform mat4 viewMatrix;
|
|
uniform mat4 modelTransform;
|
|
uniform vec3 lightPosition = vec3(0, 10, 0);
|
|
|
|
out vec3 toLightVector;
|
|
out vec3 toCameraVector;
|
|
out vec3 surfaceNormal;
|
|
|
|
void main(void){
|
|
|
|
vec4 modelPos = modelTransform * vec4(position, 1.0);
|
|
vec4 globalNormal = modelTransform * vec4(normal, 1.0);
|
|
|
|
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 = """
|
|
#version 330
|
|
|
|
in vec3 toLightVector;
|
|
in vec3 toCameraVector;
|
|
in vec3 surfaceNormal;
|
|
|
|
uniform vec3 modelColor;
|
|
|
|
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(modelColor, 1.0);
|
|
out_color.w = 1.0;
|
|
|
|
}
|
|
|
|
""";
|
|
|
|
private int location_ProjectionMatrix = 0;
|
|
private int location_ViewMatrix = 0;
|
|
private int location_ModelTransform = 0;
|
|
private int location_ModelColor = 0;
|
|
|
|
public PieceShader() {
|
|
|
|
}
|
|
|
|
public void LoadShader() {
|
|
super.LoadProgram(vertexShader, fragmentShader);
|
|
}
|
|
|
|
@Override
|
|
protected void GetAllUniformLocation() {
|
|
location_ProjectionMatrix = GetUniformLocation("projectionMatrix");
|
|
location_ViewMatrix = GetUniformLocation("viewMatrix");
|
|
location_ModelTransform = GetUniformLocation("modelTransform");
|
|
location_ModelColor = GetUniformLocation("modelColor");
|
|
}
|
|
|
|
public void SetCamMatrix(Camera camera) {
|
|
LoadMat4(location_ProjectionMatrix, camera.getPerspectiveMatrix());
|
|
LoadMat4(location_ViewMatrix, camera.getViewMatrix());
|
|
}
|
|
|
|
public void setModelTransform(Matrix4f mat) {
|
|
LoadMat4(location_ModelTransform, mat);
|
|
}
|
|
|
|
public void setModelColor(Vector3f color) {
|
|
LoadVector(location_ModelColor, color);
|
|
}
|
|
}
|