Pro OpenGL ES for iOS

(singke) #1

CHAPTER 6: Will It Blend? (^195)
z = cos(lightAngle (3.14159 / 180.0));
// Half shifting to have a value between 0.0 and 1.0.
x = x
0.5 + 0.5; //3
y = y 0.5 + 0.5;
z = z
0.5 + 0.5;
glColor4f(x, y, z, 1.0); //4
//The color and normal map are combined.
glActiveTexture(GL_TEXTURE0); //5
glBindTexture(GL_TEXTURE_2D, tex0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); //6
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB); //7
glTexEnvf(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); //8
glTexEnvf(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS); //9
// Set up the Second Texture, and combine it with the result of the Dot3
combination.
glActiveTexture(GL_TEXTURE1); //10
glBindTexture(GL_TEXTURE_2D, tex1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //11
}
The preceding operation takes place using two stages. The first blends the bump map
with the primary color, which is established using the glColor4f call. The second takes
the results of that and combines it with the color image using our old friend GL_MODULATE.
So, let’s examine it piece by piece:
„ In line 1 we define lightAngle that will cycle between 0 and 180
degrees around the texture to show how the highlights look under
varying lighting conditions.
„ Calculate the xyz values of the light vector in lines 2ff.
„ In line 3, the xyz components need to be scaled to match those of the
bump map.
„ Line 4 colors the fragments using the light vector components.
„ Lines 5f set and bind the bump map first, which is tex0.
„ GL_COMBINE in line 6 tells the system to expect a combining type to
follow.
„ In line 7, we specify that we’re going to combine just the RGB values
using GL_DOT3_RGB operations (GL_DOT3_RGBA includes the alpha but is
not needed here).

Free download pdf