I am writing a simple OBJ parser. Now I don't know how to deal with the case when #texture_coordinates > #vertices. My OBJ file looks like this:
# Blender3D v249 OBJ File: untitled.blend
# www.blender3d.org
mtllib cube.mtl
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 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.748573 0.750412
vt 0.749279 0.501284
vt 0.999110 0.501077
vt 0.999455 0.750380
vt 0.250471 0.500702
vt 0.249682 0.749677
vt 0.001085 0.750380
vt 0.001517 0.499994
vt 0.499422 0.500239
vt 0.500149 0.750166
vt 0.748355 0.998230
vt 0.500193 0.998728
vt 0.498993 0.250415
vt 0.748953 0.250920
vn 0.000000 0.000000 -1.000000
vn -1.000000 -0.000000 -0.000000
vn -0.000000 -0.000000 1.000000
vn -0.000001 0.000000 1.000000
vn 1.000000 -0.000000 0.000000
vn 1.000000 0.000000 0.000001
vn 0.000000 1.000000 -0.000000
vn -0.000000 -1.000000 0.000000
usemtl Material_ray.png
s off
f 5/1/1 1/2/1 4/3/1
f 5/1/1 4/3/1 8/4/1
f 3/5/2 7/6/2 8/7/2
f 3/5/2 8/7/2 4/8/2
f 2/9/3 6/10/3 3/5/3
f 6/10/4 7/6/4 3/5/4
f 1/2/5 5/1/5 2/9/5
f 5/1/6 6/10/6 2/9/6
f 5/1/7 8/11/7 6/10/7
f 8/11/7 7/12/7 6/10/7
f 1/2/8 2/9/8 3/13/8
f 1/2/8 3/13/8 4/14/8
I know why we can have more texture coordinates than vertices, I just don't know what to do with them especially when rendering with OpenGL (which I do). My Index Buffer looks like this when I parse the obj:
4 0 3
4 3 7
2 6 7
2 7 3
1 5 2
5 6 2
0 4 1
4 5 1
4 7 5
7 6 5
0 1 2
0 2 3
note that I subtracted 1 from all of the indices, since OpenGL wants them in that way. The object draws fine but of course the vertex data is just wrong. I store the vertex data in a dedicated VBO by the way. So in total I have my VAO with 2 VBOs (vertices, textures) and one EBO storing the information which vertecies form a triangle. How to address the texture coordinates now?
EDIT after suggestions:
#So I unpacked the vertices (that means I do have 36 vertices in total) and have the vertex, texture and normal data in one consecutive array. The outcome is this:
My VBO looks like this:
1.000000 1.000000 -1.000000 0.748573 0.750412 0.000000 0.000000 -1.000000
1.000000 -1.000000 -1.000000 0.749279 0.501284 0.000000 0.000000 -1.000000
-1.000000 -1.000000 -1.000000 0.999110 0.501077 0.000000 0.000000 -1.000000
1.000000 1.000000 -1.000000 0.748573 0.750412 0.000000 0.000000 -1.000000
-1.000000 -1.000000 -1.000000 0.999110 0.501077 0.000000 0.000000 -1.000000
-1.000000 1.000000 -1.000000 0.999455 0.750380 0.000000 0.000000 -1.000000
and so on... one row is exactly one vertex with: vertex pos, texture coordinates, normal vector.
So three rows represent one triangle.
I set up by attribute pointers like this:
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * objModel2.getObjData().size(), &objModel2.getObjData()[0], GL_STATIC_DRAW);
// vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(0);
// texture positions
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat) * 3));
glEnableVertexAttribArray(1);
// normals positions
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat) * 5));
glEnableVertexAttribArray(2);
and finally draw the cube like so:
glBindVertexArray(VAO_cube_model_2);
glDrawArrays(GL_TRIANGLES, 0, objModel2.getObjData().size() / 8);
glBindVertexArray(0);
The texture file is the following png file:
Only 3 and 5 gets drawn. I cannot figure out why...