Hi
I wrote a simple OBJ loader but the texcoords are messed up somehow. I can't figure out what is wrong. I tried flipping the texture on the Y-Axis, no success. (Here is how my object looks like: http://imgur.com/d8edSyw
Code: (FaceData holds the IDs for vertices, normals and texcoords, Vertex class holds the data)
public void parseObj(String path)
{
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("").getAbsolutePath() + "/res/" + path));
String line = reader.readLine();
while(line != null)
{
if(line.startsWith("v "))
{
int start = line.indexOf(" ");
String sub = line.substring(start + 1);
String[] vertexStr = sub.split(" " );
pos.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
}
if(line.startsWith("vt "))
{
int start = line.indexOf(" ");
String sub = line.substring(start + 1);
String[] vertexStr = sub.split(" " );
tex.add(new Vector2f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1])));
}
if(line.startsWith("vn "))
{
int start = line.indexOf(" ");
String sub = line.substring(start + 1);
String[] vertexStr = sub.split(" " );
norm.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
}
if(line.startsWith("f "))
{
int start = line.indexOf(" ");
String sub = line.substring(start + 1);
String[] vertexStr = sub.split(" ");
for(int i = 0; i < vertexStr.length; i++)
{
String verStr = vertexStr[i].replaceAll("//", "/0/");
String[] asd = verStr.split("/");
FaceData data = new FaceData();
data.vertexID = Integer.parseInt(asd[0]);
data.texID = Integer.parseInt(asd[1]);
data.normalID = Integer.parseInt(asd[2]);
faces.add(data);
}
}
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for(int i = 0; i < faces.size(); i++)
{
Vertex v = new Vertex();
v.pos = pos.get(faces.get(i).vertexID - 1);
if(faces.get(i).texID != 0)
{
Vector2f t = new Vector2f();
t = tex.get(faces.get(i).texID - 1);
t.y = t.y;
v.texCoord.set(t);
}
else
v.texCoord = new Vector2f(0,0);
v.normal = norm.get(faces.get(i).normalID - 1);
vertices.add(v);
}
}