I'm loading OBJs, which lend themselves well to setting up as indexed VBOs, since each vertex is only defined once and then a face definition will reference the same vertex more than once when they share an edge. I want to define face colors based on the materials loaded from the OBJ. This is quite easy in immediate mode, but I've realized that using VBOs will require me to duplicate each vertex in order to have two adjacent faces with different colors. What's the best way to go about solving this without duplicating vertices?


//Get the VBO from the GPU
glBindBuffer(GL_ARRAY_BUFFER, vbo);
//Tell OpenGL what to expect from the VBO
glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0); //Location
glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.SIZE * 4, 4 * 3); //Texture Coord
glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.SIZE * 4, 4 * 3 + 4 * 2); //Normal

glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
  • \$\begingroup\$ You could store the color inside each face and render each in a separate draw call. Then set a uniform in the shader that is the face color for each draw (assuming you are using shaders). \$\endgroup\$ – glampert Oct 17 '14 at 4:23
  • \$\begingroup\$ I am using shaders, but how would I access each face when I'm using glDrawElements() with my GL_ELEMENT_ARRAY_BUFFER and the indices of each vertex? \$\endgroup\$ – winduptoy Oct 17 '14 at 4:39
  • 1
    \$\begingroup\$ However, if you really need a single color per face, then it is probably better to duplicate that color in the vertex, as performing one draw call per face is quite inefficient, as you might have imagined. \$\endgroup\$ – glampert Oct 17 '14 at 4:49
  • 1
    \$\begingroup\$ It looks like my only option is to duplicate the vertex. \$\endgroup\$ – winduptoy Oct 17 '14 at 5:09
  • 1
    \$\begingroup\$ In OpenGL, vertices are only duplicates if every vertex attribute is the same: positions, texture coords, normals, colors, and any other custom attribs in use. You can't index some attributes but not others, or use multiple indices. \$\endgroup\$ – bcrist Oct 17 '14 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.