Tutorial 2

Basic Polygons



This tutorial will show you how to create simple coloured polygons and how to handle multiple csound instruments in the score. It will also show how to translate the coordinate system to position objects and how to define the perspective of the scene.



Type everything in italics into your text editor (or copy-paste...):



<CsoundSynthesizer>

<CsOptions>

-+Y

</CsOptions>

<CsInstruments>

#include "OpenGL.h"

sr=100

kr =100

ksmps=1

nchnls=1



GLfps 30

GLpanel "Coloured polygons", 512, 512

GLpanel_end

FLrun

;All the lines up to here you should recognize from the previous tutorial. The only difference is that the

;FLrun instruction appears at the beginning outside an instrument. This just means that the window

;is created as the compilation starts, before any instrument calls and independent of an instrument call.



;This next section deals with the way the image is rendered. This is like defining what type

;of lens our 'camera' is using. First we need to tell CsoundAV that we will be defining the projection's

;properties (the 'camera' properties) instead of the objects' (called models). This is done selecting

;the Matrix Mode.

;Remember that with openGL instructions you need to be very careful with upper/lower case.

;Also note that all openGL instructions start with gl. Many of the them (the ones that refer to

;'true' openGL instructions) begin with lowercase gl, then the capitalized command name

;(e.g. glMatrixMode, glClear, etc.)



glMatrixMode $GL_PROJECTION



;This next instruction is bit confusing because it actually has no effect. It might be said that it

;undoes all transformations. That means that all displacement from the centre, all rotations

;and all scaling is set to the initial default value. Here it is necessary to explain that openGL

;works by displacing the coordinate system, so that all actions that follow are exectude in relation

;to that new coordinate system. I'll try to explain that better further down. For now remember that

;glLoadIdentity resets all rotations, displacement and scaling.



glLoadIdentity



;Here we define the perspective with which our scene will be rendered. The first value refers

;to the angle that is shown. This angle is the one made by two lines coming out of the 'point of view'

;in the up-down direction. The second number states the closes point that will be shown

;(any object that is closer than this point, will be discarded. The last number states the furthest away

;that should be rendered. Anything beyond that point is discarded. 60,0.1, 100 are

;common values, though these can be very variable.

gluPerspective 60,0.1,100



;After we transform the perspective, we must tell CsoundAV that we want to alter the objects or model,

;not the 'camera'. This is done by changing the matrix mode back to the Model view, like this:

glMatrixMode $GL_MODELVIEW



;As we saw in the last tutorial, GLinsert puts all the previous openGL instructions in the rendering chain.

;GLinsert_i is a special case, because it places the instructions at a special intialisation phase

;$GL_NOT_VALID means a value of 0, and so places the instructions at the very top of the chain

;(I need confirmation on this...)



GLinsert_i $GL_NOT_VALID



;Clear the screen as in the last tutorial, this time at every initialisation phase, in position 1.1 of this phase.

glClear $GL_COLOR_BUFFER_BIT + $GL_DEPTH_BUFFER_BIT

GLinsert_i 1.1



;All the previous instructions were outside instr-endin blocks, so they act all the time from start to finish

;of compilation. Now comes instrument 1:

instr 1

glLoadIdentity

;Again glLoadIdentity, to make sure the object is drawn where we want relative to the centre.

;glLoadIdentity clears all movements of the coordinate system, taking it back to the centre or origin.

glColor 1,1,1,1

;Like glClearColor, glColor states 4 values that refer to the red, green, blue and alpha values.

;This instruction defines the colour for all objects drawn, until another glColor instruction appears.

;In this case we are selecting white.

glTranslate -1,1,-5

;glTranslate is an instruction to.... translate the coordinate system. It tells CsoundAV where to put

;the new origin or centre. In this case we displace the centre 1 unit to the left (negative on the x axis)

;1 unit up (on the y axis), and 5 units into the screen (negative on the z axis). As you can see

;the coordinate system is just as you would draw it on a piece of paper, with x going positive to the right,

;y going upwards and z coming out of the paper towards you. If we didn't displace the axis

;back 5 units we would not see our object because it would be at the same z position

;as the 'camera' which is set by default at (0,0,0).



;Now we begin creating a shape. The easiest way to define a shape using openGL is by placing

;the vertices. All vertex instructions must be within a glBegin-glEnd block, as you can see below.

;When using the instruction glBegin, you need to state what type of object you are beginning,

;which in this case are QUADS, or squares.

glBegin $GL_QUADS

;Now we state the four vertices of the square with the command glVertex3.

;Notice that when this square appears, our (0,0,0) coordinate is actually (-1,1,-5)

;because of the translate command we gave earlier.

glVertex3 0,0,0

glVertex3 1,0,0

glVertex3 1,1,0

glVertex3 0,1,0

;When we finish giving the vertices we must place a glEnd command to tell the compiler

;that the object is done.

glEnd

GLinsert 1.5

;Now we insert these instructions into the rendering chain, and close the instrument with endin.

;Notice that GLinsert has GL in uppercase.

endin



instr 2

;This instrument is very similar to the one before, but it places our origin at a different place

;(-1,0,-5) and inserts a glColor instruction at the beginning and then before the last two vertices.

;Notice that when rendering, the square actually dissolves between one colour value and the other.

glLoadIdentity

glColor 0,0,1,1

;Blue is (0,0,1,1).

glTranslate -1,0,-5

glBegin $GL_QUADS

glVertex3 0,0,0

glVertex3 1,0,0

glColor 1,1,1,1

glVertex3 1,1,0

glVertex3 0,1,0

glEnd

GLinsert 1.5

endin



;Instruments 3 and 4 are the same as instrument 2, except that they place the square in another

;position, and use different colours. It's good to point out now, that QUADS refers to a filled square.

instr 3

glLoadIdentity

glColor 1,0,1,1

;Purple is (1,0,1,1)

glTranslate -1,-1,-5

glBegin $GL_QUADS

glVertex3 0,0,0

glVertex3 1,0,0

glColor 0,0,1,1

glVertex3 1,1,0

glVertex3 0,1,0

glEnd

GLinsert 1.5

endin



instr 4

glLoadIdentity

glColor 1,1,1,1

glTranslate -1,-2,-5

glBegin $GL_QUADS

glVertex3 0,0,0

glVertex3 1,0,0

glColor 1,0,1,1

glVertex3 1,1,0

glVertex3 0,1,0

glEnd

GLinsert 1.5

endin

;This is the end of the orchestra or instrument section. We have created 4 instruments,

;which will be called now in the score.

</CsInstruments>

<CsScore>

;The instruments are called one by one, starting and ending at different times.

i 1 0 6

i 2 2 8

i 3 3 9

i 4 4 10



</CsScore>

</CsoundSynthesizer>



That's it for tutorial 2. Experiment with different translate and colour values. Try to give each vertex a different colour, and try to change the length and starting point of the instruments. You might want to check out the openGL programming guide for a more complete explanation of perspective in openGL at:

http://www.parallab.uib.no/SGI_bookshelves/SGI_Developer/books/OpenGL_PG/sgi_html/index.html



Back to OpenGL Tutorials Index