carpy-breakout/pkg/glshader/fragment_shader.go

100 lines
2.0 KiB
Go

package glshader
var FragmentShaderSource string = `
#version 330
precision mediump float;
// Inputs
smooth in vec3 fragNormal;
in vec2 fragTexCoord;
smooth in vec3 lightDir;
// Outputs
out vec4 outputColor;
struct Material {
sampler2D textureDiffuse;
sampler2D textureSpecular;
float shininess;
vec4 color;
int textureOn; // if textureOn == 0, then just color is used
};
struct Light {
vec3 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
};
uniform Light light;
uniform Material material;
uniform int lightsOn;
vec4 GetTextureDiffuse();
vec4 GetTextureSpecular();
vec4 LightColor(Light light, vec3 normal, vec3 viewDir);
void main() {
vec3 normal = normalize(fragNormal);
vec4 lightColor;
if (lightsOn != 0) {
lightColor = LightColor(light, normal, lightDir);
} else {
lightColor = material.color;
}
if (lightColor.a < 0.1f) {
discard;
}
outputColor = lightColor;
}
vec4 GetTextureDiffuse() {
vec4 t = material.color;
if (material.textureOn != 0) {
t = t * vec4(texture(material.textureDiffuse, fragTexCoord));
}
return t;
}
vec4 GetTextureSpecular() {
vec4 t = material.color;
if (material.textureOn != 0) {
t = t * vec4(texture(material.textureSpecular, fragTexCoord));
}
return t;
}
vec4 LightColor(Light light, vec3 normal, vec3 lightDir)
{
lightDir = normalize(lightDir);
normal = normalize(normal);
// Diffuse
float diffuseAmount = max(dot(normal, lightDir), 0.0);
// Specular
vec3 reflectDir = normalize(reflect(-lightDir, normal));
float specularAmount = max(dot(normal, reflectDir), 0.0);
if (diffuseAmount != 0) {
specularAmount = pow(specularAmount, material.shininess);
}
// Textures
vec4 tDiffuse = GetTextureDiffuse();
vec4 tSpecular = GetTextureSpecular();
// Multiply to get each value, then add to get the final result
vec4 a = light.ambient * tDiffuse;
vec4 d = light.diffuse * diffuseAmount * tDiffuse;
vec4 s = light.specular * specularAmount * tSpecular;
return (a + d + s);
}
`