Book Image

Python Game Programming By Example

Book Image

Python Game Programming By Example

Overview of this book

Table of Contents (14 chapters)
Python Game Programming By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Drawing with OpenGL


Until now, we have always rendered our objects with a utility routine, but most OpenGL applications require the use of some drawing primitives.

The Cube class

We will define a new class to render cubes, and we will use the following representation to better understand the vertices' positions. From 0 to 7, the vertices are enumerated and represented in a 3D space.

The sides of a cube can now be represented as tuples: the back face is (0, 1, 2, 3), the right face is (4, 5, 1, 0), and so on.

Note that we arrange the vertices in counterclockwise order. As we will learn later, this will help us enable an optimization called face culling, which consists of drawing only the visible faces of a polygon:

class Cube(object):
    sides = ((0,1,2,3), (3,2,7,6), (6,7,5,4),
             (4,5,1,0), (1,5,7,2), (4,0,3,6))

The __init__ method will store the values of the position and color, as well as the vertex coordinates with respect to the center position of the cube:

    def __init__(self...