Tutorial 8
Spheres, Cubes and lighting
In this tutorial we'll learn how to
create spheres, cubes, and enable lighting. Lighting in necessary to
appreciate 3D shapes like spheres and cubes. If lighting is not
enabled, we only see the coloured outlines of the shapes, without any
shading. Spheres, cubes and other shapes are easily creating using
GLUT instructions. GLUT (GL Utility Toolkit) is a package of higher
order openGL instructions. What this means is that GLUT simplifies
the programming process by making available many usual functions,
like drawing a sphere or a cube: Something which could take many
lines of code can be done with GLUT in only one line.
We begin with all headers 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
glMatrixMode $GL_PROJECTION
glLoadIdentity
gluPerspective 60,0.1,100
glMatrixMode $GL_MODELVIEW
glEnable $GL_DEPTH_TEST
glEnable $GL_BLEND
glBlendFunc
$GL_SRC_ALPHA,$GL_ONE_MINUS_DST_ALPHA
glEnable $GL_LIGHTING
glEnable $GL_LIGHT0
glEnable $GL_COLOR_MATERIAL
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
instr 10
ired = p4
iblue = p5
iperiod = p6
idisp = p7
glLoadIdentity
tmove GLoscil 4,p6,1
glTranslate
idisp,0,(-10-idisp)
glTranslate 0,abs(tmove) -2,0
glColor ired,0.5,iblue,1
glutSphere 0.5,25,25,1
GLinsert 1.5
endin
instr 11
ired = p4
iblue = p5
iperiod = p6
idisp = p7
glLoadIdentity
tmove GLoscil 4,p6,1
glTranslate
idisp,0,-(10-idisp)
glTranslate 0,abs(tmove) -2,0
glColor ired,0.5,iblue,1
GlutCube 1,1
GLinsert 1.5
endin
</CsInstruments>
<CsScore>
; start red blue period
displacement
i 10 1 30 1 0 120 -2.5
i 10 1.5 30 1 0.4 120 -2
i 10 2 30 0.6 0.2 130 -1.5
i 10 2.5 30 0.2 0.4 130 -1
i 10 3 30 0.2 0.8 140 0
i 11 3.5 30 0.2 0 140 1
i 11 4 30 0.4 0 150 1.5
i 11 4.5 30 0 0 150 2
i 11 5 30 0 1 160 2.5
i 11 5.5 30 0.5 1 160 3
</CsScore>
</CsoundSynthesizer>
So
this is tutorial 8. There are other GLUT shapes you might want to
experiment with, like glutTorus, glutDodecahedron, glutIcosahedron,
glutTetrahedron, and glutOctahedron. Each has its own set of
parameters, so you might have to check Gabriel's html help.
Back
to OpenGL Tutorials Index