没有使用 glShadeModel(GL_SMOOTH) 的颜色插值

No color interpolation using glShadeModel(GL_SMOOTH)

当前和想要的输出

我正在尝试使用 glShadeModel(GL_SMOOTH) 在 3D 立方体中获取颜色插值/颜色渐变,我听说这是默认设置,但由于某些原因它不起作用。看起来它正在使用 glShadeModel(GL_FLAT),而我没有声明它。我做错了什么?

源代码:

  void drawObj (CGFobject *obj)
{ int jF, jP, dummyi=0;
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix (); 
  move();
  if (!strncmp (obj->Name, "Triangle", strlen("Triangle"))) {;
  }
  glShadeModel(GL_SMOOTH);
  for (jF=0; jF < obj->nFace; jF++)
  {
    if (!jF) { 
        glLineStipple(2, 0xAAAA); }
    else{ 
        glLineStipple(2, 0xAAAA); }
        if (wire == GL_FILL && shade == GL_FLAT) glColor4fv(colors[jF]);
        glBegin(GL_POLYGON);
    for (jP=0; jP < obj->Face[jF].nPnt; jP++)
    {
      glVertex3fv(*obj->Face[jF].Pnt[jP]);
    } 
    glEnd();
  }
  glPopMatrix ();
}
  void display (void)
{ glClear(GL_COLOR_BUFFER_BIT);
  nah=eyez; fern=nah+2*diag;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (persp) /*Perspective:*/
  {
    glViewport(winWidth*(1-viewScale)/2, winHeight*(1-viewScale)/2, 
               winWidth*viewScale,       winHeight*viewScale); 
    glFrustum (-diag, diag, -diag, diag, nah, fern);
  } else /*Parallel-Projection:*/
  { glViewport(0, 0, winWidth, winHeight); 
    glOrtho (-diag, diag, -diag, diag, nah, fern);
  }
  if (backF) glEnable  (GL_CULL_FACE);
  else       glDisable (GL_CULL_FACE);
  if (aa && wire == GL_LINE) 
  { glEnable (GL_POINT_SMOOTH); glEnable (GL_LINE_SMOOTH);
    glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  } else    { glDisable (GL_POINT_SMOOTH); glDisable (GL_LINE_SMOOTH);
              glDisable (GL_BLEND); }
  glPolygonMode(GL_FRONT_AND_BACK, wire);
  glLineWidth((GLfloat)1.5f);
  if (stipple)
      glEnable(GL_LINE_STIPPLE);
  else
      glDisable(GL_LINE_STIPPLE);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity(); 
  drawObj(&obj); 
}

but it doesn't work for some reason. It looks like it's using glShadeModel(GL_FLAT), without me declaring it. What am I doing wrong?

当您在三角形基元中使用 glShadeModel(GL_SMOOTH), then the color of the fragments is calculated by interpolating the color attributes of the vertices according to the Barycentric coordinate 时。

但在您的情况下,颜色是按面指定的,而不是按顶点指定的。这意味着定义面的顶点的颜色属性是相同的。因此内插了相同的颜色,您获得了 "flat" 外观。

您必须为每个顶点坐标设置不同的颜色属性,而不仅仅是每个面,以解决此问题:

glShadeModel(GL_SMOOTH);
for (jF=0; jF < obj->nFace; jF++)
{
    .....

    // if you would set the color here,
    // then every vertex of a face would have the same color attribute

    glBegin(GL_POLYGON);
    for (jP=0; jP < obj->Face[jF].nPnt; jP++)
    {
        glColor4fv( .... ); // <------------- set different color attributes per vertex
        glVertex3fv(*obj->Face[jF].Pnt[jP]);
    } 
    glEnd();
}