added specular, ambient light,...

need to fix the texutre gamma correction,
look at the todos
This commit is contained in:
Anakin
2017-01-19 17:57:50 +01:00
parent 4c177f2ddc
commit 9fb3ca03bd
6 changed files with 61 additions and 24 deletions

View File

@@ -5,51 +5,68 @@ 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;
uniform bool b_transparent;
uniform bool b_light;
varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal;
void main()
{
// variables
vec3 diffuse;
// get fragment color from texture
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
// if not transparent, ignore alpha value and set it to 1
if(!b_transparent)
surfaceColor.a = 1.0f;
if(b_light)
{
// calculate normals in worldspace
// some values
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
//get the surface - light vector (cause this is a point light)
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
if(!b_transparent)
surfaceColor.a = 1.0f;
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
// calculate the brightness depending on the angle
// 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;
// result diffuse color
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);
}
else
{
diffuse = surfaceColor.rgb;
}
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
if(!b_transparent)
surfaceColor.a = 1.0f;
// put all together
gl_FragColor = vec4(diffuse, surfaceColor.a);
gl_FragColor = surfaceColor;
}
}