2010-11-13 80 views

回答

1

来自我的旧项目的示例代码。这会创建一个“不加盖”的圆柱体(顶部和底部是空的)。

int segments = 10; // Higher numbers improve quality 
int radius = 3; // The radius (width) of the cylinder 
int height = 10; // The height of the cylinder 

var vertices = new List<Vector3>(); 
for (double y = 0; y < 2; y++) 
{ 
    for (double x = 0; x < segments; x++) 
    { 
     double theta = (x/(segments - 1)) * 2 * Math.PI; 

     vertices.Add(new Vector3() 
     { 
      X = (float)(radius * Math.Cos(theta)), 
      Y = (float)(height * y), 
      Z = (float)(radius * Math.Sin(theta)), 
     }); 
    } 
} 

var indices = new List<int>(); 
for (int x = 0; x < segments - 1; x++) 
{ 
    indices.Add(x); 
    indices.Add(x + segments); 
    indices.Add(X + segments + 1); 

    indices.Add(x + segments + 1); 
    indices.Add(x + 1); 
    indices.Add(x); 
} 

您现在可以呈现这样的汽缸:

GL.Begin(BeginMode.Triangles); 
foreach (int index in indices) 
    GL.Vertex3(vertices[index]); 
GL.End(); 

您也可以上传顶点和索引到一个顶点缓冲区对象,以提高性能。

+0

提琴手,你的代码运作良好。你能解释指数与顶点映射吗?最初,您正在计算圆柱体的底部和顶部圆的顶点,然后将这些顶点映射到索引。我的序列不清楚。你可以在样本图像中标记这些段吗? – 2014-07-13 09:58:32

+0

顶点是三维空间中的点。索引描述如何将这些点连接成三角形(每个三角形有三个顶点)。有关更多详情,请参阅此处:http://www.opentk.com/doc/chapter/2/opengl/geometry/primitives – 2014-07-13 14:34:20

1

生成圆柱体的几何体非常简单(让我们考虑一个Z轴对齐圆柱体)。让我用伪代码:

points = list of (x,y,z) 
    where x = sin(a)*RADIUS, y = cos(a)*RADIUS, z = b, 
    for each a in [0..2*PI) with step StepA, 
    for each b in [0..HEIGHT] with step StepB 

关于指数:让我们假设N等于圆柱(这取决于身高和步骤B)和M等于数“水平”或“片”的数量每个“切片”上的点(取决于StepA)。

气缸含有一些四边形,每一个横跨两座相邻的片,所以指数看起来像:

indices = list of (a,b,c,d) 
    where a = M * slice + point, 
      b = M * slice + (point+1) % M, 
      c = (M+1) * slice + (point+1) % M, 
      d = (M+1) * slice + point 
    for each slice in [0..N-2] 
    for each point in [0..M-1] 

如果你需要法线的缸,它们易于产生:

normals = (x/RADIUS,y/RADIUS,0) 
    for each (x,y,z) in points 

这就是圆柱体的圆形部分,你可能也想要“帽子”,但我相信它们很容易做到。

我将离开翻译我的伪代码到您选择的语言的乐趣部分。 :)

剩下的就是创建/绑定VBO,加载几何图形,设置指针,使用选择的着色器并调用glDrawArrays(...) - 任何OpenGL 3教程都应该覆盖这个;你熟悉那个部分吗?

+0

对不起,我不明白你的伪代码。我找到了OpenTK.Glu.Cylinder,但是在解决问题时遇到了问题,如果您使用OpenTK,也许您可​​以帮助我解决问题。 ty – Chris 2010-11-13 17:28:33

+0

您的问题最初提到OpenGL 3和VBO,它向我建议您想自己生成几何图形。你能用Glu.Cylinder来描述你的问题吗? – Kos 2010-11-13 18:42:11

+0

请不要使用Glu.Cylinder。 Glu在OpenGL 3.0中被弃用,不再被支持。 – 2010-11-13 22:39:50

相关问题