问题
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