I wrote a program in C++ that uses OpenGL, GLUT, and GLSL to display graphics that creates a Lindenmayer System (L-System). L-Systems are parallel rewriting systems that are used for plant growth modeling. My main program displays a L-System tree that cycles through the seasons using the glutTimerFunc. The leaves and flowers change size, color, and position and the sky changes color. The user can use the 'w', 'a', 's', 'd' keys and the mouse to change where they are looking and where the camera is positioned. A point light and full Phong shading are implemented.
The tree starts off in the spring and grows flowers and leaves that change color as they grow and blossom.
Once the blossoms and leaves are fully grown and colored bright pink and green, summer comes and the leaves grow darker. Partway through the summer the blossoms fall to the ground.
In the fall, the flowers on the ground turn brown and the leaves on the tree turn orange/red.
Then in the winter the leaves turn brown and fall off the tree. The branches sit bare until the spring comes again.
The tree is drawn using a series of cylinders created using the GeometryCreator.cpp. The leaves and flowers are made of spheres from the GeometryCreator.cpp. The pattern for the tree is created using a string. The string starts out as a base string and a "rule" is substituted into the base string repeatedly for a given number of times. The main tree in my program is generated with the base string "F", a rule of "F-[-F+F+F]+[+F-F-F]F" with 2 iterations and an angle of 20 degrees.
My implementation of the L-System grammar only covers rotations in 2 dimensions. The meanings of the letters are:
+ Turn right around the z-axis by the angle defined during initialization.
- Turn left around the z-axis by the angle defined during initialization.
[ Push the current state to the stack, used to make tree branches.
] Pop the state off the stack, used to make tree branches.
F Move forward the length of a cylinder and draw.
The MStackHelp.cpp file was unusable for this project for storing rotation and translation matrices. This was because the cylinder created using GeometryCreator.cpp needed to be rotated, scaled, and translated to be useable as part of a tree. So I created three C++ float stacks to store the angle of rotation, the translation in the x direction, and the translation in the y direction. When I reached the + and - commands, I simply added or subtracted the angle to the value on the top of the angle of rotation stack. When I reached an F command I calculated the necessary translations in the x and y direction based off the sine and cosine of the top angle of rotation. When i reached the [ and ] commands, I pushed or popped the top values on all three of my stacks.
I created other L-Systems using the same grammar rules listed above but I did not add leaves, flowers, or animation to these. They can be viewed by pressing the '2', '3', and '4' keys while running my program. The fully substituted L-System strings can be seen in the terminal while the graphics part of the program runs.
Initial string "F", rule "F+F-F-F+F", 3 iterations, angle 90 degrees.
Initial string "F", rule "F[+F]F[-F]F", 3 iterations, angle 23.5 degrees.
Initial string "F", rule "FF-[-F+F+F]+[+F-F-F]", 3 iterations, angle 23 degrees.