how to draw 3d cylinder with flat shading

﹥>﹥吖頭↗ 提交于 2019-12-11 18:38:49

问题


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?


回答1:


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

glShadeModel(GL_FLAT);


来源:https://stackoverflow.com/questions/15546655/how-to-draw-3d-cylinder-with-flat-shading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!