Game Development Reference
In-Depth Information
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture.Id);
Gl.glEnable(Gl.GL_BLEND);
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
Blending needs to first be enabled. This can be done just underneath where the 2D
texture mode was enabled. Then the blend function must be set. The blend
function takes two arguments. The first argument modifies the value of the pixel
to be drawn onto the frame buffer, and the second argument modifies the value of
the frame buffer pixel that will be drawn over. The incoming pixel to be drawn
onto the frame buffer is known also known as the ''source''. GL_SRC_ALPHA is an
instruction to use the alpha of the incoming pixel. GL_ONE_MINUS_SRC_ALPHA
is an instruction to take the alpha of the incoming pixel from one. These two
instructions blend the incoming pixel onto the frame buffer using its alpha value.
The glBlendFunc modifies the R,G,B values of the source and frame buffer
pixels, after which the sum of these values is written to the frame buffer.
In all the examples given so far, the initial color of the frame buffer is black. Every
pixel is of the RGBA form 0,0,0,1. The smiley face being rendered has lots of
different types of pixels. The pixels in the corner areas are white but with an
alpha of zero. The RGBA values are generally 1,1,1,0.
When the pixels in the corner areas of the smiley face texture are rendered, they
will have an alpha value of zero. The value of one minus the source alpha is (1 - 0),
so, 1. The new frame buffer colors are calculated by multiplying the incoming
pixels by the source alpha and then multiplying the current frame pixels by one
minus the source alpha and then adding the two results together.
incomingRed * 0
þ
frameBufferRed * 1
incomingGreen * 0
þ
frameBufferGreen * 1
incomingBlue * 0 þ frameBufferBlue * 1
As you can see, using this blend the incoming pixels are ignored, making the
corners of the face appear transparent. The general equation is
(incomingRGB * incomingAlpha) þ (framebufferRGB * (1 - incomingAlpha))
Try working through this as if the incoming alpha was 1 or 0.5. This is how
OpenGL performs its blending.
Running the program will produce a face with the white backing removed.
 
Search WWH ::




Custom Search