Main Docs Examples Download Gallery

Lua Integration

Included in the src/tolua_binding is a file, bind_anl.cpp that is a tolua++-generated binding file for ANL. Include it in your Lua/tolua++ project, and call the function int tolua_bind_anl_open (lua_State* tolua_S) to enable access to the functions of the library from Lua. In the lua in the root directory of the project you will find the file noisemoduletree.lua. This file implements a class, NoiseModuleTree that I wrote in order to facilitate the construction of trees of functions. It allows one to write a declaration as a table of tables, that describes the modules that are required, and links them together. The table can be parsed by the constructor for NoiseModuleTree and the result is a chain of instances noise functions as describe. As an example:

	{name="sample_fractal", type="fractal", fractaltype=anl.FBM, basistype=anl.GRADIENT, interptype=anl.QUINTIC, octaves=8, frequency=2},
	{name="sample_remaprange", type="scaleoffset", source="sample_fractal", scale=0.25, offset=0.75},
	{name="sample_sphere", type="sphere", radius="sample_remaprange"}

In this simple example, we describe a short chain that starts with a Fractal module, which is chained to a ScaleOffset module that remaps it's range from (-1,1) to (0.5,1.0). This is then chained to a Sphere function, being set as the radius of the sphere. (In ANL, many parameters such as radius can be set as either a constant or as another noise source, enabling some very interesting effects.

The above descriptor can be parsed into an actual module tree, from which values may be extracted:

require 'noisemoduletree'
local st=NoiseModuleTree(sampletree)

st:setSeedAll(43)   -- Will iterate the chain, setting seeds to all seedable functions. Seed is incremented each time, so no two modules get the same seed
local output=st:getFunction("sample_sphere")  -- Get a reference to the function named "sample_sphere"

print(output:get(0.1,0.3,0.5))   -- Call sample sphere

local buf=CArray2Dd(256,256)    -- Allocate a utility buffer size 256x256
ranges=anl.SMappingRanges()     -- Setup a structure containing mapping ranges

anl.map2D(anl.SEAMLESS_NONE, buf, output, ranges,0)
anl.saveDoubleArray("array.tga", a)

Here, we instance NoiseModuleTree with sampletree in the constructor. This creates a class object that holds a chain of modules, all apropriately chained together as described. You can seed all the modules via setSeedAll(), and obtain a reference to any module by passing its name to getFunction(). The utility functionality of ANL provides access to helper functions to map buffers of data in 2D or 3D, with any combination of seamless modes. Seamless in 1D, seamless in 2D, seamless in 3D, etc... It also provides functionality to save buffers of data (either double-format or RGBA format) to .TGA files for viewing.

(Note: The utility stuff presently stands as the area I am focusing on for improvements. Likely, it'll be separated out and better encapsulated in a future version.)

All content on page copyright 2011 Joshua Tippetts