0

I'm making a program that given an obj file, it renders the model. For now it works without texture mapping, but i want to implement this capability, reading the material library file.

But i see that the number of vertices can be different from the number of texture coordinates. Look this example:

# Blender v2.58 (sub 0) OBJ File: ''
# www.blender.org
mtllib /home/frenk/Documenti/Progetti/dado.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 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.593772 0.740196
vt 0.593666 0.869263
vt 0.394866 0.869219
vt 0.394973 0.740152
vt 0.792572 0.740240
vt 0.993810 0.740284
vt 0.993703 0.869351
vt 0.792466 0.869307
vt 0.792360 0.995233
vt 0.593560 0.995189
vt 0.593984 0.482061
vt 0.792784 0.482105
vt 0.792678 0.611172
vt 0.593878 0.611129
usemtl dado_verde_dado.tga
s off
f 1/1 2/2 3/3 4/4
f 5/5 8/6 7/7 6/8
f 1/1 5/5 6/8 2/2
f 2/2 6/8 7/9 3/10
f 3/11 7/12 8/13 4/14
f 5/5 1/1 4/14 8/13

So i can't use the function drawElements(), because indices doesn't match. Have i do to map vertices coordinates to texture coordinates, one to one?

 //example
glBegin( GLTriangles );
gltexcoord2f(...); glvertex3f(...);
...
blaBlaBla(...);
...
glEnd();

I think that an efficient API must exist for doing what i want.

optimusfrenk
  • 1,271
  • 2
  • 16
  • 34
  • 1
    ASSIMP library will load .obj files for you and set everything up as you want. You still have to generate the GL objects to use the models, however. – Robinson Apr 21 '12 at 19:53

1 Answers1

0

Unfortunately OpenGL has no efficient API for doing what you want. If you want to use OBJ with vertex arrays/buffers, you will have to decompose all of the vertices and texcoords you are given and reassemble them in arrays such that the number of vertices and the number of texcoords are the same, duplicating vertices and texcoords as needed.

If you search I'm sure you will find several questions on how to do this.

Tim
  • 35,413
  • 11
  • 95
  • 121