Domanda

I would like to know how to draw a cylinder with flat shading.

This is what I've done so far.

void 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) {
   float z0 = 0.5 * height - i * majorStep;
   float z1 = z0 - majorStep;

   glBegin(GL_TRIANGLE_STRIP);
   for (j = 0; j <= numMinor; ++j) {
   double a = j * minorStep;
   float x = radius * cos(a);
   float y = radius * sin(a);
   glNormal3f(x / radius, y / radius, 0.0);
   glTexCoord2f(j / numMinor, i / numMajor);
   glVertex3f(x, y, z0);

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

I understand that I know to define a normal however, this normal gave me smooth shading instead of flat. May I know how can I make it flat in OpenGL and GLUT?

È stato utile?

Soluzione

If you want flat shading, you just need to specify this.

glShadeModel(GL_FLAT);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top