How do I dr开发者_如何转开发aw a cylinder with OpenGL in OpenTK?
Sample code from an older project of mine. This creates an "uncapped" cylinder (top and bottom are empty).
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);
}
You can now render the cylinder like this:
GL.Begin(BeginMode.Triangles);
foreach (int index in indices)
    GL.Vertex3(vertices[index]);
GL.End();
You can also upload vertices and indices into a vertex buffer object to improve performance.
Generating the geometry for a cylinder is quite simple (let's consider a Z-aligned cylinder). Let me use pseudocode:
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
About the indices: Let us assume N equal to the number of "levels" or "slices" of the cylinder (which depends on HEIGHT and StepB) and M equal to the number of points on every "slice" (which depends on StepA).
The cylinder contains some quads, each spanning 2 neighbouring slices, so the indices would look like:
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]
If you need normals for the cylinder, they are simple to generate:
normals = (x/RADIUS,y/RADIUS,0)
    for each (x,y,z) in points
That's it for the round part of the cylinder, you might also want the "caps" but I believe they are easy to do.
I'll leave the fun part of translating my pseudocode into your language of choice for you. :)
The rest is to create/bind the VBO, load up the geometry, set pointers, use your shader of choice and call glDrawArrays(...) - any OpenGL 3 tutorial should cover this; are you familiar with that part?
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论