Pro OpenGL ES for iOS

(singke) #1

168 CHAPTER 6: Will It Blend?^


If an object isn’t using texturing but instead has its color specified via its vertices,
lighting, or overall global coloring, alpha will let the entire object or scene have
translucent properties. A value of 1.0 means the object or pixel is completely opaque,
while 0 means it is completely invisible.
For alpha to work, as with any blending model, you work with both a source image and
a destination image. Because this topic is best understood through examples, we’re
going to start with the first one now.
First let’s go back to the original bouncy square exercise from Chapter 3. Then use
Listing 6-1 in place of the original drawInRect() method, making sure you call
setClipping in your initializer as before. Solid squares of colors are used here first,
instead of textured ones, because it makes for a simpler example.
Listing 6-1. The new drawInRect() method

(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
static const GLfloat squareVertices[] = //1
{
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
-0.5, 0.5, 0.0,
0.5, 0.5, 0.0
};

static float transY = 0.0;

glClearColor(0.0, 0.0, 0.0, 1.0); //2

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Do square one bouncing up and down.

glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), -4.0); //3

glVertexPointer(3, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);

//SQUARE 1

glColor4f(0.0, 0.0,1.0,1.0); //4

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

//SQUARE 2

glColor4f(1.0, 0.0,0.0, .5); //5
Free download pdf