0

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?

jeha
  • 10,562
  • 5
  • 50
  • 69
Trt Trt
  • 5,330
  • 13
  • 53
  • 86
  • Below his notes he has linked a C source file which shows how to do it. – ChrisWue Nov 05 '11 at 22:54
  • [My question here](http://stackoverflow.com/questions/12274863/how-to-trap-my-surface-patches-to-prevent-the-background-from-bleeding-through) has Postscript source code for calculating the vertices of faces on a cylinder.. – luser droog Sep 25 '12 at 05:59

4 Answers4

2

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))
datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

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:

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

More about cross product you can read here

Grook
  • 395
  • 1
  • 6
  • 19
0

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.

Dimitris
  • 13,480
  • 17
  • 74
  • 94
0

Yes, glm can build cross product matrices, see the GLM API (glm::gtx::matrix_cross_product::matrixCross3 and glm::gtx::matrix_cross_product::matrixCross4).

jeha
  • 10,562
  • 5
  • 50
  • 69