Tutorial 9
Textures
In this tutorial we'll learn how to
use textures. A texture is a graphic file which is placed in a
surface instead of a flat colour. In CsoundAV, you must use .png
(Portable Network Graphics) files. Photoshop can export this kind of
file, but unfortunately, a file exported by Photoshop is not usable
in CsoundAV. You can use a program like Irfanview or Paint Shop Pro
to convert the file exported by Photoshop, or use the program to
create the file. Irfanview is free, but it does not support the alpha
channel. Paint Shop Pro is very inexpensive but very powerful. You
might want to check it out at:
www.jasc.com
In this tutorial we'll start seeing
the relevance of the order in the rendering chain, as it defines
which object is considered the source and which the destination which
will define how colours are blended.
You can use any texture you like,
or you can use the one I made, which is included in the zip file in
the main page.
All headers begin as usual:
<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
To load a texture, we use the
instruction GLloadTexture. The global variable giTex1 stores a
'handle' value, and can be used later to call this texture. The first
parameter as you can see gives the path for the texture. You must
adjust the path to your system. Notice that in Csound you should use
'/' instead of the usual '\' character to separate pathnames. The
next two parameters state how the texture is to be used. First you
state the transparency which in this case is $PNG_ALPHA. This means
the transparency depends on the alpha channel of the png file. You
might want to define a specific colour to be transparent. Check the
CsoundAV html help for more paramters. For now we´ll use this
value which is the usual value. The next values states whether the
texture is to be tiled ($GL_REPEAT) or only drawn once ($GL_CLAMP).
In this case we want the texture to repeat (even though the
repetitions will not be seen).
giTex1 GLloadTexture
"d:/csound/silly.png", $PNG_ALPHA , $GL_REPEAT
Loading of textures must always be done
at initialization so we need to place the following line after any
loading of textures.
GLinsert_i $GL_INIT
glMatrixMode $GL_PROJECTION
glLoadIdentity
gluPerspective 60,0.1,100
glMatrixMode $GL_MODELVIEW
glEnable $GL_BLEND
;glEnable $GL_DEPTH_TEST
For blending to work as we intend the
depth test must be disabled. I leave this comment line so you can see
what happens if we leave depth test on.
glBlendFunc
$GL_SRC_ALPHA,$GL_ONE_MINUS_SRC_ALPHA
We use the standard blending function.
glEnable $GL_TEXTURE_2D
We enable the use of 2D textures with
the previous command.
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
The rest of the headers go as usual.
Now to our instrument.
instr 1
idisp = p4
t1 GLoscil 0.5,360,gisine
We create a sine wave oscillator which
will later control the z position of our object.
glLoadIdentity
glBindTexture giTex1
Before drawing a textured object, we
must tell the engine which texture to use. This is done with
glBindTexture, which has only one parameter which is the 'handle'
value of the texture.
glTranslate p4,p4/2,(-t1) -5
The x and y position is defined by
p-field 4 and the z position is variable according to the oscillator
created above.
GLtexSquare 1,
1,0,0,0,0,1,0,0
the instruction GLtexSquare creates a
textured square. The first parameter gives the dimensions of the
sides of the square. The next four pairs of parameters give the
texture coordinates, starting from the lower right hand corner, and
going counterclockwise. In this case the texture occupies the whole
square created. These coordinates are 'normalized', in the sense that
they are independent of the size of the square created, and always
assume a square of size 1.
GLinsert 1.5
endin
</CsInstruments>
<CsScore>
Now we draw the squares.
i1 1 60 2
i1 4 60 1
i1 8 60 0
i1 10 60 -1
i1 12 60 -2
</CsScore>
</CsoundSynthesizer>
That's
it. I've included in the csd other examples (commented out) of
texture mapping in the zip file that contains the tutorials. You
might want to comment them out and see the results.
Back
to OpenGL Tutorials Index