By Raph Schim


2018-04-16 12:08:51 8 Comments

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!

1 comments

@BDL 2018-04-16 13:14:21

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

@Raph Schim 2018-04-16 13:16:14

I just found out too (I was going to write the answer when I saw yours)!! It was from my previous version when I had only 1 vector instead of 2 ... And I didn't removed it... I'm not the sharpest tool in the box ... Thanks!

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] Undefined behavior and sequence points

0 Answered Questions

pass a std::vector<glm::mat4> to a vertex shader as vertex array attribute

1 Answered Questions

[SOLVED] Send Depthbuffer and TextureBuffer to same Fragment OpenGL3

  • 2017-03-13 08:39:39
  • Raph Schim
  • 50 View
  • 3 Score
  • 1 Answer
  • Tags:   c++ opengl opengl-3

4 Answered Questions

[SOLVED] Appending a vector to a vector

  • 2010-03-31 09:33:02
  • sub
  • 368590 View
  • 493 Score
  • 4 Answer
  • Tags:   c++ stl vector

1 Answered Questions

[SOLVED] How to load VBO and render it on separate Java threads?

0 Answered Questions

PyOpenGL - passing transformation matrix into shader

  • 2012-10-06 09:36:05
  • M-V
  • 4126 View
  • 21 Score
  • 0 Answer
  • Tags:   python opengl

1 Answered Questions

[SOLVED] Editing Buffer Data In a Vertex Array

1 Answered Questions

[SOLVED] Opengles mixing VBO and non VBO renders gives EXC_BAD_ACCESS

1 Answered Questions

[SOLVED] Incorrect lighting when batching triangle strips in OpenGL

  • 2012-09-05 08:14:02
  • M-V
  • 641 View
  • 1 Score
  • 1 Answer
  • Tags:   opengl

4 Answered Questions

[SOLVED] OpenGL3 the wrong texture is displaying

Sponsored Content