Game Development Reference
In-Depth Information
In the first three lines, we use a GLSL macro to set precision of the float values to
medium for devices that use OpenGL ES. In the next lines, you may have already
recognized our two varying variables that we pass from within our vertex shader to
this one.
Be sure to always match the name and data type of each
varying variable passed from a vertex shader to the
corresponding fragment shader.
The data type sampler2D stands for a two-dimensional texture. The u_amount variable
is meant to be set by the application code to control the amount of grayscale that
should be applied. The first line inside the main() function of the fragment shader
computes a combined color value between the original vertex color and the color value
of the texture referenced in u_texture using the coordinates passed in v_texCoords .
Now, to find an appropriate grayscale value, we compute the dot product of our
color vector and another vector containing varying color weights that match best with
the sensitivity of a typical human eyesight as suggested in Chapter 22.3.1 , Grayscale
Conversion of the topic GPU Gems , Randima (Randy) Fernando , Addison Wesley .
This topic is publicly available on NVDIA's developer zone at http://http.
developer.nvidia.com/GPUGems/gpugems_ch22.html .
Then, we use the mix() function, which applies a linear interpolation between the
original color vector and the grayscale vector using the u_amount variable. The
gl_FragColor variable is another predefined output variable of GLSL that
determines the final pixel color of the fragment that is currently being processed.
Using the monochrome filter shader program
in Canyon Bunny
Now that we have created our monochrome filter shader program, it is time to put
it to use in Canyon Bunny. We want to use the shader's effect in the game screen,
and apply it to the game world. This means that the GUI will remain colored like
before. In addition to this, the shader program can be switched on and off with a new
checkbox added to the debug section of the menu screen's Options dialog.
 
Search WWH ::




Custom Search