不使用 glShadeModel(GL_SMOOTH) 进行颜色插值

Posted

技术标签:

【中文标题】不使用 glShadeModel(GL_SMOOTH) 进行颜色插值【英文标题】:No color interpolation using glShadeModel(GL_SMOOTH) 【发布时间】:2018-06-28 20:11:16 【问题描述】:

当前和想要的输出

我正在尝试使用 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); 

【问题讨论】:

【参考方案1】:

但由于某种原因它不起作用。看起来它正在使用 glShadeModel(GL_FLAT),而我没有声明它。我做错了什么?

当您使用glShadeModel(GL_SMOOTH) 时,片段的颜色是通过在三角形图元内根据Barycentric coordinate 插值顶点的颜色属性来计算的。

但在您的情况下,颜色是按面指定的,而不是按顶点指定的。这意味着定义面的顶点的颜色属性是相同的。因此,插入相同的颜色,您将获得“平坦”的外观。

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

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();

【讨论】:

以上是关于不使用 glShadeModel(GL_SMOOTH) 进行颜色插值的主要内容,如果未能解决你的问题,请参考以下文章

基于顶点的openGL中具有不同颜色的三角形的平面着色

帮助使用 GLOrtho

Opengl使用模型视图变换移动光源

OpenGL 纹理太暗

OpenGL step to step

如何降低OpenGL纹理的噪点