L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and...

5
L-Systems Himanshu Chablani December 11, 2014 Maths and Graphics for Computer Games, Goldsmiths, University of London 1 Introduction to L-Systems An L-system or Lindenmayer system is a parallel rewriting system and a type of formal grammar. An L- system consists of an alphabet of symbols that can be used to make strings, a collection of production rules that expand each symbol into some larger string of symbols, an initial "axiom" string from which to begin construction, and a mechanism for translating the generated strings into geometric structures. L-systems were introduced and developed in 1968 by Aristid Lindenmayer, a Hungarian theoretical biologist and botanist at the University of Utrecht. Lindenmayer used L-systems to describe the behaviour of plant cells and to model the growth processes of plant development. L-systems have also been used to model the morphology of a variety of organisms and can be used to generate self-similar fractals such as iterated function systems. The recursive nature of the L-system rules leads to self-similarity and thereby, fractal- like forms are easy to describe with an L-system. Plant models and natural-looking organic forms are easy to define, as by increasing the recursion level the form slowly ’grows’ and becomes more complex. Lindenmayer systems are also popular in the generation of artificial life. Figure 1: Example of L-System 2 My Implementation I have used the Octet framework developed by Andy Thomason which uses OpenGL-ES2.0. In my implementation the L-System rules are first read from a Text file called L_System_Rules.txt. In the Text file the rules are written in a fixed manner so the code can extract data correctly. Example : S:F B:F R:F>F[+F]F[-F]F A:25.7 //// ’S’ represents the variable if there are more than one variables then they are written in the next line with S: before them. ’B’ represents the axiom and ’A’ is the angle of rotation. ’R’ represents the production rule for the L-System and it must be written in this manner Variable’>’Production Rule, if there are more than one rules then they are written in the next line with ’R:’ before them. In the end the rule is terminated by a ’/’. The next part of implementation is extracting the demanded L-System. The user can press F1 - F8 to get the respected L-System (F1 for first L-System and F8 for the eighth L-System). The code searches the file for the particular L-System and then the variables, axiom, production rules and the angle are loaded into the code. I have used char * for storing these in code because they easy to manage and control. 1

Transcript of L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and...

Page 1: L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and Graphics for Computer Games, Goldsmiths, University of London 1 Introduction to L-Systems

L-Systems

Himanshu Chablani

December 11, 2014

Maths and Graphics for Computer Games, Goldsmiths, University of London

1 Introduction to L-SystemsAn L-system or Lindenmayer system is a parallel rewriting system and a type of formal grammar. An L-system consists of an alphabet of symbols that can be used to make strings, a collection of production rulesthat expand each symbol into some larger string of symbols, an initial "axiom" string from which to beginconstruction, and a mechanism for translating the generated strings into geometric structures. L-systemswere introduced and developed in 1968 by Aristid Lindenmayer, a Hungarian theoretical biologist andbotanist at the University of Utrecht. Lindenmayer used L-systems to describe the behaviour of plantcells and to model the growth processes of plant development. L-systems have also been used to modelthe morphology of a variety of organisms and can be used to generate self-similar fractals such as iteratedfunction systems. The recursive nature of the L-system rules leads to self-similarity and thereby, fractal-like forms are easy to describe with an L-system. Plant models and natural-looking organic forms areeasy to define, as by increasing the recursion level the form slowly ’grows’ and becomes more complex.Lindenmayer systems are also popular in the generation of artificial life.

Figure 1: Example of L-System

2 My ImplementationI have used the Octet framework developed by Andy Thomason which uses OpenGL-ES2.0. In myimplementation the L-System rules are first read from a Text file called L_System_Rules.txt. In the Textfile the rules are written in a fixed manner so the code can extract data correctly.

Example :S:F B:FR:F>F[+F]F[-F]FA:25.7////’S’ represents the variable if there are more than one variables then they are written in the next

line with S: before them. ’B’ represents the axiom and ’A’ is the angle of rotation. ’R’ representsthe production rule for the L-System and it must be written in this manner Variable’>’Production Rule, ifthere are more than one rules then they are written in the next line with ’R:’ before them. In the end therule is terminated by a ’/’.

The next part of implementation is extracting the demanded L-System. The user can press F1 - F8 toget the respected L-System (F1 for first L-System and F8 for the eighth L-System). The code searches thefile for the particular L-System and then the variables, axiom, production rules and the angle are loadedinto the code. I have used char * for storing these in code because they easy to manage and control.

1

Page 2: L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and Graphics for Computer Games, Goldsmiths, University of London 1 Introduction to L-Systems

Once the L-System is loaded it can be iterated to produce the parameter for the L-System. The user canpress Left Mouse Button for next iteration and Right Mouse button for previous iteration. The user caniterate the L-System seven times. In the iteration process the current parameter is interpreted character bycharacter and if the character is a variable the respected rule is inserted into a temporary char * variableotherwise the constant is inserted. Once the iteration is complete the temporary variable is copied intothe current parameter.

After the iteration the parameter is interpreted. I am creating a single 3D mesh to represent the L-System. In my implementation I have created a stack which stores the critical information of the tree i.e.The model matrix, extrusion length, and radius. First a base circle is created with position from the modelmatrix as its centre and radius as its radius. After this the current parameter is read character by characterif a ’[’ is encounter a copy of head of the L-System stack is pushed into the stack, if a ’]’ is encounter thehead of the stack is popped, if ’+’ is encountered then a circle is created with model matrix position plusthe extrusion length as its centre and the radius as its radius after this the model matrix is rotated in theX-axis by the angle specified in the text file and translated forward and then another circle is created withthe new position as its centre, now the indices are linked. The first five indices of the rotated circle arelinked with the first five indices of the base circle and the last five are linked with the first five of the circlewe extruded before making the rotated circle and then the remaining indices of the base and extrudedcircle are linked. If a ’-’ is encountered then the procedure for the ’+’ is repeated but with a negativerotation in the X-axis. Finally is any variable is encounter then model matrix is translated forward and acircle is created with the position as its centre and the indices are linked with the base circle.

In my implementation the user can move about in 3D space with the arrow keys, rotate with the mouseand move up down with shift and control. The user can also change alter some factors of the L-Systemby pressing home/end the user can increase or decrees the radius of the circles created in the tree, bypressing inset/delete the user can increase or decrees the extrusion length and by pressing alt/space barthe user can increase or decrees the rotation angle. My implementation also provides a textured terrain,mixed lighting and skybox implementation with sun and moon revolving around which changes the lightdirection. The light colour on the L-System is calculated using a shader which takes into account thedirectional light direction, colour, position, and attenuation factor , the ambient light colour, and theposition of the normal’s for the mesh.

3 ResultWith my implementation the user can produce any number of L-Systems as long as they provide the rulesin the text file in the specified format. The system implemented are as follows:

1)Axiom: FRules : F->F[+F]F[-F]FAngle : 25.7

Figure 2: First System

2)Axiom: FRules : F->F[+F]F[-F][F]Angle : 20.0

2

Page 3: L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and Graphics for Computer Games, Goldsmiths, University of London 1 Introduction to L-Systems

Figure 3: Second System

3)Axiom: FRules : F->FF-[-F+F+F]+[+F-F-F]Angle : 22.5

Figure 4: Third System

4) Axiom: XRules : X->F[+X]F[-X]+X,F->FFAngle : 20

Figure 5: Fourth System

3

Page 4: L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and Graphics for Computer Games, Goldsmiths, University of London 1 Introduction to L-Systems

5) Axiom: XRules : X->F[+X][-X]FX,F->FFAngle : 25.7

Figure 6: Fifth System

6) Axiom: XRules : X->F-[[X]+X]+F[+FX]-X,F->FFAngle : 22.5

Figure 7: Sixth System

7) Axiom: X-F-FRules : X>X-F+X+F-X,F->FFAngle : 130

4

Page 5: L-Systemshchab001/Himanshu_Chablani_L-Systems.pdfHimanshu Chablani December 11, 2014 Maths and Graphics for Computer Games, Goldsmiths, University of London 1 Introduction to L-Systems

Figure 8: Sierpinski’s Triangle

8) Axiom: F++F++FRules : F->F-F++F-FAngle : 60

Figure 9: Koch’s Snowflake

References[1] http://en.wikipedia.org/wiki/L-system

5