3-D Programming with Python
Graphics programming can be tedious. Linking against large 3-D libraties increases compilation time. Because a lot of fine tuning is often necessary for everything to look perfect, stretches of minor changes buried between long builds are commonly encountered. These lengthy debug cycles make 3-D graphics an ideal application for prototyping in a high-level language like Python.
Extensions to a number of 3-D graphics APIs are available for Python. For IRIX systems, the Python distribution comes with a module providing access to the SGI IRIS GL library. Python programs can make use of the Java3D API from inside JPython, an implementation of Python that runs inside a Java Virtual Machine. This article focuses on the OpenGL library because of its widespread use and excellent support for Linux and Python.
PyOpenGL is a suite of Python modules that provides access to OpenGL as well as an assortment of helper utilities and extensions to complement OpenGL's low-level interface. It was originally created by James Hugunin, Thomas Schwaller and David Ascher. Tarn Burton recently has taken over as lead developer, and Rene Liebscher and Michael Fletcher also maintain the package.
Since OpenGL wrappers make up the bulk of PyOpenGL's functionality, you will need a basic understanding of OpenGL to write programs with it. There are many excellent tutorials and references available on OpenGL, see Resources for a list of recommendations.
The first requirement for PyOpenGL is OpenGL itself. If you don't have an OpenGL implementation installed already, check your GNU/Linux distribution to see if it includes the packages, or download the Mesa 3-D graphics library from www.mesa.org. For PyOpenGL to work at full capacity, the module Numerical Python must be installed. Sources for Numeric and PyOpenGL can be found at numpy.sourceforge.net and pyopengl.sourceforge.net, respectively. Compilation and installation is easy thanks to Greg Ward's distutils module, which is included in Python as of version 1.6. Running the command python setup.py install from inside the unpacked source directories should build and install the modules. Before installing from source, you may want to check if your GNU/Linux distribution already provides these modules. They were included as part of my Debian distribution. Note: the version I worked with is PyOpenGL 1.5.7, since the time of this writing, version 2.0 has become available.
The OpenGL specification does not define specifications for interaction with windowing systems. Consequently, programs using OpenGL must use an external GUI toolkit. The program in Listing 1 uses GLUT, a cross-platform windowing toolkit for OpenGL. Unless you are using a commercial OpenGL implementation, you probably already have GLUT installed.
This code opens a window, sets up lighting and draws a teapot. Aside from the added syntactic compactness Python affords, it looks much like the equivalent program written in C. One minor difference is how the display function callback is set. Setting the display function callback in C or C++ would only require calling the function glutDisplayFunc(display). Setting the callback in PyOpenGL is done in two steps: invoking glutSetDisplayFunc() and then glutDisplayFunc(). This idiosyncrasy also applies for setting other callbacks such as glutMouseFunc() and glutReshapeFunc().
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. Listing 2 shows the same program using Togl.
Notice it uses considerably less code but provides much more functionality. The cost of this is flexibility. If Togl's default lighting and user interface don't meet your requirements, you will need to re-implement them yourself. Togl is excellent for prototyping, as it eliminates the need to write and debug boilerplate lighting and navigation code.
PyOpenGL also integrates well with other GUI toolkits that have 3-D widgets. Bindings exist for wxWindows, FLTK, FOX and GTK.
|Understanding OpenStack's Success||Feb 21, 2017|
|Natalie Rusk's Scratch Coding Cards (No Starch Press)||Feb 17, 2017|
|Own Your DNS Data||Feb 16, 2017|
|IGEL Universal Desktop Converter||Feb 15, 2017|
|Simple Server Hardening||Feb 14, 2017|
|Server Technology's HDOT Alt-Phase Switched POPS PDU||Feb 13, 2017|
- Understanding OpenStack's Success
- Own Your DNS Data
- Simple Server Hardening
- Understanding Firewalld in Multi-Zone Configurations
- Teradici's Cloud Access Platform: "Plug & Play" Cloud for the Enterprise
- From vs. to + for Microsoft and Linux
- Returning Values from Bash Functions
- Tech Tip: Really Simple HTTP Server with Python
- Bash Shell Script: Building a Better March Madness Bracket
- Natalie Rusk's Scratch Coding Cards (No Starch Press)