Thursday 5 September 2013

Animation Software Development


L System 




The L-System developed is the idea obtained from the book “The Algorithmic Beauty of plants”
explained by Prusinkiewicz and Lindenmayer (1990).
A Lsystem grammar has the following parameters,
G = { V,S,W,P }
where,
   V is a set of symbols containing variables, usually it consists of alphabets.
   S has a set of symbols that remain constant.
   W defines the initial state(axiom) of the system.
   P is a set of production rules that defines the predecessor and successor.

The 2D L-System makes use of the turtle graphics where the Cartesian co-ordinates (x,y) represents the
position of the turtle, while the angle alpha, also called the heading angle, represents the direction
which the turtle is facing.
The tree is generated using a predefined axiom, a set of rules and the number of generations that is
parsed from a text file. The production rules consists of only two operators +, - that helps in branching
the lines by a particular turn angle.

For 3D LSystem, the state of the turtle is defined using three axes (x,y,z). AngleU, AngleL, AngleH are the three free angles in space(Prusinkiewicz and Lindenmayer 1990). Seven operators instead of two are used such as +,-,&,^,<,>,|.
Rotation of the turtle in space is represented by :
[H’ L’ U’ ] = [ H L U ] R
These vectors have unit length and represent the orientation of the turtle in 3D space,
H indicating the turtle's heading, L the left direction and U the up direction. These vectors are
perpendicular to each other and satisfies the equation,
[H x L] = U
R is a 3x3 rotation matrix obtained about an angle(a).

The rules are stored in a text file as follows :
data1.txt
   Axiom G
   Rules
   F -> FF
   G -> F[+GL][<GL][-GL][>GL][&GL]
   Generations 2

The graphical interpretation of the symbols F and G are similar to 2D.
F and G ------ calculates the from and to position of a line based on the rotation of the turtle.
L ------- draws a leaf.
[ ------- stores the current position and angle of the turtle in stack.
] .------- retrieves the position and angle from the stack to regain a previous position.
The data1.txt file is parsed and produces the result as in below Figure


The vertex points are stored using VAO that helps to draw lines in 3D space. The lines are replaced
with cylinders to achieve a realistic 3D look. The cylinders are drawn between two points by
calculating the rotation matrices accordingly. The length of the cylinder and its radius can be modified
through the user interface.
The turn angle sets the angle of the branch. The rotation matrices are calculated based on that. The
default value is set to 25.
The leaves used are meshes modeled in Maya and imported as Obj file. The leaves have their own
properties and can be modified through the UI. The Colour of the branch and leaves can be modified as
well.
The trees are defined through the grammar. Modifying the rules would result in different trees.
For example :
data3.txt
   Axiom F
   Rules
   F -> F[-&<FL][<++&FL]||F[--&>FL][+&FL]
   Generations 2

The above text file would result as shown in below Figure.


data2.txt
   Axiom F
   Rules
   F -> F[+FL]F[-FL]F
   Generations 2
The above data file produces results as shown in below Figure. 

The user interface has the flexibility of importing user defined Obj files and use it as leaves for the
generated tree as shown above.

IMPLEMENTATION :

The program was written in C++. It runs in Linux and uses the following libraries :

  •  NGL, NCCA Graphics Library that has all the basic functions necessary for implementing 3d operations.
  • OpenGL, the portable 3D graphics standard.
  • The GLEW library is used to reduce complexity in linking the OpenGL driver binary libraryand the OpenGL code.