问题描述:

I am trying to draw a cylinder in OpenGl. I found this algorithm but I can't make any sense of it.

http://paulbourke.net/miscellaneous/sphere_cylinder/

"Given the two perpendicular vectors A and B one can create vertices around each rim of the cylinder. So, for a 4 vertex facet the vertices might be given by the following where theta2 - theta1 is some suitably small angle that determines the roughness of the approximation."

How can I find A and B ?? I am using glm. can glm calculate cross product?

A and B form the base of a orthogonal (prefarably orthonormal), cartesian 2D coordinate system. Think of them like X and Y axes. Recall that the parametric equation for a circle is

```
p(t) = r (X cos(t) + Y sin(t))
```

Now replace X and Y with A, B, and you got the equation for the circular cross section of a cylinder. To make it a cylinder you extrude by the vector perpendicular to A and B, i.e. C = A × B

```
p(s, t) = s * C + r (A cos(t) + B sin(t))
```

There are an answer a few strings upper:

There are a number of ways of creating these two vectors, they normally require the formation of any vector that is not colinear with the cylinder axis. The cross product of that vector with the cylinder axis (P2-P1) gives one of the vectors (A say), taking the cross product of this new vector with the axis gives the other vector (B). These two perpendicular vectors are then normalised.

So lets go step by step:

- Create any not collinear with cylinder axis vector.
- Find the cross product of this vector with cylinder axis. Result of cross product is vector called vector A.
- Finding the cross product of vector A with cylinder axis results in vector B
- Normalize vetors A and B

More about cross product you can read here

Yes, glm can build cross product matrices, see the GLM API (`glm::gtx::matrix_cross_product::matrixCross3`

and `glm::gtx::matrix_cross_product::matrixCross4`

).

I have created a module for Unity3D in C# that creates a cylinder procedurally and allows you to tweak its parameters. You should be able to easily convert to C++ as the geometry calculation is the same everywhere and I'd like to think the code is easy to understand :) Watch the video to see what it's about and download the code from GitHub.