代码之家  ›  专栏  ›  技术社区  ›  S. Tarık Çetin

OpenGL在执行渐变时跳过顶点

  •  1
  • S. Tarık Çetin  · 技术社区  · 6 年前

    当我这样做时:

    void drawTest()
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glShadeModel(GL_SMOOTH);
        glBegin(GL_POLYGON);
        glColor3d(1, 0, 0); // red
        glVertex2d(-200, 100); // top-left
    
        glColor3d(0, 1, 0); // green
        glVertex2d(-200, 0); // middle-left
    
        glColor3d(0, 0, 1); // blue
        glVertex2d(-200, -100); // bottom-left
    
        glColor3d(0, 0, 1); // blue
        glVertex2d(200, -100); // bottom-right
    
        glColor3d(0, 1, 0); // green
        glVertex2d(200, 0); // middle-right
    
        glColor3d(1, 0, 0); // red
        glVertex2d(200, 100); // top-right
        glEnd();
    }
    

    我明白了:

    enter image description here

    但当我切换到 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 在第三行中,结果如下:

    enter image description here

    如您所见 GL_FILL 选项跳过 middle-left 执行渐变时的顶点,而 GL_LINE 选项可以正确执行此操作。

    我能做些什么来解决这个问题?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Nicol Bolas    6 年前

    停止使用 GL_POLYGON .

    这个 GL\U多边形 原语类型将转换为一系列三角形,但这种三角剖分的方式将完全取决于实现。在您的例子中,实现可能是获取前3个顶点,并将其生成三角形。由于前3个顶点是共线的,因此形成一个没有面积的三角形,因此不会为它们渲染任何内容。

    您需要一个非常特定的三角剖分,所以您可以自己进行三角剖分并使用 GL_TRIANGLES 。例如:

    float positions[] =
    {
        -200, 100,      // top-left
        -200, 0,        // middle-left
        -200, -100,     // bottom-left
        200, -100,      // bottom-right
        200, 0,         // middle-right
        200, 100,       // top-right
    };
    
    //In the same order as `positions`.
    enum pos_indices { top_left, middle_left, bottom_left, bottom_right, middle_right, top_right };
    
    void position(pos_indices index)
    {
        int ix = index * 2;
        glVertex2f(positions[ix], positions[ix + 1]);
    }
    
    float colors[] =
    {
        1, 0, 0,    // red
        0, 1, 0,    // green
        0, 0, 1,    // blue
    };
    
    //In the same order as `colors`
    enum color_indices { red, green, blue };
    
    void color(color_indices index)
    {
        int ix = index * 3;
        glColor3f(colors[ix], colors[ix + 1], colors[ix + 2]);
    }
    
    void drawTest()
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glShadeModel(GL_SMOOTH);
        glBegin(GL_TRIANGLES);
        {
            color(red);
            position(top_left);
            color(green);
            position(middle_left);
            color(red);
            position(top_right);
    
            color(green);
            position(middle_left);
            color(green);
            position(middle_right);
            color(red);
            position(top_right);
    
            color(green);
            position(middle_left);
            color(blue);
            position(bottom_left);
            color(green);
            position(middle_right);
    
            color(blue);
            position(bottom_left);
            color(blue);
            position(bottom_right);
            color(green);
            position(middle_right);
        }
        glEnd();
    }