Search code examples
c++windowsopenglglslglfw

Points have no color, even with a shader


I'm trying to set a different color to each point in an OpenGL application, for that i have constructed 2 float std::vectors called, vertex_position_data and vertex_color_data, respectively representing x,y,z and r,g,b.

coordinates: 0.9996775489540122 0.8108964808511072 0.9943463478468979 rgb: 0.317364987841259 0.620614750460882 0.54

With it, i did:

    unsigned int vertexArrayID;
    unsigned int vertex_bufferID;
    unsigned int color_bufferID;

    glGenVertexArrays(1, &vertexArrayID);
    glBindVertexArray(vertexArrayID);

    glCreateBuffers(1, &vertex_bufferID);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferID);
    glBufferData(GL_ARRAY_BUFFER, vertex_position_data.size() * sizeof(vertex_position_data[0]), &vertex_position_data[0], GL_STATIC_DRAW);

    glCreateBuffers(1, &color_bufferID);
    glBindBuffer(GL_ARRAY_BUFFER, color_bufferID);
    glBufferData(GL_ARRAY_BUFFER, vertex_color_data.size() * sizeof(vertex_color_data[0]), &vertex_color_data[0], GL_STATIC_DRAW);

    glEnableVertexArrayAttrib(vertex_bufferID, 0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
    
    glEnableVertexArrayAttrib(color_bufferID, 1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);

    glBindVertexArray(vertexArrayID);

And called inside a loop:

while (!glfwWindowShouldClose(viewPortWindow))
{
        
    /* Rendering Area*/
    glClearColor(0.690f, 0.862f, 0.890f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPointSize(10);
        
    glDrawArrays(GL_POINTS, 0, 27);

    glfwSwapBuffers(viewPortWindow);
    glfwPollEvents();
}

Observation: For testing purposes i set an input with 10 point coordinates (actually 9), 9 * 3 = 27.

vertex shader

#version 330 core

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;

out vec3 vs_color;

void main()
{
    gl_Position = vec4(vertex_position, 1.0f);
    vs_color = vertex_color;
}

fragment shader

 #version 330 core
    
    in vec3 vs_color;
    
    out vec4 fs_color;
    
    
    void main()
    {
        fs_color = vec4(vs_color, 1.0f);
    }

By running it, i get:

enter image description here

Why i'am getting those black points?


Solution

  • You have to bind the ARRAY_BUFFER, before glVertexAttribPointer is called. Furthermore the 1st argument of glEnableVertexArrayAttrib is the Vertex Array Object rather than the Vertex Buffer Object:

    glEnableVertexArrayAttrib(vertexArrayID, 0);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferID);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
    
    glEnableVertexArrayAttrib(vertexArrayID, 1);
    glBindBuffer(GL_ARRAY_BUFFER, color_bufferID);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
    

    glVertexAttribPointer associated the buffer the buffer currently bound to the ARRAY_BUFFER target to the specified vertex attribute. The reference to the buffer is stored in the state vector of the currently bound Vertex Array Object. See also Vertex Buffer Object.