calculate normal matrix once in cpp (performance),
added bool variable to turn light on/off (still needs button),
This commit is contained in:
@@ -4,10 +4,8 @@ precision mediump int;
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
//uniform mat3 n_matrix;
|
||||
uniform mat3 n_matrix;
|
||||
uniform sampler2D texture;
|
||||
uniform mat4 norm_matrix;
|
||||
uniform mat4 m_matrix;
|
||||
|
||||
uniform struct Light {
|
||||
vec3 position;
|
||||
@@ -15,6 +13,7 @@ uniform struct Light {
|
||||
} light;
|
||||
|
||||
uniform bool b_transparent;
|
||||
uniform bool b_light;
|
||||
|
||||
varying vec2 v_texcoord;
|
||||
varying vec3 v_position;
|
||||
@@ -22,21 +21,34 @@ varying vec3 v_normal;
|
||||
|
||||
void main()
|
||||
{
|
||||
mat3 n_matrix = transpose(inverse(mat3(norm_matrix * m_matrix)));
|
||||
vec3 normal = normalize(n_matrix * v_normal);
|
||||
// variables
|
||||
float brightness;
|
||||
|
||||
vec3 surfaceToLight = light.position - v_position;
|
||||
|
||||
float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
|
||||
brightness = clamp(brightness, 0, 1);
|
||||
|
||||
// Set fragment color from texture
|
||||
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
|
||||
|
||||
if(!b_transparent)
|
||||
if(b_light == true)
|
||||
{
|
||||
surfaceColor.a = 1.0f;
|
||||
// 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user