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

55 lines
1.2 KiB
GLSL

#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
uniform mat3 n_matrix;
uniform sampler2D texture;
uniform struct Light {
vec3 position;
vec3 intensities;
} light;
uniform bool b_transparent;
uniform bool b_light;
varying vec2 v_texcoord;
varying vec3 v_position;
varying vec3 v_normal;
void main()
{
// variables
float brightness;
if(b_light == true)
{
// calculate normals in worldspace
vec3 normal = normalize(n_matrix * v_normal);
//get the surface - light vector (cause this is a candel)
vec3 surfaceToLight = light.position - v_position;
// calculate the brightness depending on the angle
brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
brightness = clamp(brightness, 0, 1);
}
else
{
brightness = 1;
light.intensities = vec3(1,1,1);
}
// get fragment color from texture
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
// if not transparent, ignore alpha value and set it to 1
if(!b_transparent)
surfaceColor.a = 1.0f;
// pass the data to ogl
gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a);
}