Game Development Reference
In-Depth Information
Basic rendering and lighting
We will now construct a simple object from the primitive shapes (triangles), and ex-
plore how OpenGL's built-in lighting system can help us to visualize our object in three
dimensions.
Note
Continue from here using the
Chapter2.2_BasicRenderingAndLighting
project files.
Creating a simple box
The
glBegin()
and
glEnd()
functions are the two important OpenGL commands
that work together to define the starting and ending points (known as
delimiters
) for
the construction of a primitive shape. The
glBegin()
function requires a single ar-
gument that specifies the type of primitive to render. This determines whether the ver-
tices we input represent points, lines, triangles, quads, or whatever the renderer sup-
ports. We'll be using
GL_TRIANGLES
for our box, each of which requires three unique
vertices in space in order to render.
There are a variety of commands that can be called between
glBegin()
and
glEnd()
to build the primitive, but the two commands that we will be using are
glVertex3f()
, which defines the position of a vertex in space, and
glColor3f()
which sets the color of subsequent vertices using the same RGB system that we saw
in the previous chapter (note that it does not have an alpha value).
The actual task of rendering the box happens in the
DrawBox()
function of the
chapter's source code. The most important part is as follows:
static int indices[36] = {
0,1,2, 3,2,1, 4,0,6, 6,0,2, 5,1,4, 4,1,0,
7,3,1, 7,1,5, 5,4,7,7,4,6, 7,2,3, 7,6,2};
glBegin (GL_TRIANGLES);
for (int i = 0; i < 36; i += 3) {
Search WWH ::
Custom Search