opengl3 2 个点向量的颜色相同

opengl3 Same color for 2 vectors of points

本文关键字:颜色 向量 opengl3      更新时间:2023-10-16

我想用颜色显示 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);
}

和我的着色器:顶点着色器:

#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] 的颜色不一样?

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

谢谢!

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

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

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

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

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