Skip to content

haozoo/bonsai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bonsai++

bonsai++ is a 3D bonsai tree generation algorithm built on top of a primitive OpenGL graphics engine. Bonsai growth is randomly generated and animated for fun.

There are two viewing modes: USER where the camera is operated by the user and ROTATING where the camera follows a preset path.

bonsair.mp4

note: screen capture software ruined most of the color, so unfortunately colors are duller than intended


About bonsai++

bonsai++ is made up of two parts: a recursive bonsai generation algortihm and an OpenGL graphics engine!


Bonsai Generation

generateBonsai is where the brunt of the tree-generation algorithm is, as this function dictates the location of each branch and leaf. generateBonsai slowly grows each branch using a recursive function, and on each recursive step, a new branch has a posibility to spawn. As the branches go further outwards, the likelihood of a new branch also increases, this is so the bonsai is more 'tree-y' and mimics a real tree's growth.

As each branch dies, foliage is also generated recursively, simply stacking loose circles of diminishing size on top of the final branch position. To give noise to the foliage, the probabilty that a leaf block will generate decreases proptionate to its distance from the end of the branch.

Beyond the features mentioned above, the algorithm does also take extra precautions to make sure that a (fairly) life-like bonsai tree is grown.

OpenGL graphics engine

Instead of a using a pre-built library to render the model generated by the above algorithm, I decided to build my own model viewer using OpenGL. While still basic, this part of the program features a shading system (vertex and frament shaders only), a primitive lighting system and user-controlled camera movement.


Full Demo Video

This video shows demos the project. Unfortunately there is no audio, but a general overview is that the program begins in cinematic ROTATING view, where new bonsai creation and animation is demo'd first, followed by bonsai recreation. From there USER view mode is demo'd, as the camera is shown to move forward, left, back and right - as well as zooming in on the tree.


bonsai_full_demo.mp4

Challenges

While tweaking the bonsai's growth algorithm was a painful process of trial and error - the majority of challenges faced were in regards to learning how to use OpenGL's state-full window.

In particular, managing the camera was difficult - as processing the pitch / yaw of the camera using Euler Angles was new to me. Same goes for the lookAt, projection and perspective matrices that were needed to convert the 3D space onto a 2D screen.

The shader logic for the program was also difficult to implement, as writing each component was a steep learning curve. This alongside vertex attribute and buffer objects being annoying to handle made even just getting the program itself up and running one of the bigger tasks. Unfortunately, I also spent time implementing diffuse lighting and specular lighting - but found that it was unecessary for the final product.


Technology

This project was written in C++ and used OpenGL to create the graphics engine - the following helper libraries were used:

*   GLAD v0.1.34   // OpenGL helper library to load platform specific OpenGL function points 
*   GLFW v3.3.4    // OpenGL helper for window and context management
*   stb_image.h    // Image loader 
*   glm v0.9.9     // OpenGL mathematics library for vector / matrices 

set-up

This project requires OpenGL 3.3 to be installed on the system, as it uses the API specification to communicate with with onboard graphics. Being platform independent, this program runs on every major operating system given the libraries listed above and OpenGL version 3.3 are setup.

To run, just excute the Makefile in the directory

make *

And then, run bonsai using

./bonsai

Features

bonsai++ features 2 modes, USER where the camera movement is defined by the user and ROTATING directed camera view that circles around the bonsai.

Key Bindings

movement

keybind action
w moves camera forward
a moves camera to the left
s moves camera backwards
d moves camera to the right

(these only work when in USER camera mode)

camera control

input
move mouse changes view angle
scroll mousewheel zooms in and out
r switches between USER view and ROTATING view

bonsai generation

keybind action
q creates an entirely new bonsai tree
e re-animates the current bonsai tree

Folder Structure

/
├─ archive/          // Old 2D bonsai generation project
├─ img/              // Images to load as textures
├─ include/          // Include for GLAD function loader
├─ src/              
|  ├─ bonsai/        // Contains Bonsai generation algorithm 
|  ├─ camera/        // Contains Camera handling class
|  ├─ shaders/       // Contains Shader handling class and vertex / fragment shaders
|  ├─ texture/       // Contains Texture handling class
|  ├─ util/          // Helper functions for handling OpenGL

About

Procedurally generate bonsai trees

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors