In this tutorial things start getting complicated. We will be changing alpha values through the use of a global control instrument. This means that we will have one instrument which will produce no visible output, but will generate a variable that will be available for all other instruments. When you want to modify several instruments simultaneously you will need to use a global variable which will usually be controlled by a 'control' instrument. Control instrument should have a lower number that the instruments it will be controlling. The reason for this is a little complicated for this tutorial, so just try to practice this when you use control instruments.
When using alpha values, a new concept called alpha blending must be introduced. Alpha blending is the process of mixing colours according to the alpha values of the source (SRC) object and the destination (DST) object. The source object is the one created latest in the rendering chain, either because it has a greater number in GLinsert or because it was created later. The pixel colour is defined by the function:
C = Csrc * F src + Cdst * Fdst
Where C is the original colour and F refers to the blending function used. For example when:
glBlendFunc $GL_SRC_ALPHA,$GL_ONE_MINUS_SRC_ALPHA
is used the function will be:
C = Csrc * α src + Cdst * (1- αsrc)
In this case the colour of the foreground object (src) is multiplied by its alpha value, and the colour of the destination objext is multiplied by (1-src alpha). So the back colour is strong when the foreground object is more transparent. Remember that an alpha value of 1 means opaque and a value of 0 means transparent.
Grasping this takes a while and requires experimentation, so play a lot with this tutorial, and make up your own examples to understand it.
Let's start the tutorial.
<CsoundSynthesizer> <CsOptions> -+Y </CsOptions> <CsInstruments>
$GL_SRC_ALPHA |
---|
$GL_ONE_MINUS_SRC_ALPHA |
$GL_DST_ALPHA |
$GL_ONE_MINUS_DST_ALPHA |
$GL_ONE |
$GL_ZERO |