Graphics Reference
In-Depth Information
times it involves more computation than we might expect. In these sections,
we will review the primary kinds of work of the fixed-function fragment pro-
cessor and ask how we can do them with a fragment shader.
Shading
In the world of fixed-function OpenGL, shading means one of the standard
ways to apply colors to the pixels in a graphics primitive. For fixed-function
operations, all the processes that develop a color from lighting models are done
during vertex processing, so if we simply replace the fixed-function process-
ing with fragment shaders, we can assume that the key inputs to the fragment
shader are the color values, depths, and texture coordinates of each vertex in
each graphics primitive. The simple flat and Gouraud shading were described
in the earlier chapter on lighting.
Flat Shading
Flat shading is a standard, and very simple, way to give a color to a polygon.
It takes a single color and applies it identically to each pixel in the polygon.
To use flat shading, we usually specify a single color first and then define the
vertices of the primitive. If you should specify a separate color for each vertex
but still specify flat shading, the color that is used will be the color of the last
vertex specified in the primitive.
We noted in the chapter on vertex shaders that GLSL did not originally
support non-interpolating rasterization behavior, but that it has now added
the keyword flat for output variables headed to the fragment shader as well
as for the corresponding input variables in the fragment shader, so that those
variables are not interpolated across the polygon. In the previous chapter, we
introduced this concept and gave an example of a vertex shader to support flat
shading. Below, we show some fragment shader code that does this.
in flat float vLightIntensity;
uniform vec4 uColor;
out vec4 fFracColor;
void main( )
{
fFragColor= vec4( vLightIntensity * uColor.rgb, 1. );
}
The effect of this code is to use the familiar diffuse lighting computa-
tion that computes the light intensity in the vertex shader and sends it as a
flat variable instead of as a regular variable. The light intensity then is treated
as a constant for the primitive, yielding the same color for the entire primitive.
Search WWH ::




Custom Search