0

I've written a simple model viewer for android that parses a .obj wavefront file to create a 3d model. I'm exporting a simple icosphere that has been textured with a uv map from blender that comes in this format

v 0.000000 -1.000000 0.000000
v 0.723600 -0.447215 0.525720
v -0.276385 -0.447215 0.850640
.
.
.
v -0.723600 0.447215 -0.525720
v 0.276385 0.447215 -0.850640
v 0.894425 0.447215 0.000000
vt 0.238062 0.172144
vt 0.314133 0.212645
vt 0.264721 0.263452
.
.
.
vt 0.176955 0.098382
vt 0.327646 0.115775
vt 0.361262 0.040083
f 103/1 15/2 48/3
f 48/3 44/4 103/1
f 13/5 103/1 44/4
.
.
.
f 47/12 105/8 46/10
f 14/9 46/10 105/8
f 105/8 47/12 15/2

"v" represents the vertices, "vt" represents the texture coordinates and "f" represents the faces with the values before the forward slash being the indices corresponding to the vertices and the values after the slash are the indices corresponding to the texture coordinates. My problem is that I'm not sure how to input the texture coordinate indices into opengl es so my current model looks very strange since the texture coordinates do not match up.

enter image description here

1 Answer 1

0

OBJ defines the origin within a texture to be at the top left. OpenGL defines it to be in the bottom left. You need to invert the y coordinates coming in from the OBJ.

EDIT: in response to what I'm interpreting as scepticism on Christian Rau's part below, for OpenGL purposes and limiting the properties to just those you actually use, a vertex is the combination of a position and a texture coordinate. It's the two things together. You might think they're logically distinct but OpenGL doesn't. You might respond to the question "which OBJ vertex is at entry 23 in the list passed to OpenGL?" with an answer like "it's the vertex 46/10".

One way to implement that when loading is via any of the Dictionary subclasses (eg, Hashtable). When encountering a need to use the vertex 46/10 you'd create a suitable composite key and check the Hashtable for a mapping to the OpenGL array index position. If you failed to find one, you'd allocate the next available index position and store a record in your dictionary.

So the following, from your question:

f 103/1 15/2 48/3
f 48/3 44/4 103/1
f 13/5 103/1 44/4
.
.
.
f 47/12 105/8 46/10
f 14/9 46/10 105/8
f 105/8 47/12 15/2

Gives rise to ten vertices, those being the vertices 103/1, 13/5, 48/3, 44/4, 15/2, 47/12, 105/8, 46/10, 14/9 and 46/10.

2
  • So you mean the image in his question shows the correct texture just flipped vertically? Mar 24, 2012 at 19:10
  • I'm optimistic that's the case. The texture coordinates have been automatically unwrapped per the question so there's no reason to assume it'd look like it should but upside down. I'll add a discussion of OpenGL vertices since you're clearly sceptical and I've seen you give really good answers on other questions.
    – Tommy
    Mar 24, 2012 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.