我给出了两个GLKVector3
的代表气缸的起点和终点。 用这些点和半径,我需要建立和渲染一个圆柱体。 我可以建立一个气缸与所述点之间,而是在固定方向上的正确距离(目前始终在y(0,1,0)向上方向)。 我不知道我需要什么样的计算,使得到筒上的正确平面上的两个点之间,使一条线将通过两个端点运行。 我想有某种计算为我创造我的顶点数据与方向矢量或角度,我可以申请,这将创建一个指向正确的方向气缸。 没有人有一个算法,或知道的人,这将有助于?
Answer 1:
你在画这些缸不止一个? 或曾经绘制它在不同的位置? 如果是这样,使用算法从真棒文章是一个不那么真棒想法。 每次上传的几何数据到GPU的时候,你招致性能开销。
一种更好的方法是计算几何形状的单个基本圆筒一次-说,一个具有单元半径和高度-和东西,顶点数据到VBO 。 然后,当你画,使用模型对世界变换矩阵(如果需要独立半径和长度)规模和气缸旋转到位。 通过这种方式,并发送给与每个绘制调用的GPU唯一的新数据是4x4矩阵,而不是所有的一切缸polycount你正在绘制的顶点数据。
Answer 2:
检查这真棒文章 ; 它的过时,但在调整算法之后,它就像一个魅力。 一个尖端,OpenGL ES 2.0的仅支持三角形所以代替使用GL_QUAD_STRIP作为方法执行,使用GL_TRIANGLE_STRIP代替,结果是相同的。 该网站还包含了一些关于OpenGL的几何其他有用的信息。
为解决方案请参见下面的代码。 自代表网,并且包含顶点,索引和这样的。
- (instancetype)initWithOriginRadius:(CGFloat)originRadius
atOriginPoint:(GLKVector3)originPoint
andEndRadius:(CGFloat)endRadius
atEndPoint:(GLKVector3)endPoint
withPrecision:(NSInteger)precision
andColor:(GLKVector4)color
{
self = [super init];
if (self) {
// normal pointing from origin point to end point
GLKVector3 normal = GLKVector3Make(originPoint.x - endPoint.x,
originPoint.y - endPoint.y,
originPoint.z - endPoint.z);
// create two perpendicular vectors - perp and q
GLKVector3 perp = normal;
if (normal.x == 0 && normal.z == 0) {
perp.x += 1;
} else {
perp.y += 1;
}
// cross product
GLKVector3 q = GLKVector3CrossProduct(perp, normal);
perp = GLKVector3CrossProduct(normal, q);
// normalize vectors
perp = GLKVector3Normalize(perp);
q = GLKVector3Normalize(q);
// calculate vertices
CGFloat twoPi = 2 * PI;
NSInteger index = 0;
for (NSInteger i = 0; i < precision + 1; i++) {
CGFloat theta = ((CGFloat) i) / precision * twoPi; // go around circle and get points
// normals
normal.x = cosf(theta) * perp.x + sinf(theta) * q.x;
normal.y = cosf(theta) * perp.y + sinf(theta) * q.y;
normal.z = cosf(theta) * perp.z + sinf(theta) * q.z;
AGLKMeshVertex meshVertex;
AGLKMeshVertexDynamic colorVertex;
// top vertex
meshVertex.position.x = endPoint.x + endRadius * normal.x;
meshVertex.position.y = endPoint.y + endRadius * normal.y;
meshVertex.position.z = endPoint.z + endRadius * normal.z;
meshVertex.normal = normal;
meshVertex.originalColor = color;
// append vertex
[self appendVertex:meshVertex];
// append color vertex
colorVertex.colors = color;
[self appendColorVertex:colorVertex];
// append index
[self appendIndex:index++];
// bottom vertex
meshVertex.position.x = originPoint.x + originRadius * normal.x;
meshVertex.position.y = originPoint.y + originRadius * normal.y;
meshVertex.position.z = originPoint.z + originRadius * normal.z;
meshVertex.normal = normal;
meshVertex.originalColor = color;
// append vertex
[self appendVertex:meshVertex];
// append color vertex
[self appendColorVertex:colorVertex];
// append index
[self appendIndex:index++];
}
// draw command
[self appendCommand:GL_TRIANGLE_STRIP firstIndex:0 numberOfIndices:self.numberOfIndices materialName:@""];
}
return self;
}
文章来源: Using OpenGL ES 2.0 with iOS, how do I draw a cylinder between two points?