# OBJ file, face materials and drawing them with OpenGL

I'm implementing a model class which loads OBJ and MTL files, and ran into an issue or question with face materials.

Consider the following example: It's a cube with 5 sides Gray and 1 side Green.

mtllib Materials.mtl

o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000

usemtl Green
f 7 8 4 3

usemtl Gray
f 5 8 2 1
f 6 7 3 2
f 8 5 1 4
f 1 2 3 4
f 8 7 6 5


The OBJ file obviously shares same vertices for adjacent faces even if the faces get different materials.

When drawing such a cube with OpenGL however, I would need to duplicate the vertices of the adjacent faces with different materials, since every vertex specifies the color it has - and I have two different colors for the same vertices here, depending on the face and the different material it has. If I wouldn't do that, the adjacent faces would smooth over to the different materials.

First: Did I get this correctly or am I missing an OpenGL "feature" here which lets me "specify" the color of a whole triangle / face rather than a single vertex?

Second: If not, is there an easier way to keep track of the faces materials and duplicating the adjacent vertices if they get different materials? What would be the easiest way to implement different materials on one object?

• yes the only way is to duplicate your vertices or your indices – Anthony Raimondo Feb 8 '15 at 4:46
• I see, without that, it probably wouldn't be challenging to load the OBJ files :D – Ray Feb 8 '15 at 10:22
• heres a good tutorial on obj parcing that might help. opengl-tutorial.org/beginners-tutorials/… – Anthony Raimondo Feb 8 '15 at 18:10
• @AnthonyRaimondo thx very much, I was looking for a tut not as techy as the original spec – Ray Feb 8 '15 at 21:33
• @AnthonyRaimondo it doesn't go into detail about material vertex sharing though – Ray Feb 8 '15 at 21:50