Raph Schim
Raph Schim

Reputation: 538

opengl3 Same color for 2 vectors of points

I want to show the correspondence between 2 vectors of points using color. It means that I want v1[i] having the same color as v2[i].

To do so, I created 2 vectors in which I have my points (they have the same number of points), and 1 vector containing colors.

Now I want to draw them, but points that should have the same colors (that have the same index in v1 and v2), have different colors. I Find it disturbing because I use 1 vector of color, and I don't modify it between the draw calls.

Here is my code :

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);
}

and my shader : 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;
}

and Fragment :

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

Why is it false? Why do colors sent to VBO[0] and colors sent to VBO[1] are not the same?

Maybe I'm doing it really wrong and there is a much simpler way to achieve that?

Thanks!

Upvotes: 1

Views: 207

Answers (1)

BDL
BDL

Reputation: 22168

The problem lies in the vertex attribute binding for the second position VBO:

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

This line tells OpenGL that it should start reading after the first two floats. Since you don't skip the first color in the same way, the first point drawn uses points3D[1] but colors[0].

Solution: Since both position arrays contain the same number of points, they should also start from the same index. Change the line above to

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

Upvotes: 3

Related Questions