SWBF2-Classic-Msh-Viewer/QtMeshViewer/Resources/fshader.glsl

75 lines
2.1 KiB
Plaintext
Raw Normal View History

#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
uniform mat3 n_matrix;
uniform vec3 cameraPosition;
uniform sampler2D texture;
uniform float materialShininess;
uniform vec3 materialSpecularColor;
uniform bool b_transparent;
uniform bool b_light;
uniform struct Light {
vec3 position;
vec3 intensities;
float attenuationFactor;
float ambientCoefficient;
} light;
varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal;
void main()
{
if(b_light)
{
// some values
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
if(!b_transparent)
surfaceColor.a = 1.0f;
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
// ambient
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
// diffuse
float diffuseCoefficient = max(0.0, dot(normalWorld, surfaceToLight));
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
// specular
float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalWorld))), materialShininess);
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;
// attenuation
float distanceToLight = length(light.position - v_surfacePosition);
float attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
// linear color before gamma correction)
vec3 linearColor = ambient + attenuation * (diffuse + specular);
// final color after gama correction
vec3 gamma = vec3(1.0/2.2);
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
//gl_FragColor = vec4(linearColor, surfaceColor.a);
}
else
{
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
if(!b_transparent)
surfaceColor.a = 1.0f;
gl_FragColor = surfaceColor;
}
}