Fractal Tree Creator
Fractal Tree Creator is an application that lets you configure unique and
purely mathematical 2D fractal trees. There are many parameters that can be
changed to modify the appearance of the fractal tree, such as the number of
branches at each fork, the spread of the branches, etc. I wrote this
application because I am interested in fractals and wanted to
experiment with how fractal trees can be made to look like trees in the
real world. It is written in C++ using SDL3 and OpenGL3.
How it works
The vertices and lines of the tree are stored in separate arrays. A recursive function takes in the tree's parameters, the parent branch's end point, and the angle and depth (number of branches from the trunk) of the current branch. It calculates the current branch's end point and adds it to the array of vertices, then adds the new branch to the array of lines. Finally, the angle of the child branches are calculated before calling the recursive function again for each child branch. Once the maximum depth is reached, the recursive function stops calling iteself.
Saving the vertices and lines to arrays before drawing allows OpenGL to draw the tree in batch, which is much more efficient than drawing lines one by one. Modern graphics cards can render millions of lines in real time with this method. This also decouples the 'building' of the tree (i.e. calculating each branch's position) from drawing the tree, meaning the tree does not have to be built every frame, and can just be built when the tree changes.
Future goals for this project
As of now, branches can only be 1 pixel thick. In the future, I plan to let the user control the thickness of the branches. This would require branches to be composed of triangles rather than just lines (as OpenGL has little support for drawing thick lines). I am currently researching methods of doing this without visual artefacts.
I also plan to let the user control the colour of the tree and change its colour based on the branch depth, e.g. to create the effect of leaves by making the end branches green and the rest brown.