问题描述:

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.

您可能感兴趣的文章：

- java - how to convert string with tags to xml?
- jquery - unable to download dynamically created file
- android - App runs fine on emulator but crashes on device
- html - Why it is accepted to use an UL, LI structure in menu lists?
- silverlight - Preserving a bound ListBox's scroll position when the underlying collection changes
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial