3-D Programming with Python

Jason scratches the surface of OpenGL programming techniques using PyOpen GL, a suite of Python modules.
Improving Performance

Now that you have seen how to write simple OpenGL programs, you are probably wondering if Python can scale up to the demands of more advanced 3-D applications. While the performance of a PyOpenGL program generally lags behind that of its C or C++ counterpart, optimization techniques can narrow the gap considerably.

The main strategy in improving execution speed is to reduce the amount of time spent within the Python interpreter by moving expensive operations into native code. One means of accomplishing this is to rewrite sluggish parts of the program in a fast, natively compiled language like C or C++. Implementing these compiled portions of the program as Python extension modules allows the remaining interpreted Python code to access their functionality. While this approach certainly has potential for speeding things up, it lacks the simplicity of a pure Python solution. It also requires you understand how to write Python extension modules in a language that compiles to native code. Besides, if you wanted to do it in C, you wouldn't have started messing around with Python in the first place!

OpenGL display lists provide a way to move operations into native code without any of the headaches associated with the former approach of writing extension modules. Display lists allow OpenGL programs to cache a set of commands further down in the rendering pipeline. In some environments, OpenGL even can store display lists on the graphics card itself, far away from the bottleneck of the Python interpreter.

The glGenLists() command creates an array of empty display lists. It takes a single integer argument, the number of display lists, to generate. It returns the number of lists that were successfully created. Wrapping a set of OpenGL operations with the commands glNewList() and glEndList() fills a specified display list. Once stored, subsequent invocations of that set of operations requires only a single command, glCallList(). The syntax for using display lists in PyOpenGL is pretty much the same as in OpenGL with C.

Next Steps

We have just begun to scratch the surface of OpenGL programming techniques in Python. For more information, make sure to check out the documentation that comes with PyOpenGL or on-line at pyopengl.sourceforge.net/documentation/index.html.


Jason Petrone (jpetrone@acm.org) is a member of the technical staff at the Corporation for National Research Initiatives (CNRI) in Reston, Virginia. He first discovered the powerful combination of OpenGL and Python while working on a CAVE virtual reality project at his former place of employment, the National Center for Supercomputing Applications (NCSA).



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Solution for Python 2.5.2

mj's picture

Just Download TOGL 2.0 and uncompress it on TCL sub directory in Python Installation directory.
now it included in Python TK TCL auto_path and can be used in TKinter
i tested it with sample code.
i using XP , Python 2.5.2


Anonymous's picture

You say :
While GLUT is suitable for most small OpenGL applications, it still requires a fair amount of work to implement functionality that is often desirable when testing, such as mouse control for zooming, panning and rotation. Togl is a Tkinter widget that automatically provides these features as well as default lighting.

but Mike C. Fletcher said:

Togl refuses to compile with the version of Tk distributed with Python 2.3 and above[...] and so it's being dropped from PyOpenGL. You can rebuild PyOpenGL 2.0.1 with Togl support on Unix, but unless you really need to run old scripts which used it, it's probably not worth the effort (of setting the flag to true).

In theory, if you can compile (or find compiled) Togl for Tk 8.4 (and then install it in the Python Tk instance), it should run fine. I, however, have burned far too much PyOpenGL development time on it (given that Tk is hardly the cutting edge in GUIs these days), so I'm not likely to work on it any more.


Cata Fest's picture

togl !!
Most people use python 2.3 , 2.4 or 2.5
I use pyhon 2.2 with PyOpenGL ...