93 lines
2.2 KiB
Java
93 lines
2.2 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 vec2 uv;
|
|
layout(location = 2) in vec3 normal;
|
|
|
|
uniform mat4 projectionMatrix;
|
|
uniform mat4 viewMatrix;
|
|
uniform mat4 modelTransform;
|
|
uniform vec3 lightPosition = vec3(0, 1, 0);
|
|
|
|
out vec3 toLightVector;
|
|
out vec3 surfaceNormal;
|
|
|
|
void main(void){
|
|
vec4 worldPos = modelTransform * vec4(position, 1.0);
|
|
|
|
toLightVector = lightPosition - worldPos.xyz;
|
|
surfaceNormal = (modelTransform * vec4(normal, 0.0)).xyz;
|
|
|
|
gl_Position = projectionMatrix * viewMatrix * worldPos;
|
|
}
|
|
""";
|
|
|
|
private static String fragmentShader = """
|
|
#version 330
|
|
|
|
in vec3 toLightVector;
|
|
in vec3 surfaceNormal;
|
|
|
|
uniform vec3 modelColor = vec3(1, 1, 1);
|
|
|
|
out vec4 out_color;
|
|
|
|
void main(void){
|
|
vec3 unitNormal = normalize(surfaceNormal);
|
|
vec3 unitLightVector = normalize(toLightVector);
|
|
|
|
float diffuse = max(0.5, dot(unitNormal, unitLightVector));
|
|
|
|
float brightness = diffuse;
|
|
|
|
out_color = vec4(modelColor, 1.0) * brightness;
|
|
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);
|
|
}
|
|
}
|