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
We hadn't seen this particular function we just enabled. This makes the graphic engine check whether an object is in front of another, rather than rendering just one on top of the other depending of the order in which the objects are created. This is useful when you want to create 'realistic' 3D scenes where objects in front cover objects in the back. Notice that this is independent of the alpha blending function. Depth test helps define which object will be the source and which the destination for the alpha blending.
glEnable $GL_BLEND
glBlendFunc $GL_SRC_ALPHA,$GL_ONE_MINUS_DST_ALPHA
Notice the blend function used. This is normally the blend function you would want to use when you want opaque objects in the front to completely hide objects in the back, and when you want 3D shapes to look 'normal'. To understand better what I mean, try other blend functions, and see the results.

glEnable $GL_LIGHTING
This instruction enables lighting. It doesn't create any lighting, it just enables it. Used by itself has no effect. You must enable a light as well.
glEnable $GL_LIGHT0
This command enables a light. In this case light number 0, You can have several lights in a scene, and you must enable each one individually. The light is created at a default position with default values, which for now we will not deal with.
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
The rest of the headers you should recognize, as the definition of i-rate variables with p-field values.
ired = p4
iblue = p5
iperiod = p6
idisp = p7
glLoadIdentity
tmove GLoscil 4,p6,1
We create an oscillating variable called tmove, which will oscillate with an amplitude of 4 (values from -4 to 4), and each cycle of oscillation will take p6 frames.
glTranslate idisp,0,(-10-idisp)
We first translate our axis idisp units on the x-axis. Notice that the values go linearly from -2.5 to +3. We also translate along the z-axis. By using (-10-idisp) what we do is that the objects on the left (negative idisp) are closer to us (higher z values). Notice that the sphere at the right is larger and is in front of the sphere to its left.
glTranslate 0,abs(tmove) -2,0
This variable translation generates the bouncing effect. By using abs() we change the oscillation from a sine to a 'bounce'. Then we subtract 2, so the 'floor' is not in the center, but down.
glColor ired,0.5,iblue,1
The colour is determined by two of the p-fields.
glutSphere 0.5,25,25,1
This is the instruction used to create a sphere. The prefix glut indicates it is part of the GLUT set. The parameters are: radius, number of slices, number of stacks and type of sphere. To understand the concept of slices and stacks, imagine you are looking at the globe from above, i.e. you look directly at the north pole. The number of slices is the number of parallels and the number of stacks is the number of meridians. What this in the end determines is how 'spherical' the object created is. The higher these numbers, the more spherical, but the more complex and demanding the object is for rendering. The type can be either 0 or 1, and determines whether the sphere is solid, or wireframe.
GLinsert 1.5
endin

instr 11
This second instrument will draw cubes.
ired = p4
iblue = p5
iperiod = p6
idisp = p7
glLoadIdentity
tmove GLoscil 4,p6,1
glTranslate idisp,0,-(10-idisp)
The process will be very similar to the used above, except this time, we displace -(10-idisp) on the z axis. This means that objects to the right will be closer to you. Notice how closer objects with the same size look bigger.
glTranslate 0,abs(tmove) -2,0
glColor ired,0.5,iblue,1
As with the spheres before, we make our objects 'bounce'.
GlutCube 1,1
This GLUT instruction creates a cube. glutCube has only two parameters: size and type. Size determines the length of each of the cube's sides, and type as with spheres, determines if the cube is solid or wireframe.
GLinsert 1.5
endin


</CsInstruments>
<CsScore>
In the score we draw 5 spheres (instr 10) and 5 cubes. Notice the different colouring and period of each. Also notice how they have been spaced. As usual change the values to experiment.
; 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