Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Game Programming By Example

You're reading from   Python Game Programming By Example A pragmatic guide for developing your own games with Python

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785281532
Length 230 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime