3-D Programming with Python

Jason scratches the surface of OpenGL programming techniques using PyOpen GL, a suite of Python modules.
Using Texture Maps

Texturing mapping is the technique of taking image data, like a photograph, and “gluing” it to a polygon. Using textures requires converting the external texture images into one of the low-level internal formats supported by OpenGL. While the code required for setting textures in Python does not differ much from normal OpenGL code, Python does simplify the process of loading and manipulating texture image files.

The Python standard library includes rgbimg, a module for reading SGI imglib(.rgb) files. While this may seem like an obscure format, its simplicity allows its inclusion in Python's standard library without causing too much bloat. Using the GIMP or ImageMagick, you can convert images from formats like PNG, JPEG or TIFF to SGI imglib. The command rgbimg.longimagedata(file) will read and decode the specified SGI imglib formatted file and return it as a binary string of four-byte RGBA pixels. This data can be passed to OpenGL functions like glTexImage2D, using the format parameter GL_RGBA and the data type GL_UNSIGNED_BYTE.

Another useful module in the Python standard library is imageop. This module includes functions for cropping, scaling and grayscale conversions for images. The imageop functions operate on data in the same GL_RGBA/GL_UNSIGNED_BYTE format as the data returned from rgbimg.longimagedata().

PyOpenGL Utility Functions

In addition to wrapping the core libraries and toolkits, PyOpenGL also includes a number of utility functions to perform high-level tasks. Have you ever wanted to grab a still image of an OpenGL scene? You always can use programs that support screen capture, such as xv or The GIMP, but what if you want to grab a still at a specific point in time? Taking screenshots manually won't work if the program needs to run without user control, like a CGI script. You also can't grab a dozen stills a second manually to create a movie. PyOpenGL has a couple helper functions for automatically grabbing scene shots in various formats.

Listing 3 is an adaptation of the previous program that saves a PostScript image of the scene to disk, then exits. You can take a look at the image that was saved in Figure 1.

Listing 3. Saving a PostScript Image, Then Exiting

Figure 1. Results from Listing 3

The scene is saved using the command

openglutil.glSaveEPS('ex3.eps', 240, 240)

The first argument is the name of the file to save to. The second is the width in pixels to capture, and the third is the height. Using the same syntax with the glSavePPM() command will save an image of the scene in the portable pixmap file format. Additionally, if you have compiled PyOpenGL with TIFF support, the command glSaveTIFF() is available for saving scene shots in the TIFF format.

PyOpenGL also provides a convenience wrapper for OpenGL's object selection or “picking” mechanism. The selection mode of operation in OpenGL automatically tells you which objects fall within a given region of the view screen. This means you can pass the selection mechanism to a point on the screen where a mouse click occurred, and it will tell you what object was clicked on.

Here are the basic steps for setting up the selection mechanism in PyOpenGL:

  1. Write a method or function that draws each object in the scene wrapped in between a call to glPushName() and glPopName(). Pass a different integer as an argument to glPushName() for each independent object in the scene. OpenGL will use this value to indicate which objects were selected.

  2. Set up the event handler for mouse clicks. The way this works depends on the toolkit you are using. In Tkinter, mouse-click event handlers are set up by passing a method or function reference to the bind() method.

  3. Once a mouse click has occurred, pass the mouse x and y coordinates, along with a reference to the method or function created in part one, to the function OpenGL.GL.glSelectWithCallback(). This function will return a list of tuples of the form (near, far, names) where near and far represent integer depth values, and names is a tuple of the names passed in glPushName() for the selected objects. If no objects were selected, an empty tuple is returned.

Listing 4 demonstrates use of the selection mechanism in PyOpenGL. This demo draws a cube and a sphere on the screen. Experiment with holding down the Ctrl key and pressing the left mouse button with the pointer in different places.

Listing 4. The Selection Mechanism in PyOpenGL

Figure 2. Results from Listing 4

The fog.py Demo Included with PyOpenGL

A Simple Scene with Lighting and Textures Rendered Using PyOpenGL

A 3DStudio Model Rendered Using PyOpenGL

The selection mechanism is the simplest way to perform this task, but it is by no means the only way. Another technique is to render the objects to a back buffer that OpenGL does not send to an output device. To do this, render each object using a different color. Once done, the back buffer should contain the 2-D rendering of the scene. By examining the color at a particular point you can determine which object was drawn in that region of the screen. This back buffer is called the feedback buffer in OpenGL.

Yet another method for object picking is to do the intersection testing by hand. This means you would have to project a ray from the view screen and test each object in the scene for intersection. Once you have found a list of intersected objects, you then must sort by depth values to see which was closest to the view screen. While this may offer more control over the process, it is an arduous task and is difficult to do efficiently in Python.



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 ...