Tutorial 1

Creating an openGL window

This tutorial explains the .csd file syntax and the creation and clearing of an openGL window in CsoundAV. It might be complicated at first, specially if you're new to programming, but you'll soon see the benefits.

The csound program should include only the parts in italics. Type only those parts into the text editor. Be careful with uppercase and lower case characters in openGL instructions, flags and section headers. CsoundAV is case sensitive with these instructions (though not with the common sound instructions).

I have deliberately included the original file in a zip folder, but you should try (at least at the beginning) to type the program yourself...

All .csd programs start like this:

<CsoundSynthesizer>


;This just tells the compiler (i.e. CsoundAV) that this is the start and that the file is in .csd (unified) format. The different section

;in a csd file begin <Section> and end </Section>. You can see this clearly in this example. The sections are: CsOptions,

;CsInstruments and CsScore. The only other section the compiler will recognize is CsVersion, which limits the minimum version

;needed to run the csd file, but is rarely used.

<CsOptions>

;This section contains the command line flags that are used when compiling the csd file. They are added to the command line

;when a front end calls csound to compile.

-+Y

;This flag tells csound not to render audio. This is useful for video only programs. See the Csound Manual and the CsoundAV

;html help for more command line flags.

</CsOptions>

;This marks the end of the CsOptions section.



;Here's where everything starts:

<CsInstruments>

;This section defines the 'instruments' that will be created when the program compiles. It is also referred to as the Orchestra.

;The term 'instrument' might be a little confusing because a csound instrument doesn't necessarily produce sound. An

;instrument can be thought of as any process or group of processes made by csound. An instrument can be a control

;instrument, an input instrument, an effects instrument (among others), or like in this case a graphic instrument. Instruments are

;written to be called later in the Score. Just creating an instrument in the Orchestra has no effect.


#include "OpenGL.h"

;This line must be present if you will be using CsoundAV openGL capabilities. It contains macro definitions that will make later

;programming more intuitive. Don't worry about this line, just put it there.


sr=100
kr =100
ksmps=1
nchnls=1

;This is the standard Csound header. It states the sample rate (sr), control rate (kr), the control samples per second (ksmps)

;and the number of output channels. This affects only the audio engine, so the values are very low in this case. This header is a

;requirement of Csound. It must always be there.



;This is where the graphics part starts:

GLfps 30

;Here you set the number of frames per second for the GL window. A usual value is 30.


GLpanel "Empty window", 512, 512

;Here you define the openGL window name and size of the openGL window to be created. Experiment with other values.

GLpanel_end

;This command must accompany the previous GLpanel.



;Here we define the first instrument:

instr 1

;instr is the csound command to define an instrument. Every instrument must have a unique number, and must end with an endin

;instruction.

FLrun

;This instruction creates the window. The previous GL instructions just defined the window, while this command creates it.

glClearColor 0,0,0,0

;Here we set the background color. The values used are from 0 to 1 and represent the red, green and blue (RGB) values. The

;fourth value sets the alpha value. Don't worry about it for now. Try using different values to produce different colours.

glClear $GL_COLOR_BUFFER_BIT + $GL_DEPTH_BUFFER_BIT

;glClear is an openGL instruction to clear the window. You generally want to use the value $GL_COLOR_BUFFER_BIT +

;$GL_DEPTH_BUFFER_BIT, which will completely clear the window.

GLinsert 1.5

;This tells CsoundAV where in the graphic render chain to put the GL instructions above. A common value is 1.5. This value is

;fine for most cases. If this command is missing, the openGL instructions don't enter the rendering chain and therefore have no

;effect. Always remember to put it after a set of openGL instructions.

endin

;This is the end of the instrument.

</CsInstruments>

;The end of the CsInstruments section. With this program only one instrument (instr 1) will be available.

<CsScore>

;The score starts. Score in csound means 'instrument calls'. The score command i calls an instrument. The numbers following i ,

;specify the instrument number, the start time (in seconds)and the duration (in seconds). The i command below calls instrument

;1 at second 4, for a length of 100 seconds. Try changing the start time and the duration.

i 1 4 100

</CsScore>

;The score section ends and then the .csd file ends

</CsoundSynthesizer>


If you're running WinXoundPro and have located CsoundAV on the CsoundAV path dialog, you can save the program and press f4, then click on Play, or press enter. The program should compile and create an openGL window. Check spelling and lower/upper case if csound reports an error.


NOTES:
-The size stated on the instruction GLpanel refers only to the initial size of the window. If it is dragged, the resolution changes accordingly.

-Notice in the orchestra section that some parts like the header and the GLpanel instructions are outside instr-endin blocks. There is a strong reason for this, but it can be thought of as anything outside instr-endin blocks is a global instruction, while anything inside is local and applies only to the instrument. Note that most instructions can only be in one place, only outside, or only inside instr-endin blocks.

Back to OpenGL Tutorials Index