Search code examples
c++openglpoint-clouds

Change color of points openGL3.3


I'm working on opengl3.3+, and my question is : is it possible to add color to a pointcloud. In fact, I'm loading a pointCloud from a filein an array (with VAO and VBO to display that array), and I have colors for those point in another array. In opengl < 3 I would have use the function glColor3f before rendering each points, but in OpenGL3 is there a way to do it? (Maybe using shader?)

So, to be simple :
I load a file, in which I can find (x,y,z) the positions of points in pointcloud, and (r,g,b) the color associated. And I would like to draw the point with the associated color. Is this a way to achieve that?

Thanks! EDIT : here's my actual code to render :

void setupPointCloud() {
    glGenVertexArrays(1, &this->VAO);
    glGenBuffers(1, &this->VBO);
    glBindVertexArray(this->VAO);
    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);

    glBufferData(GL_ARRAY_BUFFER, this->bufferXYZ_.size() * sizeof(GLfloat), &this->bufferXYZ_[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat), (GLvoid*)0);
}

void draw() {
    glBindVertexArray(this->VAO);

    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    glEnable(GL_PROGRAM_POINT_SIZE);

    //std::cout << bufferXYZ_.size();
    glDrawArrays(GL_POINTS, 0, m_Total);

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

(bufferXYZ_ is an array of GLfloat that contains points, and I have bufferColor_ that contains the color associated points in the good order).


Solution

  • Yes, in order to associate the color for each vertex, you need to use shaders.

    You can look at these tutorials on OpenGL. They'll give you a knowledge on the modern OpenGL. In particular, this part shows you how to do what you want.