0
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ just test your code and see the results, if there was any problem ask how to solve them \$\endgroup\$
    – Ali1S232
    Commented Nov 25, 2011 at 22:43

1 Answer 1

0
\$\begingroup\$

From what I can see your approach is good, and your math seems alright as well. I would compile the code and see what the result is to make sure though.

\$\endgroup\$
1
  • \$\begingroup\$ well, my l-system code compiles okay, but it'll be a while before I can display anything on-screen. I'll need to write the code that utilizes the l-system to draw a tree before I can tell if anything is wrong! \$\endgroup\$
    – user3643
    Commented Nov 26, 2011 at 5:57

You must log in to answer this question.