Game Development Reference
In-Depth Information
Let there be light!
At this stage, we can see our box, but all of its faces have exactly the same coloring,
which makes it a little difficult to determine the exact shape as it moves around in
space. OpenGL has some basic built-in lighting functionality, which we will make use
of.
Normals
Normals represent the direction pointing away from a given surface or point. They are
used in a variety of useful techniques (and not just lighting!), and the most basic of
which is simple diffuse lighting or lighting an object based on the angle between the
light source and the surface. Our lighting system will use each point's normal to de-
cide in which direction the incoming light should reflect away from that surface helping
it calculate the overall color of the polygon.
Setting the normal for a given vertex can be accomplished by calling the
glNormal3f() function. This function sets the normal for the subsequent vertices,
which could be more than one in the case where they all share the same normal value
until glNormal3f() is called again. For the record, glColor3f() functions in the
same way. The renderer assumes that you're using the same color and normal for
each new vertex until you specify otherwise.
The normal can be calculated fairly easily by performing a cross-product on the three
vertices that make up the triangle. If we remember our 3D mathematics, this gives us
a vector perpendicular to the vectors of all three vertices. The cross product is non-
commutative, so the output vector could either point inwards or outwards from the sur-
face, depending on what order we performed the cross product, but fixing it is simply
a matter of multiplying it by -1 .
Creating ambient, diffuse, and specular lighting
There are three basic lighting effects that were some of the earliest lighting effects
produced in 3D graphics and are still used to today to simulate basic and cheap light-
ing effects in a 3D environment.
Search WWH ::




Custom Search