Pro OpenGL ES for iOS

(singke) #1

182 CHAPTER 6: Will It Blend?^


//glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//SQUARE 1

//glEnableClientState(GL_COLOR_ARRAY);

glColorPointer(4, GL_FLOAT, 0, squareColorsYMCA);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

//SQUARE 2

glLoadIdentity();
glTranslatef( (GLfloat)(sinf(transY)/2.0),0.0, -3.0);

glColorPointer(4, GL_FLOAT, 0, squareColorsRGBA);
glBindTexture(GL_TEXTURE_2D,m_Texture1.name);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

transY += 0.075f;
}
In addition to this, make sure to add loadTexture() from the Chapter 5 examples, and
initialize it in the usual place. Because we need two different textures, initialize the first
as m_Texture0 and the second as m_Texture1. You will likely notice that while I have both
blending and color stuff, I commented out some lines just for this first run-through to
ensure that the basic stuff is working. If it’s working, you should see something like
Figure 6-8 (left). And if that works, unleash the vertex colors by uncommenting
glEnableClientState(GL_COLOR_ARRAY) and glEnable(GL_BLEND), which should yield
Figure 6-8 (center). And for Figure 6-8 (right), the Golden Gate Bridge is colored with a
solid red. I shall let you, dear reader, figure out how to do this.
Using a single bitmap and colorizing it is a common practice to save memory. If you are
doing some UI components in the OpenGL layer, consider using a single image, and
colorize it using these techniques. You might ask why is it a solid red as opposed to
merely being tinted red, allowing for some variation in colors. What is happening here is
that the vertex’s colors are being multiplied by the colors of each fragment. For the red,
I’ve used the RGB triplet of 1.0,0.0,0.0. So when each fragment is being calculated in a
channel-wise multiplication, the green and blue channels are going to be multiplied by 0,
so they are completely filtered out, leaving just the red. If you wanted to let some of the
other colors leak through, you would specify the vertices to lean toward a more neutral
tone, with the desired tint color being a little higher than the others, such as 1.0,0.7,0.7.
Free download pdf