Laurent Crivello
Laurent Crivello

Reputation: 3931

Unable to set colors in OpenGL

I have an OpenGL scene, made as VBO and IBO. My VBO is a serie of 3 floats: x,y,z,x,y,z,x,y,z... In addition to that, I have a color array made of series of 3 floats: r,g,b,r,g,b,r,g,b...

My goal is that the first vertice (x=vertice[0], y=vertice[1], z=vertice[2]) is linked to the first color (r=color[0], g=color[1], b=color[2]). I am however unable to display the colors with the code below:

    glGenBuffers(1, &VertexVBOID);

    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*nbVertex*3, glVertex, GL_STATIC_DRAW);

    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glVertexPointer(3, GL_FLOAT,0,0);
    glColorPointer(3, GL_FLOAT, 0, glColors);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);

    glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, 0);

Is there an issue in the code above, or in the way my array is set up ?

Upvotes: 1

Views: 500

Answers (1)

genpfault
genpfault

Reputation: 52166

Give this sequence a try:

glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glVertexPointer(3, GL_FLOAT,0,0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glColorPointer(3, GL_FLOAT, 0, glColors);

Also, look at the docs for glBindBuffer():

Buffer object names are unsigned integers. The value zero is reserved, but there is no default buffer object for each buffer object target. Instead, buffer set to zero effectively unbinds any buffer object previously bound, and restores client memory usage for that buffer object target.

Upvotes: 3

Related Questions