Graphics Reference
In-Depth Information
// the reflect and refract functions assume the normal is
// pointing toward the eye, that is, normal.z > 0.
// if that's not true, make it true:
if( normal.z < 0. )
{
normal = -normal;
}
vec3 ReflectVector = reflect(normalize(vECposition),normal);
vec3 RefractVector = refract(normalize(vECposition),
normal,uIofR);
vec4 reflectcolor = texture( uTexUnit, ReflectVector );
vec4 refractcolor = mix(texture(uTexUnit,RefractVector),
WHITE,0.2);
fFragColor=vec4(mix(reflectcolor.rgb,
refractcolor.rgb,uMix),1.);
}
Note that both the reflect( ) and refract( ) functions use the argu-
ment
normalize( vECposition )
What is this? The GLSL reflect( ) and refract( ) functions want that
argument to be the incoming vector from the eye to the point that is being
reflected from or refracted through. Remember that in OpenGL, once the
viewing transformation has been applied, the eye ends up at the origin. So,
those arguments could have been listed more clearly by making it obvious
that we were creating that vector as a difference between two points, like this:
normalize( vECposition - vec3(0.,0.,0.) )
Also, note that the refraction result mixes in some white. This is because
most refractive-transparent objects have a small amount of “milky” appear-
ance to them, as light is somewhat atenuated as it passes through the mate-
rial.
The second mix( ) is blending the refraction with some reflection, as is
usually the case with real objects.
Are these accurate reflections and refractions? They look good, but they
are not perfect, at least not in the ray-tracing sense. For one thing, each of
Search WWH ::




Custom Search