Initializing OpenGL Features

The resize function is enough to start using OpenGL functions to render to the screen, but there are a few other things we should set to make it more interesting (see Listing 9-5).

Figure 9-6. The viewing frustum

Listing 9-5. Initializing OpenGL def init():






The first thing that the init function does is call glEnable with GL_DEPTH_TEST, which tells OpenGL to enable the Z buffer. This ensures that objects that are far from the camera aren't drawn over objects that are near to the camera, regardless of the order that we draw them in code.

The glEnable function is used to enable OpenGL features, and glDisable is used to disable them. Both functions take one of a number of uppercase constants, beginning with GL_. We will cover a number of these that you can use in your games in this book, but see the OpenGL documentation for a complete list.

The second line in init sets the clear color, which is the color of the parts of the screen that aren't drawn to (the equivalent of automatically calling screen.fill in the 2D sample code). In OpenGL, colors are given as four values for the red, green, blue, and alpha components, but rather than a value between 0 and 255, it uses values between 0 and 1.

The remaining lines in the function initialize OpenGL's lighting capabilities, which automatically shades 3D objects depending on the position of a number of lights in the 3D world. The call to glShadeModel sets the shade model to GL_FLAT, which is used to shade faceted objects like cubes, or anything with edged surfaces. An alternative setting for the shade model is GL_SMOOTH, which is better for shading curved objects. The call to glEnable(GL_COLOR_MATERIAL) tells OpenGL that we want to enable materials, which are settings that define how a surface interacts with a light source. For instance, we could make a sphere appear highly polished like marble, or softer like a piece of fruit, by adjusting its material properties.

The remaining portion of Listing 9-5 enables lighting (glEnable(GL_LIGHTING)) and light zero (glEnable(GL_LIGHT0)). There are a number of different lights that you can switch on in OpenGL; they are numbered GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, and so on. In a game, you would have at least one light (probably for the sun), and additional lights for other things such as headlights, lamps, or special effects. Placing a light source inside a fireball effect, for example, will ensure that it illuminates the surrounding terrain.

The last line sets the position of light zero to (0, 1, 1, 0). The first three values in this tuple are the x, y, and z coordinates of the light; the last value tells OpenGL to make it a directional light, which creates a light source with parallel light rays, similar to the sun. If the last value is 1, OpenGL creates a point light source, which looks like a close-up light, such as a bulb, candle, or plasma fireball. See Figure 9-7 for the differences between point and directional light sources.

Was this article helpful?

0 0

Post a comment