vector rotations for branches of a 3d tree

Posted by freefallr on Game Development See other posts from Game Development or by freefallr
Published on 2011-11-25T21:50:59Z Indexed on 2011/11/26 2:09 UTC
Read the original article Hit count: 312

Filed under:
|
|
|
|

I'm attempting to create a 3d tree procedurally. I'm hoping that someone can check my vector rotation maths, as I'm a bit confused.

I'm using an l-system (a recursive algorithm for generating branches).

The trunk of the tree is the root node. It's orientation is aligned to the y axis.

In the next iteration of the tree (e.g. the first branches), I might create a branch that is oriented say by +10 degrees in the X axis and a similar amount in the Z axis, relative to the trunk.

I know that I should keep a rotation matrix at each branch, so that it can be applied to child branches, along with any modifications to the child branch.

My questions then:

for the trunk, the rotation matrix - is that just the identity matrix * initial orientation vector ?

for the first branch (and subsequent branches) - I'll "inherit" the rotation matrix of the parent branch, and apply x and z rotations to that also.

e.g.

using glm::normalize;
using glm::rotateX;
using glm::vec4;    
using glm::mat4;
using glm::rotate;

vec4 vYAxis     = vec4(0.0f, 1.0f, 0.0f, 0.0f);
vec4 vInitial   = normalize( rotateX( vYAxis, 10.0f ) );

mat4 mRotation  = mat4(1.0);

// trunk rotation matrix = identity * initial orientation vector
mRotation *= vInitial;

// first branch = parent rotation matrix * this branches rotations
mRotation *= rotate( 10.0f, 1.0f, 0.0f, 0.0f );     // x rotation
mRotation *= rotate( 10.0f, 0.0f, 0.0f, 1.0f );     // z rotation

Are my maths and approach correct, or am I completely wrong?

Finally, I'm using the glm library with OpenGL / C++ for this. Is the order of x rotation and z rotation important?

© Game Development or respective owner

Related posts about c++

Related posts about opengl