Tutorial 3

Rotation constants and p-fields

This tutorial will show you how to use the glRotation instruction, and show you what score p-fields are and how they are used.

The program starts just the same as the previous tutorials, until instrument 1. Remember to type only the lines in italics.


<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


GLinsert_i $GL_NOT_VALID

glClear $GL_COLOR_BUFFER_BIT + $GL_DEPTH_BUFFER_BIT

GLinsert_i 1.1


instr 1

islant = p4

;Upto here you should recognize the code. Now we see a csound variable being defined.

;Csound works with several types of variables that have different uses and performance.

;The simplest ones are called i-rate (or instrument-rate) variables. They can be though of

;as constants within an instrument. An i-rate variable starts always with the character 'i'.

;Normally the characters following this 'i' give some short description about what the variable will be used for.

;In this case, we will be using the variable islant. Notice that we assign the value p4 to the variable.

;This means assign p-field 4 to the variable. P-fields are numbers that are entered in the score section

;when calling the instrument. See below in the score that apart from the 3 values we've used when calling

;instruments (instrument number, start time and end time) there are 2 more values or columns. ;

;These are p-fields number 4 and 5. All p-fields after 3 have no definite or fixed use.

;Their use is defined by the composer for every instrument. P-fields are useful because they permit

;instantiation of an instrument with variations intead of having to write a whole new instrument.

;P-fields must be separated by at least one space (can be more or a tab).

irotation = p5

;The above applies here as well. The fifth value accompanying the instrument call is written

;to variable irotation. Notice the values usedin the score: for irotation we have multiples of 45,

;and for islant we have either 0 or 45. Also notice that islant and irotation are local variables,

;i.e. they are different and independent for every instrument call. Each time the instrument is called

;it has its own independent islant and irotation.

glLoadIdentity

;Remember that glLoadIdentity resets all rotations and transformations.

glTranslate 0,0,-5

;First we translate our axis 5 units into the screen so we can see the objects drawn.

;If we didn't do this, the objects would be drawn at the same place the camera is, and wouldn't be seen.

glRotate irotation,0,0,1

;Now we will use the instruction glRotate. The values that must be entered are: first the angle

;and then a scaling factor the rotaion around each axis. In this first case we will rotate irotation degrees

;around the z axis. Rotation around the z axis is rotating like the hands of a clock or a propeller.

;Notice that each time instrument 1 is instantiated, it is rotated 45 degrees more than the previous instrument.

glRotate islant,0,1,0

;Now we will use the variable islant to make a rotation around the y axis. Notice that this rotation

;occurs after the previous rotation and applies to the already modified axis. In other words,

;we are not rotation around the original y axis, but upon a modified relative y axis. This might take a while

;to understand specially if you're not good at visualizing 3D geometry, but hopefully with a little practice

;it'll make sense. Rotation upon the y axis is like the rotation of an LP record. Just imagine that the turntable

;has first rotated like a propeller and then plays the record.... makes sense?

glBegin $GL_TRIANGLES

;Now that we have our axis positioned, we will draw a triangle. This is done just like drawing QUADS

;like in the previous tutorial, but require only 3 vertices. Notice the way glColor is used to make the centre

;white and the edges blue.

glColor 1,1,1,1

glVertex3 0,0,0

glColor 0,0,1,1

glVertex3 1,2,0

glVertex3 -1,2,0

glEnd

GLinsert 1.5

;Never forget to put GLinsert...

endin

;This is the only instrument... It will be called 8 times down in the score with different rotation values

;and start times.

</CsInstruments>

<CsScore>

i 1 1 12 45 45

i 1 2 12 0 90

i 1 3 12 45 135

i 1 4 12 0 180

i 1 5 12 45 225

i 1 6 12 0 270

i 1 7 12 45 315

i 1 8 12 0 0



</CsScore>

</CsoundSynthesizer>

</CsoundSynthesizer>


That's tutorial 3. Rotation and the use of p-fields will be become clear with use, so don't worry if you can't quite grasp it at first.

The other values that can be used with glBegin are:

$GL_POINTS

$GL_LINES

$GL_LINE_STRIP

$GL_LINE_LOOP

$GL_TRIANGLES

$GL_TRIANGLE_STRIP

$GL_TRIANGLE_FAN

$GL_QUADS

$GL_QUAD_STRIP

$GL_POLYGON



For a complete definition of each, refer to the openGL programming guide at:

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



Back to OpenGL Tutorials Index