Загрузчик файлов Java .OBJ

У меня довольно длинный вопрос, и я надеюсь, что кто-то может мне помочь. У меня возникли проблемы с загрузкой файла obj в нужный формат. Я пытаюсь загрузить вершины файла и координаты текстуры в массив из следующего:

общественный класс Вершина {

private float x;
private float y;
private float z;

private float u;
private float v;

public Vertex(float x, float y, float z, float u, float v) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.u = u;
    this.v = v;
}

public float getX() {
    return x;
}
public float getY() {
    return y;
}
public float getZ() {
    return z;
}

public float getU() {
    return u;
}

public float getV() {
    return v;
}
}

После того, как это будет сделано, мне нужны все индексы в массиве из следующего:

public class Index3 {

private int i1;
private int i2;
private int i3;

public Index3(int i1, int i2, int i3) {
    this.i1 = i1;
    this.i2 = i2;
    this.i3 = i3;
}

public int getI1() {
    return i1;
}

public int getI2() {
    return i2;
}

public int getI3() {
    return i3;
}
}

У меня эти два массива отлично рисуются на экране, когда я сам кодирую массивы, но мой загрузчик obj явно неверен.

public class OBJLoader {

public static Mesh loadModel(String fileName) {
    FileReader fr = null;
    try {
        fr = new FileReader(new File("res/models/" + fileName));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    BufferedReader reader = new BufferedReader(fr);
    String line = null;

    List<Index3> indices = new ArrayList<Index3>();

    List<OBJFace> objFaces = new ArrayList<OBJFace>();

    List<Vector3f> vs = new ArrayList<Vector3f>();
    List<Vector2f> vts = new ArrayList<Vector2f>();
    List<Vector3f> vns = new ArrayList<Vector3f>();

    try {
        while ((line = reader.readLine()) != null) {
            String[] split = line.split(" ");
            if (line.startsWith("v ")) {
                Vector3f v = new Vector3f();
                v.setX(Float.parseFloat(split[1]));
                v.setY(Float.parseFloat(split[2]));
                v.setZ(Float.parseFloat(split[3]));
                vs.add(v);
            } else if (line.startsWith("vt ")) {
                Vector2f vt = new Vector2f();
                vt.setX(Float.parseFloat(split[1]));
                vt.setY(Float.parseFloat(split[2]));
                vts.add(vt);
            } else if (line.startsWith("vn ")) {
                Vector3f vn = new Vector3f();
                vn.setX(Float.parseFloat(split[1]));
                vn.setY(Float.parseFloat(split[2]));
                vn.setZ(Float.parseFloat(split[3]));
                vns.add(vn);
            } else if (line.startsWith("f ")) {
                objFaces.add(parseFace(split[1]));
                objFaces.add(parseFace(split[2]));
                objFaces.add(parseFace(split[3]));
                String[] s1 = split[1].split("/");
                String[] s2 = split[2].split("/");
                String[] s3 = split[3].split("/");
                indices.add(new Index3(Integer.parseInt(s1[0]), Integer.parseInt(s2[0]), Integer.parseInt(s3[0])));
            }
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Vertex[] vertices = new Vertex[objFaces.size()];
    Index3[] finalIndices = new Index3[indices.size()];

    for (int i = 0; i < objFaces.size(); i++) {
        OBJFace current = objFaces.get(i);

        Vector3f currentPos;
        Vector2f currentTex;
        Vector3f currentNorm;

        //FIX AUTOGEN NORMALS AND TEX COORDS
        if (current.getVertex() != -1)
            currentPos = vs.get(current.getVertex());
        else
            currentPos = new Vector3f(0, 0, 0);
        if (current.getTexCoord() != -1)
            currentTex = vts.get(current.getTexCoord());
        else
            currentTex = new Vector2f(1, 1);
        if (current.getNormal() != -1)
            currentNorm = vns.get(current.getNormal());         
        else 
            currentNorm = new Vector3f(0, 1, 0);

        Vertex newVert = new Vertex(currentPos.getX(), currentPos.getY(), currentPos.getZ(), currentTex.getX(), currentTex.getY());

        vertices[i] = newVert;
    }

    for (int i = 0; i < indices.size(); i++) {
        finalIndices[i] = indices.get(i);
    }

    return new Mesh(vertices, finalIndices);
}

private static OBJFace parseFace(String face) {
    String[] split = face.split("/");

    int vi = -1;
    int vti = -1;
    int vni = -1;

    vi = Integer.parseInt(split[0]) - 1;

    if (split.length > 1) {
        if (!split[1].isEmpty()) {
            vti = Integer.parseInt(split[1]) - 1;
        }
    }

    if (split.length > 2) {
            vni = Integer.parseInt(split[2]) - 1;
    }

    return new OBJFace(vi, vti, vni);
}
}

Когда я рисую куб из файла obj с помощью этого загрузчика, в области куба случайным образом рисуется очень искаженная группа треугольников.

В итоге спрашиваю были ли в загрузчике ошиблись и как можно исправить.

Спасибо за любую помощь!!!


person redonkulasman    schedule 20.11.2014    source источник
comment
вы можете проверить мой ответ по этой ссылке, он отлично работает для .obj в Android, его в java, поэтому с обычной java должно быть легко работать с небольшой работой stackoverflow.com/questions/22316440/   -  person JRowan    schedule 20.11.2014
comment
Большое спасибо! Похоже, это должно помочь мне исправить мой загрузчик.   -  person redonkulasman    schedule 20.11.2014
comment
да они очень похожи :)   -  person JRowan    schedule 20.11.2014