Graphics Reference
In-Depth Information
The terms can be summed and the sum used directly, as shown in the
following fragment shader code that was used to produce the images in Fig-
ure 10.10.
uniform sampler3D Noise3;
uniform float uNoiseScale;
uniform float uNoiseMag;
uniform vec4 uColor1;
uniform vec4 uColor2;
in float vLightIntensity;
in vec3 vMCposition;
out vec4 fFragColor;
void main( )
{
vec4 nv = texture( Noise3, vMCposition * uNoiseScale );
float sum = abs(nv.r-.5) + abs(nv.g-.5) +
abs(nv.b-.5) + abs(nv.a-.5);
sum = clamp( uNoiseMag * sum, 0.0, 1.0 );
vec3 color = mix( uColor1.rgb, uColor2.rgb, sum )*
vLightIntensity;
fFragColor = vec4( color, 1.0 );
}
Note that, unlike C and C++, GLSL overloads the abs( ) function name for
taking the absolute value of both integers and floats.
Some Examples of Noise in Different Environments
A traditional use of noise is to provide interesting textures, often mimicking
natural phenomena, to use in our images. These use several different tech-
niques, including using only one or two of the available octaves of noise, or
manipulating noise or turbulence so that the function values lie in only a lim-
ited range or are shifted. We include code for these examples, so you can see
examples of some manipulations you might use.
As a first example of the use of noise to create a rich image, we illustrate
noise to simulate surface erosion using pixel discards. In Chapter 3, we used the
texture coordinates directly to determine the pixels to discard, and the result