Write fast 3D software without a PhD

  • June 24, 2008
  • Avatar for jacob
    Jacob
    Peddicord

There are two kinds of fast in programming: Fast to code and fast to run. 3D applications and games are known to be speedy. But the time it takes to write and understand the code behind it? Not so much. On the flipside, many abstraction layers designed to make coding easier usually aren't very efficient. So where's the median?

Meet Pyglet for Python. Pyglet, as described by their website is "a cross-platform windowing and multimedia library for Python." While it is certainly that, there is so much more that Pyglet does. The key focus of Pyglet? OpenGL.

With many windowing libraries, just opening a window is complex, let alone one using OpenGL. With Pyglet, it is just one line:

window = pyglet.window.Window()

That simple method will open a fully-initialized OpenGL window. You don't have to accept the defaults, so alternate properties could be specified, such as a window caption and size.

Pyglet is also purely event driven, meaning that all events (keyboard, mouse, window, etc) are caught and sent to your application functions. In fact, only two more lines are needed to make the above code fully functional: an import statement and a run(). Of course, your application will just sit open with an empty window, but hey, it's a start!

One of the great things I like about Pyglet is that it allows a seasoned OpenGL programmer to still work directly with it. If you want to go beyond what Pyglet provides and use glTranslatef(), you can. As long as you have pyglet.gl loaded, you can use OpenGL inline with the rest of your code:

glLoadIdentity()
glTranslatef(10.0, 20.0, 0)
batch.draw()

The only downside to this? If you want to write true 3D software, you'll still need to know OpenGL. Maybe that's an upside to some. Pyglet is not (yet) able to handle 3D on its own, meaning that if you want to write an application using only Pyglet functions, you're restricted to a 2D plane or an orthographic projection.

That said, there are a plethora of features included that all are useful in some way. From a two-line framerate display and limiter and time scheduling, to full text and media support, almost everything needed for a game-development environment is there.

Both Python and OpenGL are known for having extremely well-written documentation. Since Pyglet is a little bit of both, it should have good documentation as well, right? Actually, it really does. The level of documentation and examples provided with Pyglet is enough that you could sit on a plane for five hours with only a text editor and documentation and program without ever needing to look something up online. Even the yet-to-be-released Pyglet 1.1 already has an phenomenal amount of provided documentation.

If you're looking to start a new Python project or game but just didn't know where to begin, Pyglet is your building ground. I'll personally admit: In one of my projects, I have switched between programming languages four times before finally finding Pyglet. I rewrote the entire application in less than a week, whereas it took a few months to develop the other versions.

Give it a try, even if you're not into graphics or games. You'll love it.

Avatar for jacob Jacob Peddicord

Home » Articles »