opengl3 2个点向量的相同颜色

时间:2018-04-16 12:08:51

标签: c++ opengl opengl-3

我想用颜色显示2个点矢量之间的对应关系。这意味着我希望v1 [i]具有与v2 [i]相同的颜色。

为此,我创建了2个向量,其中我有我的点(它们具有相同的点数),以及1个包含颜色的向量。

现在我想绘制它们,但是应该具有相同颜色(在v1和v2中具有相同索引)的点具有不同的颜色。我发现它令人不安,因为我使用了1个颜色矢量,我不会在绘制调用之间修改它。

这是我的代码:

std::vector<GLfloat> points2D;
//it is names Points3D because I want this vector to contain the reprojection of 3D points into 2D points
std::vector<GLfloat> points3D;
std::vector<glm::vec3> colors;
GLuint VAO[2], VBO[4];

//Create Points2D and Points3D and colors from others vectors (I will change that later)
void addPoints(std::vector<float>& pt2ds, std::vector<float>& pt3ds) {
    std::default_random_engine generator;
    std::uniform_real_distribution<float> distribution(0.0f, 1.0f);
    for (int i = 0; i < pt2ds.size(); i+=2) {
        points2D.push_back(pt2ds[i]);
        points2D.push_back(pt2ds[i + 1]);
        points3D.push_back(pt3ds[i]);
        points3D.push_back(pt3ds[i + 1]);
        float a = distribution(generator);
        float b = distribution(generator);
        float c = distribution(generator);
        glm::vec3 color(a, b, c);
        colors.push_back(color);
    }
}

void setupPoints() {
    glGenVertexArrays(2, &VAO[0]);
    glGenBuffers(4, &VBO[0]);

    glBindVertexArray(VAO[0]);

    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, points2D.size() * sizeof(GLfloat), &points2D[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

    glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);


    glBindVertexArray(VAO[1]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ARRAY_BUFFER, points3D.size() * sizeof(GLfloat), &points3D[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(2 * sizeof(float)));

    glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);
    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);


    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

void draw(Shader& shader) {
    shader.use();
    glBindVertexArray(VAO[0]);
    glDrawArrays(GL_POINTS, 0, (GLsizei)points2D.size() /2);

    glBindVertexArray(VAO[1]);
    glDrawArrays(GL_POINTS, 0, (GLsizei)points3D.size() / 2);


    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

和我的着色器:vertexShader:

#version 330 core
layout (location = 0) in vec2 aVertCoord;
layout (location = 1) in vec3 col;

out vec3 color;
void main()
{
    gl_Position = vec4(aVertCoord.xy, 0.0, 1.0);
    color = col;
}

和片段:

#version 330 core
out vec4 FragColor;
in vec3 color;
void main()
{
    FragColor = vec4(color,1.0);
}

为什么这是假的?为什么发送到VBO [0]的颜色和发送到VBO [1]的颜色不一样?

也许我做错了,有一种更简单的方法来实现它?

谢谢!

1 个答案:

答案 0 :(得分:3)

问题在于第二个位置VBO的顶点属性绑定:

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(2 * sizeof(float)));

这一行告诉OpenGL它应该在前两个浮点数后开始读。由于您没有以相同的方式跳过第一种颜色,因此绘制的第一个点使用points3D [1]但颜色为[0]。

解决方案:由于两个位置数组都包含相同数量的点,因此它们也应该从相同的索引开始。将上面的行更改为

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);