PDA

View Full Version : Capping end of object



jame322
05-28-2014, 07:07 AM
So i need to cap the end of a cylinder that uses triangle strip, heres my creation code so far:

drawCylinder(int numMajor, int numMinor, float height, float radius)
{
double majorStep = height / numMajor;
double minorStep = 2.0 * M_PI / numMinor;
int i, j;

for (i = 0; i < numMajor; ++i) {
GLfloat z0 = 0.5 * height - i * majorStep;
GLfloat z1 = z0 - majorStep;

glBegin(GL_TRIANGLE_STRIP);
for (j = 0; j <= numMinor; ++j) {
double a = j * minorStep;

GLfloat x = radius * cos(a);
GLfloat y = radius * sin(a);

glNormal3f(x / radius, y / radius, 0.0);
glTexCoord2f(j / (GLfloat)numMinor, i / (GLfloat)numMajor);
glVertex3f(x, y, z0);

glNormal3f(x / radius, y / radius, 0.0);
glTexCoord2f(j / (GLfloat)numMinor, (i + 1) / (GLfloat)numMajor);
glVertex3f(x, y, z1);


}
glEnd();
}
}

reto.koradi
05-29-2014, 09:42 AM
You can draw a circle at the top and bottom. The calculation of the coordinates is pretty much the same you're already using for your cylinder points. The most convenient approach for a circle is using a triangle fan (GL_TRIANGLE_FAN), use the center of the circle as the first vertex, and then the points on the periphery of the circle as the remaining vertices.

Carmine
05-29-2014, 11:27 AM
Using your code and your style of programming, the routine below yields these images.
I've selected flat shading and wireframe on the backside of polys to show the structure of the cylinder.
I'm using your own code to compute the bottom and top circles that cap the cylinder.
There is replication of vertices going on (top and bottom), which is not efficient.
Also you are generating the cylinder every time you draw it. This is also inefficient.
It would be better to compute all the vertices once, put them in an array, and render
the polys using indices into the array. However, what you've done is fine for a small program.
I doubt that your height parameter is working the way you intended.
But I leave it up to you to look into that.

1321 1322 1323



void Draw_Cylinder (int numMajor, int numMinor, float height, float radius)
{
int i, j;
GLfloat x, y, z0, z1;
const float M_PI = 3.141593;

double majorStep = height / numMajor, a;
double minorStep = 2.0 * M_PI / numMinor;

for (i = 0; i < numMajor; ++i) {
z0 = 0.5 * height - i * majorStep;
z1 = z0 - majorStep;
}

glBegin (GL_TRIANGLE_STRIP);
for (j = 0; j <= numMinor; ++j) { // Cylinder Sides

a = j * minorStep;

x = radius * cos(a);
y = radius * sin(a);

glNormal3f (x / radius, y / radius, 0.0);
glTexCoord2f (j / (GLfloat)numMinor, i / (GLfloat)numMajor);
glVertex3f (x, y, z0);

glNormal3f (x / radius, y / radius, 0.0);
glTexCoord2f (j / (GLfloat)numMinor, (i + 1) / (GLfloat)numMajor);
glVertex3f (x, y, z1);
}
glEnd ();

glColor3f (1.0, 0.6, 0.2);
glBegin (GL_POLYGON); // Cylinder Bottom - Orange Circle
glNormal3f (0.0, 0.0, -1.0);
for (j = 0; j <= numMinor; ++j) {
a = j * minorStep;
x = radius * cos(a);
y = radius * sin(a);
glVertex3f (x, y, z0);
}
glEnd ();

glColor3f (1.0, 0.6, 1.0);
glBegin (GL_POLYGON); // Cylinder Top - Pink Circle
glNormal3f (0.0, 0.0, 1.0);
for (j = 0; j <= numMinor; ++j) {
a = j * -minorStep;
x = radius * cos(a);
y = radius * sin(a);
glVertex3f (x, y, z1);
}
glEnd ();

return;
}