OpenGL Color By Face

时间:2016-02-02 13:22:38

标签: c++ opengl matrix graphics colors

我是OpenGL的新手,还有一个我正在研究的小项目。该项目的一部分是创建一个网格,允许网格的某些部分具有不同的颜色。

E.g。整个网格是绿色的,但网格上的一个块是红色的,也许是另一个黄色。

在使用标记时,我使用GL_TRIANGLE_STRIP绘制此网格所做的工作。在那个阶段之后,我还在同一个顶点数据数组中包含了颜色。但输出并不是我想要的结果。

首先有一个插值,我试图通过添加颜色的'flat'标志来删除。但似乎存在重叠问题。这导致了picture

无论如何都要创建一个网格。网格块可以与网格颜色不同。

更新 这是我的GL_TRIANGLES

代码
short* Grid::Indicies()
{
    const int X_GRID_SIZE = X_GRID_SIZE_;
    const int Y_GRID_SIZE = Y_GRID_SIZE_;
    const int INDICIES_SIZE = (((X_GRID_SIZE * 4) + ((X_GRID_SIZE_ - 3) * 2)) * Y_GRID_SIZE);

    short* indicies = new short[INDICIES_SIZE];
    int index = 0;

    for (size_t y = 0; y < Y_GRID_SIZE_; y++)
    {
        // Current, Down, Right, Down
        indicies[index++] = (short)(y * X_GRID_SIZE_);
        indicies[index++] = (short)((y + 1) * X_GRID_SIZE_);
        indicies[index++] = (short)((y * X_GRID_SIZE_) + 1);
        indicies[index++] = (short)((y + 1) * X_GRID_SIZE_);

        for (size_t x = 1; x < X_GRID_SIZE_ - 1; x++)
        {
            // Current, Down, Current, Down, Right, Down
            for (size_t i = 0; i < 2; i++)
            {
                indicies[index++] = (short)((y * X_GRID_SIZE_) + x);
                indicies[index++] = (short)(((y + 1) * X_GRID_SIZE_) + x);
            }
            indicies[index++] = (short)((y * X_GRID_SIZE_) + x + 1);
            indicies[index++] = (short)(((y + 1) * X_GRID_SIZE_) + x);
        }
        // Current, Down
        indicies[index++] = (short)(((y + 1) * X_GRID_SIZE_) - 1);
        indicies[index++] = (short)(((y + 2) * X_GRID_SIZE_) - 1);
    }
    indicies_size_ = index;
    return (indicies);
}

GLfloat vertices[] = {
    // Position                 // Color
    -1.0f,  1.0f,  0.0f,       1.0f,  0.0f,  0.0f, 
    -0.5f,  1.0f,  0.0f,       1.0f,  0.0f,  0.0f, 
     0.0f,  1.0f,  0.0f,       1.0f,  0.0f,  0.0f, 
     0.5f,  1.0f,  0.0f,       1.0f,  0.0f,  0.0f, 
     1.0f,  1.0f,  0.0f,       1.0f,  0.0f,  0.0f, 

    -1.0f,  0.5f,  0.0f,       1.0f,  0.0f,  0.0f, 
    -0.5f,  0.5f,  0.0f,       1.0f,  0.0f,  0.0f, 
     0.0f,  0.5f,  0.0f,       1.0f,  0.0f,  0.0f, 
     0.5f,  0.5f,  0.0f,       1.0f,  0.0f,  0.0f, 
     1.0f,  0.5f,  0.0f,       1.0f,  0.0f,  0.0f, 

    -1.0f,  0.0f,  0.0f,       1.0f,  0.0f,  0.0f, 
    -0.5f,  0.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
     0.0f,  0.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
     0.5f,  0.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
     1.0f,  0.0f,  0.0f,       0.0f,  1.0f,  0.0f,

    -1.0f, -0.5f,  0.0f,       0.0f,  1.0f,  0.0f, 
    -0.5f, -0.5f,  0.0f,       0.0f,  1.0f,  0.0f, 
     0.0f, -0.5f,  0.0f,       0.0f,  1.0f,  0.0f, 
     0.5f, -0.5f,  0.0f,       0.0f,  1.0f,  0.0f, 
     1.0f, -0.5f,  0.0f,       0.0f,  1.0f,  0.0f, 

    -1.0f, -1.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
    -0.5f, -1.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
     0.0f, -1.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
     0.5f, -1.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
     1.0f, -1.0f,  0.0f,       0.0f,  1.0f,  0.0f, 
};

GLuint v_buffer_object, v_array_object, e_buffer_object;
glGenVertexArrays(1, &v_array_object);
glGenBuffers(1, &v_buffer_object);
glGenBuffers(1, &e_buffer_object);

glBindVertexArray(v_array_object);

glBindBuffer(GL_ARRAY_BUFFER, v_buffer_object);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, e_buffer_object);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(grid.Indicies()) * grid.IndiciesSize(), grid.Indicies(), GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

2 个答案:

答案 0 :(得分:1)

不要使用GL_TRIANGLE_STRIP。使用GL_TRIANGLES并使用2个三角形和4个顶点绘制每个块。每个顶点只属于1个块。多亏了你,你不必使用&#39; flat&#39;标志,只需一次更改4个顶点的颜色。您可以尝试将每个块分开绘制,彼此之间有空格。然后,您可以通过平移顶点并创建网格来连接它们。

答案 1 :(得分:1)

使用GL_TRIANGLE_STRIP时,平面着色将使用顶点 i +2中的颜色来着色 i (这称为引发顶点< / em>的)。这不能得到你想要的东西。一个简单的选择是切换到GL_TRIANGLES,并确保指定元素索引,以便为每个四边形中的两个三角形选择相同的顶点(因为第三个顶点是三角形的激发顶点,默认情况下) 。例如,假设这些是顶点:

 0   1   2   3

 4   5   6   7

 8   9  10  11

12  13  14  15

让我们看看其中一个四边形:

0---1
|   |
4---5

让我们选择顶点0来保持整个四边形的颜色。然后我们做的是确保两个三角形都包含顶点0,并且顶点0在两个三角形中都是最后一个。

0---1
| \ |
4---5

所以我们的索引数组将是:

5 1 0 4 5 0 ...

这使用正(逆时针)绕线顺序。