Navigation menu |
A portable, open-source, coherent noise-generating library for C++ |
|||||||||||||||||||||||||
Example: Perlin wormsNormally, libnoise is used to generate procedural textures and terrain. This page demonstrates an unusual application of libnoise to render and animate worm-like creatures in real time. To see this program in action, download the example sources (43 KB) and compile worms.cpp. To run this program, you'll also need OpenGL 1.1 (or better), and GLUT, a windowing toolkit for OpenGL. The worms source code is released under the terms of the GNU General Public License. Keyboard Controls
How this program worksRendering the wormsThis program creates a worm object for each worm to render. Each object is instantiated from a Worm class with the following properties:
A worm is divided into several worm segments of constant length. A worm object generates coherent-noise values from its Perlin-noise module to determine the angles of each segment. It generates these values along its lookup line segment. Refer to the following diagram:
Note that this line segment is parallel to the x axis. Referring to the above diagram, the coherent-noise values generated along this line segment look like this:
A worm object divides this line segment into n discrete values, where n is the number of worm segments. It sets the angle of its head segment using the coherent-noise value from the leftmost position of the line segment. It sets the angle of the next worm segment using the value from the next position, and so on.
A worm object draws each worm segment off of the previous worm segment, forming a chain of worm segments. Refer to the following diagram.
In the above diagram, the segment with the block is the head segment. Animating the WormsAfter a worm object renders itself, it moves its lookup line segment by a small constant amount in the x, y, and z directions. Because these coherent-noise values are very similar to the previous values, the worm will slightly change its position on the next frame. The following list of images show three frames of the worm. After each frame, the lookup line segment moves in the positive y direction:
Once again, the segment with the block is the head segment. Moving the WormsAfter each frame, the worm object moves its head segment by a short distance along a vector with an angle opposite the angle of the head segment. Also, it shifts its lookup line segment a short distance in the negative x direction, which propagates the previous coherent-noise values over to subsequent worm segments. This causes the worms to exhibit a "slithering" effect. |
||||||||||||||||||||||||||
© 2003-2005 Jason Bevins |