1st installment of tech ENABLE_ENVIRONMENT_MAP

This commit is contained in:
enricoturri1966 2020-05-28 15:27:29 +02:00
parent 4b6bcd7028
commit c63e03c367
11 changed files with 93 additions and 6 deletions

View file

@ -17,6 +17,9 @@ struct SlopeDetection
uniform vec4 uniform_color;
uniform SlopeDetection slope;
uniform sampler2D environment_tex;
uniform bool use_environment_tex;
varying vec3 clipping_planes_dots;
// x = tainted, y = specular;
@ -26,6 +29,7 @@ varying vec3 delta_box_min;
varying vec3 delta_box_max;
varying float world_normal_z;
varying vec3 eye_normal;
vec3 slope_color()
{
@ -40,5 +44,8 @@ void main()
vec3 color = slope.actived ? slope_color() : uniform_color.rgb;
// if the fragment is outside the print volume -> use darker color
color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(color, ZERO, 0.3333) : color;
gl_FragColor = vec4(vec3(intensity.y, intensity.y, intensity.y) + color * intensity.x, uniform_color.a);
if (use_environment_tex)
gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, uniform_color.a);
else
gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, uniform_color.a);
}

View file

@ -51,22 +51,23 @@ varying vec3 delta_box_max;
varying vec3 clipping_planes_dots;
varying float world_normal_z;
varying vec3 eye_normal;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
eye_normal = normalize(gl_NormalMatrix * gl_Normal);
// Compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex.
// Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range.
float NdotL = max(dot(normal, LIGHT_TOP_DIR), 0.0);
float NdotL = max(dot(eye_normal, LIGHT_TOP_DIR), 0.0);
intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
vec3 position = (gl_ModelViewMatrix * gl_Vertex).xyz;
intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position), reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS);
intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position), reflect(-LIGHT_TOP_DIR, eye_normal)), 0.0), LIGHT_TOP_SHININESS);
// Perform the same lighting calculation for the 2nd light source (no specular applied).
NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0);
NdotL = max(dot(eye_normal, LIGHT_FRONT_DIR), 0.0);
intensity.x += NdotL * LIGHT_FRONT_DIFFUSE;
// compute deltas for out of print volume detection (world coordinates)