F.
Kenton "Doc Mojo" Musgrave
Pandromeda's MojoWorld: www.pandromeda.com
C Code for Various Textures
This is a collection of
C routines used to produce images by Ken
Musgrave appearing in:
"Texturing and Modeling:
A Procedural Approach," by David S. Ebert, ed.,
F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
Morgan Kaufmann, 2002. ISBN 1-55860-848-6.
This is the original C
texture code, on which the Larry Gritz's
RenderMan shaders that appear in the text were based. As such, it
may be more able to accurately reproduce the images in the book.
I've included this code
not so much because it's ready-to-use, as
are the RenderMan shaders, but because those shaders cannot
reproduce exactly the appearance on the images in the text.
(One major difference is that I use 256-entry color lookup tables
in my texture code, while the RenderMan shaders use color splines.
The lookup tables more naturally accommodate discontinuities in color:
To get them in the spline scheme, you need to have three consecutive,
identical entries. When you have a lot of such discontinuities, as in
the "strata" texture, a lookup table is easier.)
It's also important to
note that the RenderMan shaders are third- and
fourth-generation code. When I first develop a texture, I almost
always do it in C (in my version of the Optik ray tracer, first written
by John Amanatides and Andrew Woo at the University of Toronto in
the mid-1980's). The first-generation code is always too ugly to
show to anyone. I've learned over time to always comment my code
as I write it, and to try to keep it fairly structured. But after it's
working well enough, a second pass is always necessary to clean it
up and comment it adequately. (I rarely know exactly what I'm
doing when I'm inventing them, so it's impossible to comment them
cogently at the time.) Given all this, I figured it would be both
educational and useful to include some of the second-generation C
code here. Again, I include it more for purposes of phenomenological
observation of process, than for immediate usefulness. It'll take
some work on your part to make any of these texture routines work
in any renderer.
CAVEAT EMPTOR: "Your
mileage may vary." As all the fractals on
which these textures are based are in turn based on Ken Perlin's
"noise" function, which flavor of noise you use is critical. As the
noise function is, in turn, based on a random number generator, you
are unlikely to be able to reproduce exactly the images in the text.
But you must use what Darwin Peachey describes as "gradient noise"
in Chapter 1. Furthermore, the range of that noise function should
be [-1,1], not [0,1]. That is, all the variations of fBm used in these
textures have both positive and negative values, and an expected
value of 0.0 (statistically speaking). If you were to use other kinds
of noise functions (Darwyn describes several in Chapter 1), you'll
get very different results--very likely, unrecognizable.
In conclusion, I'd like
to say "I wish I could just give away the ray
tracer I used to make my images." But the original code belongs to
the Dynamic Graphics Project of the University of Toronto, which
does not want me to distribute it. And also, it's full of first-
generation code that I wrote, that I'd simply be mortified to show
to the world! So this is what I'm able and willing to share.