Pro OpenGL ES for iOS

(singke) #1

CHAPTER 6: Will It Blend? (^189)
Now it’s time to play with other combiner settings. Try GL_ADD for the texture mode,
followed by GL_BLEND and GL_DECAL. My results are shown in Figure 6-12. For addition,
notice how the white part of the overlay texture is opaque. Because white is 1.0 for all
three colors, it will always yield a 1.0 color so as to block out anything underneath. For
the nonwhite shades, you should see a little of the Hedly texture poke through.
GL_BLEND, as shown in Figure 6-12 (center), is not quite as obvious. Why cyan splats in
place of the red? Simple. Say the red value is 1.0, its highest. Consider the equation for
GL_BLEND:
Output=Pn( 1 −Sn)+Sn×C
The first section would be zero for red, because red’s value of 1 is subtracted by the 1 in
the equation, and by gosh, the second one would be too, providing that the default
environment color of black is used. Consider the green channel. Assume that the
background image has a value of .5 for green, the ‘‘previous’’ color, while keeping the
splat color (the source) of solid red (so no blue or green in the splat). Now the first
section of the equation becomes .5*(1.0-0.0), or .5. That is, the .5 value for green in the
previous texture, Hedly, is multiplied against ‘‘1- minus-green’’ in the source texture.
Because both the green and blue channels in the source’s red splats would be 0.0, the
combination of green and blue without any red gives a cyan shading, because cyan is
the inverse of red. And if you look really closely at Figure 6-12 (center), you can just
make out a piece of Hedly poking through. The same holds true for the magenta and
yellow splats. In Figure 6-12 (right), GL_DECAL is used and can serve many of the same
duties that decals for plastic models had, namely, the application of signs or symbols
that would block out anything behind it. So for decals, typically the alpha channel would
be set to 1.0 for the actual image part of the texture, while it would be 0.0 for any part
that was not of the desired image. Typically the background would be black, and on
your paint program you would have it generate an alpha channel based on luminosity or
for the part of the image that has a nonzero color. In the case of the splat, because the
background was white, I had to invert the colors first to turn it black, generate the mask,
and merge it with the normal positive image. The image actually used is the
rgb_splats_masked.256.color.png file you can find in the project download. Some alpha
that is slightly less than 1 was generated for the green channel, and as a result, you can
see a little part of Hedly showing through.
Note On older pre-iPhone 3GS/pre-iPod touch third-generation devices, Apple lists a number
of caveats in its OpenGL ES programming guide. If you want to ensure your creation will work
on earlier devices, you should check it out.

Free download pdf