如何将线条颜色与顶点颜色分开?

时间:2019-07-17 20:38:49

标签: c++ opengl shader

线会自动绘制为顶点的颜色

我尝试将每条线与顶点分开绘制,但是没有用。

void draw()
{
    glDrawArrays(GL_POINTS, 0, vertecies.size());
    glDrawElements(GL_LINES, lines.indecies.size(), GL_UNSIGNED_INT, nullptr);
}


void buffer_init()
{
    glGenBuffers(1, &vertex_bufffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_bufffer);
    glBufferData(GL_ARRAY_BUFFER, MAX_VERTEX_COUNT * sizeof(gl_vertex2d), &vertecies[0], GL_DYNAMIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(gl_vertex2d), 0);
    glEnableVertexAttribArray(0);

    //colors
    glGenBuffers(1, &vertex_color_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_color_buffer);
    glBufferData(GL_ARRAY_BUFFER, MAX_VERTEX_COUNT * sizeof(gl_vertex2d), &vertecies[0], GL_DYNAMIC_DRAW);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(gl_vertex2d), (GLvoid*)8);
    glEnableVertexAttribArray(1);


    glGenBuffers(1, &lines_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lines_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_LINES_COUNT * sizeof(gl_lines2d), &lines.indecies[0], GL_DYNAMIC_DRAW);

    //that's how i'm trying to color lines
    glGenBuffers(1, &lines_color_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, lines_color_buffer);
    glBufferData(GL_ARRAY_BUFFER, MAX_LINES_COUNT * sizeof(gl_lines2d), &lines.colors[0], GL_DYNAMIC_DRAW);
}

但是最后三行代码并没有真正改变任何东西:图片保持不变。

我正在使用以下顶点和片段着色器:

#FRAGMENT
#version 330 core

in vec4 vColor;
out vec4 fColor; 

void main()
{
    fColor = vColor;
}

#VERTEX
#version 330 core

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
out vec4 vColor;

void main()
{
    gl_Position = position;
    vColor = color;
}

0 个答案:

没有答案