0

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
You Hock Tan
  • 995
  • 2
  • 9
  • 18

1 Answers1

2

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

glShadeModel(GL_FLAT);
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • is there a way to do it without this method because doing this causes problem to my highlight and smooth shading as well – You Hock Tan Mar 21 '13 at 11:56
  • You can switch it on and off as you need. If you don't want to use it, you need to provide the same normals for all vertices of a face. Therefore, some vertices need to be specified more than once, just with different normals. – Nico Schertler Mar 21 '13 at 12:34