Tutorial 6
Control instruments and alpha
blending
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 closer to the viewer. Grasping this takes a
while and requires experimentation, so play a lot with this tutorial,
and make un your own examples to understand it.
Let's start the tutorial.
<CsoundSynthesizer>
<CsOptions>
-+Y
</CsOptions>
<CsInstruments>
#include "OpenGL.h"
sr=100
kr =100
ksmps=1
nchnls=1
GLfps 30
GLpanel "OpenGL panel",
512, 512
GLpanel_end
FLrun
glMatrixMode $GL_PROJECTION
glLoadIdentity
gluPerspective 60,0.1,100
glMatrixMode $GL_MODELVIEW
glEnable $GL_BLEND
glBlendFunc
$GL_SRC_ALPHA,$GL_ONE_MINUS_DST_ALPHA
GLinsert_i $GL_NOT_VALID
glClear $GL_COLOR_BUFFER_BIT
+ $GL_DEPTH_BUFFER_BIT
GLinsert_i 1.1
gisine ftgen 1,0,1024,10,1
instr 1 ;control instrument
gkalpha linseg
0,10,1,10,1,10,0
endin
instr 2
ired = p4
igreen = p5
iblue = p6
iy = p7
glLoadIdentity
talpha = t (gkalpha)
tmove GLoscil 2,360,1
glTranslate tmove,iy,-6
glColor
ired,igreen,iblue,talpha
glBegin $GL_QUADS
glVertex3 0,0,0
glVertex3 1,0,0
glVertex3 1,1,0
glVertex3 0,1,0
glEnd
GLinsert 1.5
endin
</CsInstruments>
<CsScore>
i 1 0 30
i 2 2 28 1 0 0 1
i 2 3 27 0 1 0 0.8
i 2 4 26 0 0 1 0.6
i 2 5 25 1 0 1 0.4
i 2 6 24 0 1 1 0.2
i 2 7 23 1 1 0 0
i 2 8 22 1 1 1 -0.2
i 2 9 21 1 1 0 -0.4
i 2 10 20 0 1 1 -0.6
i 2 11 19 1 0 1 -0.8
i 2 12 18 0 0 1 -1
i 2 13 17 0 1 0 -1.2
i 2 14 16 1 0 0 -1.4
</CsScore>
</CsoundSynthesizer>
That's
it for tutorial 6. Remember as always to experiment with values, and
changing things around. It's the only way to learn.
Notice
how the control instrument 'binds' objects together, by letting them
all share values, even if they start at different times or have
different durations.
Notice
also how the change in alpha can be though of as a change in
transparency. Also be aware that the behavior of alpha values, is
completely dependent of the blending function used. Some very
interesting results can be achieved from a dull scene by just
changing the alpha blending characteristic. The values that can go in
either parameter of glBlendFunc are:
|
$GL_SRC_ALPHA
|
|
$GL_ONE_MINUS_SRC_ALPHA
|
|
$GL_DST_ALPHA
|
|
$GL_ONE_MINUS_DST_ALPHA
|