Animating with OpenGL

The motivation to use OpenGL stems from limitations of CPU processing power when we are faced with the task of visualizing millions of data points and doing it fast (sometimes even in real time).

Modern computers have powerful GPUs that are made for fast visualization-related computations (such as games), and there is no reason why they can't be used for science-related visualizations.

Actually, there is at least one drawback of writing hardware-accelerated software that is hardware dependent. Modern graphics cards require proprietary drivers which are sometimes not available on the target platform/machine (the user's laptop, for example). Even when available, sometimes installing the required dependencies on-site is not what you want to spend your time on, while all you want is to present your findings and demonstrate your research results. This is not a showstopper but you should bear this in mind, and measure the benefits and costs of introducing this complexity in your project.

With the caveats explained, we can say yes to hardware-accelerated visualizations and to OpenGL, which is the industry standard for accelerated graphics.

We will be using OpenGL as it is cross-platform, so the examples should work as presented on Linux, Mac, or Windows, provided you have the required hardware and OS-level drivers installed.

Getting ready

If you have never used OpenGL, we will now try to give a quick introduction, although to really understand OpenGL, at least one complete book needs to be read. OpenGL is a specification, not an implementation, so OpenGL itself doesn't have any code, while the implementations are libraries developed according to this specification. These are shipped with your operating system or by vendors of graphics cards such as NVidia or AMD/ATI.

Moreover, OpenGL is concerned only with graphics rendering and not animation, timing, or other "complex" things that are left for additional libraries to pick up.

Note

Basics of animating with OpenGL

Since OpenGL is a rendering library, it does not know what objects we draw on a screen. It doesn't care if we draw a cat, a ball, a line, or all of these objects. So, to move a rendered object, we need to clear and draw the whole image again. To animate something, we need a loop that draws and redraws everything very quickly and displays it to a user, so that the user thinks he/she sees an animation.

Installing OpenGL on a machine is a platform-dependent process. On the Mac OS X, OpenGL implementation is part of the OS upgrade, but development libraries (so called headers) are part of the Xcode development package.

On Windows, the best way would be to install the vendor's latest graphics drivers for your graphic card. OpenGL may work without them, but you will probably be left without the latest features with stock drivers.

On Linux, if you are not against installing closed source software, there are vendor-specific drivers downloadable either from the distro's own software manager or from the vendor site as an installable binary. Standard implementations are almost always Mesa3D—the best known OpenGL implementation, which uses Xorg to provide support for OpenGL for Linux, FreeBSD, and similar operating systems.

On Debian/Ubuntu, you should install the following packages and their dependencies:

$ sudo apt-get install libgl1-mesa-dev libgl-mesa-dri

After this, you should be ready to use some development libraries and/or frameworks to actually write OpenGL-backed applications.

We are focused here on Python, so we will overview some of the Python's most used libraries and frameworks that are built on top of OpenGL. We will mention matplotlib and its current and future support for OpenGL:

  • Mayavi: This is a library specialized for 3D
  • Pyglet: This is a pure Python library for graphics
  • Glumpy: This is a fast rendering library built on top of NumPy

How to do it...

Specialized project Mayavi is a full-featured 3D graphics library, which is mainly used for advanced 3D rendering. It comes with already mentioned Python packages like EPD (though not with a free license), which is a recommended way of installing it on Windows and Mac OS X. On Linux, it can also be easily installed using pip:

$ pip install mayavi

Mayavi can be used as a development library/framework or as an application. The Mayavi application comprises a visual editor for easy data exploration and somewhat interactive visualization.

As a library, it can be used in a similar way to matplotlib—either from the script interface or as a full object-oriented library. Most of that interface is inside the mlab module, to be able to use that interface. For example, simple animation with Mayavi can be done as follows:

import numpy
from mayavi.mlab import * 

# Produce some nice data. 
n_mer, n_long = 6, 11 
pi = numpy.pi 
dphi = pi/1000.0 
phi = numpy.arange(0.0, 2*pi + 0.5*dphi, dphi, 'd') 
mu = phi*n_mer 
x = numpy.cos(mu)*(1+numpy.cos(n_long*mu/n_mer)*0.5) 
y = numpy.sin(mu)*(1+numpy.cos(n_long*mu/n_mer)*0.5) 
z = numpy.sin(n_long*mu/n_mer)*0.5 

# View it. 
l = plot3d(x, y, z, numpy.sin(mu), tube_radius=0.025, colormap='Spectral') 

# Now animate the data. 
ms = l.mlab_source 
for i in range(100): 
    x = numpy.cos(mu)*(1+numpy.cos(n_long*mu/n_mer + 
                                      numpy.pi*(i+1)/5.)*0.5) 
    scalars = numpy.sin(mu + numpy.pi*(i+1)/5) 
    ms.set(x=x, scalars=scalars)

This code will produce the following window with rotating figure:

How to do it...

How it works...

We generate a dataset and create set of functions for x, y, and z to be used in the plot3d function for start position of the figure.

We then import the mlab_source object that enables us to manipulate our plot on the level of points and scalars. We then use this feature to set particular points and scalars in for loop to create a rotation animation with 100 frames.

There's more...

If you want to experiment more, the easiest way to do so is to load IPython, import mayavi.mlab, and run some test_* functions.

To see what is going on, you can use IPython's ability to inspect and explore Python source, as follows:

In [1]: import mayavi.mlab 

In [2]: mayavi.mlab.test_simple_surf?? 
Type:       function 
String Form:<function test_simple_surf at 0x641b410> 
File:       /usr/lib/python2.7/dist-packages/mayavi/tools/helper_functions.py 
Definition: mayavi.mlab.test_simple_surf() 
Source: 
def test_simple_surf(): 
    """Test Surf with a simple collection of points.""" 
    x, y = numpy.mgrid[0:3:1,0:3:1] 
    return surf(x, y, numpy.asarray(x, 'd')) 

We can see here how, by adding two question marks after the function name ("??"), IPython found the source of the function and showed it to us. This is true exploratory computing, and is often used within the visualization community, because it is a fast way to get to know your data and code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.170.134