How to transform the random vectors around normal

Hello everyone, I want to generate several random direction vectors around the vertex normals, but How should I transform them from local coordinates to world coordinates ? I have calcuated the theta and phi, but how to use them to obain correct direction vector in world coordinates ?

Thanks !

Use the normal to generate a coordinate system. For instance, pick the axis with the largest absolute value in the normal, and take the cross product between the normal and that axis, and normalize. This will give you one of the basis vectors. Then take the cross product between the normal and the basis vector, normalize, and you have your second basis vector. The normal is the last basis vector.

Then you construct a matrix where the rows are the three basis vectors. If you want the normal to represent the local y axix, you’ll want to put it in the second row, for z use the third row.

If you get bogus results, fill in the columns in the matrix instead of the rows.

I think that should work… then again I just got out of bed :slight_smile:

Since I was in a bit of a rush this morning, I’ll flesh out my reply a bit.

First construct the basis vectors (in world space) for the local coordinate system. The normal is the z basis vector, so you need to find the x and the y basis vectors. Do this using the above approach.

Next you use the basis vectors to construct a matrix which you can use to transform the random vectors from the local space to world space.

Finally, you generate one or more random vectors using your phi/theta in local coordinates, where “up” (in my case, positive z) represents the vertex normal, and then transform these using the above matrix.

To check if you made the transformation matrix correct, you can start by verifying that the vector (0, 0, 1) is transformed into the vertex normal.

If you want to use y as “up”, just make sure to take this into account when generating the random vectors, and put the vertex normal in the second row/column instead (the two basis vectors goes in the other two columns).

I’m assuming you want to generate uniformly distributed vectors, btw.