Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

c++ - Generating 3D Cylinder with Rotationmatrix

I am trying to create a cylinder in 3D space. I get a start and endpoint in 3D and to place the vertices, I need to create 2 circles for the bottom and the top of the cylinder. I thought about making a vector u with coordinates (1,0,0) and calculate a direction vector dir = end - start. Now I make the cross product to get a vector w = u x dir: this way I am 90 degrees away from the dir vector with w. The problem is, how do I calculate the next position w' with an arbitrary angle between w and w'. I Read a lot about rotation matrix, and that i can rotate around X-Axis with something like:

Rx(angle) =   
(1   0          0          )  
(0   cos(angle) -sin(angle))  
(0   sin(angle) cos(angle) ) 

but the direction can be something like (1.442, -3.22, 7.23). So it isn't always the x-axis but maybe I need to rotate for every new w' in every axis. And I don't know how to figure out what angle I need to calculate, how to calculate and which angle belongs to witch rotation matrix.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need basis vectors for this (which can be encoded into transform matrix).

So let assume you got:


p0[3],p1[3] - 3D center points (x,y,z) of your cylinder bases
r - radius

To construct transform matrix you need


O[3] - origin position
X[3],Y[3],Z[3] - basis vectors

So you can exploit cross product to get perpendicular vectors:

O = p0;
Z = p1-p0;
Y = (1.0,0.0,0.0);
if (|dot(Y,Z)/|Z||>0.75) Y = (0.0,1.0,0.0); // this just make sure Z and Y are not parallel
X = cross(Y,Z);
Y = cross(Z,X);
X = r*X/|X|;
Y = r*Y/|Y|;  

Now construct your 4x4 transform matrix M or use the X,Y,Z,O directly to compute the vertexes:

base0:

x=cos(a);
y=sin(a);
z=0.0;

base1:

x=cos(a);
y=sin(a);
z=1.0;

Where a = <0.0,2.0*PI> is angle. XY plane is parallel to bases and z is cylinder axis. To convert these (x,y,z) to World coordinates use:

(x',y',z') = M * (x,y,z)

Which can be done natively in OpenGL by using M as part of GL_MODELVIEW or do this instead:

x' = O[0] + x*X[0] + y*Y[0] + z*Z[0];
y' = O[1] + x*X[1] + y*Y[1] + z*Z[1];
z' = O[2] + x*X[2] + y*Y[2] + z*Z[2];

Take a look at related QAs:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...