# Creating a cylinder without GLU

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, For an assignment I need to make a cylinder, specifying the vertices and faces myself. I have most of the code already...(Isn't as much code as it looks, a lot of comments) cylinder::cylinder(short meshDensity){ short resolution; resolution = meshDensity; setColour(1.0, 0.0, 0.0, 1.0); vertexCount = (resolution * (resolution-1)) + 2; faceCount = (resolution * (resolution - 2)) + (resolution * 2); // allocate memory for the vertex and face lists vertices = new vertex[vertexCount]; faces = new face[faceCount]; if ((vertices == 0L) || (faces == 0L)) fatal("cylinder constructor couldn't allocate a vertex and/or face array (out of memory?)"); // now generate the vertex data // we do this with a single loop that calculates a coordinate at each end of the cylinder // (the X and Z values will be the same at the top and bottom - only the Y value changes) for (short i=0; i<meshDensity; i++) { float angle = i * (360.0 / meshDensity); vertices.set (cos_d(angle), 1.0, sin_d(angle)); // a vertex at the top // note here that when we calculate the second vertex it's not the next vertex in the array, // but instead is offset by the mesh density vertices[i+meshDensity].set (cos_d(angle), -1.0, sin_d(angle)); // a vertex at the bottom } // we're going to need these two values in several places from now on, so calculate them once here // (it's the vertex number for the top and bottom centre point of each disc) short topPt = meshDensity * 2; short botPt = meshDensity * 2 + 1; // once the vertices on the disc at each end of the cylinder have been generated, there // are two more vertices to generate - the centre points of each disc // these two extra vertices go to the end of the vertex list vertices[topPt].set(0, 1.0, 0); vertices[botPt].set(0, -1.0, 0); // next, generate the face linking information // we use a single loop here that sweeps once around the cylinder, and at each iteration generates // face linking for a face along the length, and then at each end for (short i=0; i<meshDensity; i++) { // in this loop, "i" is marching around the perimeter of the cylinder, one vertex at a time // we use "i" and the "next vertex past i" as starting points - we need to ensure that // the "next vertex past i" wraps around the current disc, so let's precalculate that // value first short j = (i + 1) % meshDensity; // now generate the first face (a four-sided face that runs the length of the cylinder) faces.init(i, j, j+meshDensity, i+meshDensity); // next, generate a triangle at the top end of the cylinder faces[i + meshDensity].init(i, topPt, j); // finally, generate a triangle at the bottom end of the cylinder faces[i + (2 * meshDensity)].init(i + meshDensity, j + meshDensity, botPt); } calculateVertexNormals(); ******************************************************************************* What I am having trouble with is how to use these vertices to create the cylinder. I thought it would be something like this... glBegin(GL_QUAD_STRIP); for (short i=0; i<meshDensity; i++) { glNormal3fv(vertices.normal.data); glVertex3fv(vertices.coordinate.data); glNormal3fv(vertices[i+meshDensity].normal.data); glVertex3fv(vertices[i+meshDensity].coordinate.data); } glEnd(); But that doesn't seem to be working. Any ideas would be greatly appreciated. Thanks

##### Share on other sites
Hi,

Given that this IS an assignment, it wouldn't be in your interests to give you a solution. However you may want to google something like "cylinderical coordinates".

MAMEman.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633670
• Total Posts
3013262
×