adjust light functionality,

bugs:
- background cannot be changed during runtime
This commit is contained in:
Anakin
2017-01-18 17:01:43 +01:00
parent a521dfc292
commit 4c177f2ddc
9 changed files with 75 additions and 35 deletions

View File

@@ -15,40 +15,41 @@ uniform struct Light {
uniform bool b_transparent;
uniform bool b_light;
varying vec2 v_texcoord;
varying vec3 v_position;
varying vec3 v_normal;
varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal;
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);
}
vec3 diffuse;
// get fragment color from texture
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
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;
// pass the data to ogl
gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a);
if(b_light)
{
// calculate normals in worldspace
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
//get the surface - light vector (cause this is a point light)
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
// calculate the brightness depending on the angle
float diffuseCoefficient = max(0.0, dot(normalWorld, surfaceToLight));
// result diffuse color
diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
}
else
{
diffuse = surfaceColor.rgb;
}
// put all together
gl_FragColor = vec4(diffuse, surfaceColor.a);
}