Given a normal N and a point P, what is the most efficent way to find a perpendicular vector V (any vector that lies in the plane perpendicular to N at P) in GLSL?
Here’s how the math should work:
N dot V is 0 when a and b are orthoganal to each other (because the dot product is NV cos theta). Component-wise, this is N_xV_x + N_yV_y + N_z*V_z=0. N_(stuff) are constants. This is a linear equation of three unknowns, so you can choose any two of them arbitrarily. For instance, you could set V_x and V_y to 1 and solve for V_z. There may or may not be some way to choose the best vector perpendicular depending on the situation, but this should give you a start. Adding V to P will give you a point in the plane you want.
A technical note about vectors (you may skip this if you so desire): vectors don’t have a position associated with them. They consist of a magnitude and a direction, not a location from which the vector projects. Often, vectors are used to represent points, but they are not the same thing. A vector doesn’t really lie in a particular plane; points do, because they have position. V+P will denote the displacement from the origin to the desired point. This is a really fine distinction, but it makes a bunch of the math work.
If you don’t want to bother solving math equations, you can simply make cross product on N with any vector:
vec3 T = cross(N, vec3(0.0,0.0,1.0));
It will always be perpendicular to N. Bear in mind that performance-wise this way may be worse.
That is a far better solution, good call there.
Keep in mind that you have to make sure that N is not (0,0,1) or you will get an indeterminate result when doing cross(N, vec3(0.0,0.0,1.0))
Why indeterminate? He’ll just get zero vector
Of course, PickleWorld, make sure the second vector in cross product does have a different direction from N.
You’re right of course. I was thinking about what you do with the result of the math operation instead of just the operation. Sorry for the confusion!
I recalled having the “indetermination” with a billboard orientation which was based on the result of the cross product you mentioned.