I'm trying to create vertex and index buffer for a cylinder (in OpenGL, but it shouldn't matter). I think my vertex buffer is fine (I checked drawing with GL_POINTS). Here's the code that builds it:
int sides = 10, slices = 40;
float radius = 3.5 * 10.0;
numVertices = sides * slices;
Vertices = malloc(sizeof(Vertex) * numVertices);
int angleincs = 2*M_PI/sides;
int cs_angleincs = 2*M_PI/slices;
float zval;
float zstep = height / (float)sides;
float i;
for(int m=0; m<slices; m++)
{
int index = (m*sides);
for (int n=0; n<sides; n++)
{
Vertices[index + n].Position.x = cosf(i);
Vertices[index + n].Position.y = sinf(i);
Vertices[index + n].Position.z = zval;
i += angleincs;
}
zval += zstep;
}
I'm stuck at the index buffer generation. Any help on how to build it? I tried to adapt some code from a torus generator, and I get something that apparently resembles a cylinder but it's a bit weird and has a few extra weird triangles.
numIndices = (2 * (sides+1) * slices + slices);
Indices = malloc(sizeof(GLushort) * numIndices);
int n=0;
for (int i=0;i<slices; i++) {
for (int j=0; j<sides; j++) {
Indices[n++] = i * sides + j;
Indices[n++] = ((i+1)%slices) * sides + j;
}
Indices[n++] = i * sides;
Indices[n++] = ((i+1)%slices) * sides;
Indices[n++] = ((i+1)%slices) * sides;
}
Thanks in advance.
