Graphics Reference
In-Depth Information
Flat Shading
Flat shading is a type of per-vertex color computation. In order to use flat
shading for a graphics primitive, the vertex shader will determine a color for
a particular vertex (called the provoking vertex ) and pass it forward to the frag-
ment processor. The color will not be interpolated across the fragments. The
color can come from an aColor atribute variable, or it could come from a light-
ing calculation, as described below.
In early versions of GLSL, it was not possible to specify flat shading, and
flat shading was seen as an operation that would be done by fixed-function
processing outside the GLSL shaders. However, GLSL has added a keyword
flat to the GLSL language, defining a vari-
able type called flat out variables . These vari-
ables may be passed to a fragment shader and
call for the variable's value not to be inter-
polated across a graphics primitive during
fragment processing. Our familiar teapot is
shown in Figure 6.3 with flat shading, a look
that may be familiar from your own begin-
ning graphics work.
Vertex shaders that use flat out vary-
ing variables difer litle from those you are
already familiar with. An example vertex
shader is shown below, which computes light
intensity from the standard diffuse technique
and passes this intensity to a fragment shader through the flat out variable
vLightIntensity . Compare this with the vertex shader you saw early in the
book to create Figure 2.2.
Figure 6.3. The familiar teapot with flat shading.
uniform vec3 uLightPos;
flat out float vLightIntensity;
void main( )
{
vec3 transNorm = normalize( uNormalMatrix * aNormal );
vec3 ECposition = ( uModelViewMatrix * aVertex ).xyz;
vLightIntensity = dot(normalize(uLightPos-
ECposition),transNorm);
vLightIntensity = abs(vLightIntensity);
gl_Position = uModelViewProjectionMatrix * aVertex;
}
Search WWH ::




Custom Search