Tutorial 5

glScale and rotation

This tutorial doesn´t introduce many new concepts, but expands on instructions previously studied. The new instruction introduced in this tutorial is glScale . This instruction 'scales' our reference axis. It is a cumulative transformation like others we've seen in the sense that all transformations after it are affected by the scaling, until a new glLoadIdentity appears to clear transformations. For example if after scaling the reference axis to twice the size and then translating 1 unit, what will happen is actually a translation of two units, since we are translating one unit on a scaled axis. The other thing that is played around with is colour values (look at the score) and rotation. See how simple rotations can create nice 3D effects.


<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

gisine ftgen 1,0,1024,10,1
;All our usual headers.
instr 2
ired = p4
igreen = p5
iblue = p6
ix = p7
iy = p8
glLoadIdentity
tsize GLoscil 1,360,1
trot1 GLoscil 1,300,1
;Instrument 2 is very similar (and based) on instrument 2 from the previous tutorial. P-field assignments
;are a little different, and we will use to independent oscillators to control rotation and size. Notice the frequency
;difference in both.
glTranslate ix,iy,-6
glRotate (trot1*360),1,0,1
;Notice that we rotate around the x and the z axis simultaneously, which produces a strong 3D rotation.
glColor ired,igreen,iblue,1
glScale (tsize*3) , (tsize*3) ,(tsize*3)
;The glScale instruction has three values which indicate the scaling of each axis. If the values are equal,
;the relative shape remains the same. If they are different, the aspect ratio is changed and even rotations
;will behave strangely. What will happen here is that the objects will grow from 0 size to 3 times the size,
;back to 0 and then when the negative part of the oscillator comes, the shape is drawn in the opposite direction.
;This will be clearer when you run the program.
glLineWidth 6
;This instruction determines how thick a line will be rendered. Useful when drawing lines instead of filled polygons, as below.
glBegin $GL_LINE_LOOP
;Notice that this time instead of $GL_QUADS we've used $GL_LINE_LOOP.
;This creates lines connecting every point, instead of a filled polygon.
;Notice that a line is added between the last vertex and the first, closing the loop.
glVertex3 0,0,0
glVertex3 1,0,0
glVertex3 1,1,0
glVertex3 0,1,0
glEnd
GLinsert 1.5
endin



</CsInstruments>
<CsScore>

i 2 2 38 1 0 0 1 -1
i 2 3 37 0.8 0.2 0 0.8 -0.8
i 2 4 36 0.6 0.4 0 0.6 -0.6
i 2 5 35 0.4 0.6 0 0.4 -0.4
i 2 6 34 0.2 0.8 0 0.2 -0.2
i 2 7 33 0 1 0 0 0
i 2 8 32 0 0.8 0.2 -0.2 0.2
i 2 9 31 0 0.6 0.4 -0.4 0
i 2 10 30 0 0.4 0.6 -0.6 0
i 2 11 29 0 0.2 0.8 -0.8 0
i 2 12 28 0 0 1 -1 0
i 2 13 27 0.2 0 0.8 -1.2 0
i 2 14 26 0.4 0 0.6 -1.4 0
;Notice how the colour is gradually changing and how this reflects on the colour of the objects.

</CsScore>
</CsoundSynthesizer>

As I said before this tutorial doesn't introduce too many new concepts but shows other uses of the instructions we've seen in previous tutorials. It also looks a lot nicer because it moves more 'unexpectedly'. As always experiment a lot changing values and adding whatever you come up with to see the results.

Back to OpenGL Tutorials Index