274 TUGboat, Volume 18 (1997), No. 4
Graphics Applications
Creating 3D animations with
METAPOST
Denis Roegel
Abstract
METAPOST can be used to create animations. We
show here an example of animation of polyhedra,
introducing the 3d package.
Introduction
METAPOST (Hobby (1992); see also the description
in Goossens et al. (1997)) is a drawing language very
similar to METAFONT, but whose output is Post-
Script. METAPOST is especially suited for geomet-
rical and technical drawings, where a drawing can
naturally be decomposed in several parts, related in
some logical way. Knuth is using METAPOST for the
revisions of and additions to The Art Of Computer
Programming (Knuth, 1997), and it is or will be a
component of every standard T
E
X distribution.
Unfortunately, METAPOST is still quite bare
and the user is only offered the raw power — a little
bit like the T
E
X user who only has plain T
E
Xat
his/her disposal. The lack of libraries is certainly
due to the infancy of METAPOST (which came in
the public domain at the beginning of 1995) and
thus to the small number of its users.
In this paper, we present a way to produce
animations using METAPOST. The technique is
quite general and we illustrate it through the 3d
package.
Animations
The World Wide Web has accustomed us to various
animations, especially java animations. Common
components of web pages are animated GIF images.
Producing animations in METAPOST is actu-
ally quite easy. A number of n images will be
computed and their sequence produces the anima-
tion. The animation will be similar to a movie,
with no interaction. More precisely, if an_image(i)
produces a picture parameterized by i, it suffices to
wrap this macro between beginfig and endfig:
def one_image_out(expr i)=
beginfig(<figure number>);
an_image(i);
endfig;
enddef;
andtoloopoverone_image_out:
for j:=1 upto 100:one_image_out(j);endfor;
Assuming that <figure number> is equal to
the parameter of an_image, the compilation of this
program will produce a hundred files with extensions
.1, .2, ..., .100. All these files are PostScript files
and all we need to do is to find a way to collate
them in one piece. How to do this depends on the
operating system. On UNIX for instance, one can
use Ghostscript to transform a PostScript file into
ppm and then transform each ppm file into GIF with
ppmtogif. These programs are part of the NETPBM
package (Davidsen, 1993). Finally, a program such
as gifmerge (M¨uller, 1996) creates an animated
GIF file (GIF89A) out of the hundred individual
simple GIFs. However, various details must be taken
care of. For instance, only a part of Ghostscript’s
output is needed and selection can be made with
pnmcut.
The whole process of creating an animation
out of METAPOST’s outputs can be summed up in
a shell script, similar to the one in figure 1. As
we will see, this script (including the arguments of
awk and pnmcut) can be generated automatically by
METAPOST itself.
Objects in space
Introduction The author applied this idea to the
animation of objects in space. The macros in the
3d.mp package
1
provide a basis for the representa-
tion of three-dimensional objects. The basic com-
ponents of the objects are the points or the vectors.
Both are stored as triplets. More precisely, we have
three arrays
2
of type numeric:
numeric vect[]x,vect[]y,vect[]z;
Vector i’s components are vect[i]x, vect[i]y
and vect[i]z. It is then straightforward to define
the usual operations on vectors using this conven-
tion. For instance, vector addition is defined as:
def vect_sum(expr k,i,j)=
vect[k]x:=vect[i]x+vect[j]x;
vect[k]y:=vect[i]y+vect[j]y;
vect[k]z:=vect[i]z+vect[j]z;
enddef;
1
On CTAN, under graphics/metapost/macros/3d.The
code is documented with MFT (Knuth, 1989) and illustrated
with METAPOST. This paper describes version 1.0 of the
macros.
2
METAPOST has a few simple types such as numeric,
boolean, string, path, . . . . It also has pairs (pair)and
triples (color). We might have cheated and stored points
as colors, but instead, we found it interesting to illustrate a
construction equivalent to Pa sc al ’s records or C’s structures.
In METAPOST, instead of having a list or an array of
structures, we use several lists or arrays, so that a record
is a cross-section over several arrays.