Java OBJ文件加载器

Java OBJ文件加载器,java,wavefront,Java,Wavefront,我有一个很长的问题,希望有人能帮助我。我在将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.

我有一个很长的问题,希望有人能帮助我。我在将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);
}
}
公共类对象加载程序{
公共静态网格加载模型(字符串文件名){
FileReader fr=null;
试一试{
fr=新文件读取器(新文件(“res/models/”+文件名));
}catch(filenotfounde异常){
e、 printStackTrace();
}
BufferedReader读取器=新的BufferedReader(fr);
字符串行=null;
列表索引=新的ArrayList();
List objFaces=new ArrayList();
列表vs=新的ArrayList();
List vts=new ArrayList();
List vns=new ArrayList();
试一试{
而((line=reader.readLine())!=null){
String[]split=line.split(“”);
if(以“v”开头的行){
向量3f v=新向量3f();
v、 setX(Float.parseFloat(split[1]);
v、 setY(Float.parseFloat(split[2]);
v、 setZ(Float.parseFloat(split[3]);
vs.add(v);
}else if(第行开始使用(“vt”)){
Vector2f vt=新的Vector2f();
vt.setX(Float.parseFloat(split[1]);
vt.setY(Float.parseFloat(split[2]);
添加(vt);
}else if(line.startsWith(“vn”)){
Vector3f vn=新的Vector3f();
setX(Float.parseFloat(split[1]);
vn.setY(Float.parseFloat(split[2]);
vn.setZ(Float.parseFloat(split[3]);
添加(vn);
}else if(第行开始,以“f”)){
add(parseFace(split[1]);
add(parseFace(split[2]);
add(parseFace(split[3]);
字符串[]s1=split[1]。split(“/”);
字符串[]s2=split[2]。split(“/”);
字符串[]s3=split[3]。split(“/”);
index.add(新的Index3(Integer.parseInt(s1[0])、Integer.parseInt(s2[0])、Integer.parseInt(s3[0]);
}
}
reader.close();
}捕获(例外e){
e、 printStackTrace();
}
顶点[]顶点=新顶点[objFaces.size()];
Index3[]finalIndices=新的Index3[index.size()];
对于(int i=0;i1){
如果(!split[1].isEmpty()){
vti=Integer.parseInt(split[1])-1;
}
}
如果(拆分长度>2){
vni=Integer.parseInt(split[2])-1;
}
返回新对象面(vi、vti、vni);
}
}
当我使用这个加载程序从一个obj文件中绘制一个立方体时,会在立方体区域随机绘制一组非常扭曲的三角形

最终,我要问的是,我是否在装载机中出错,以及如何修复它


感谢您的帮助!!!

您可以在这个链接中查看我的答案,它非常适用于android中的.obj,它是java的,所以在普通java中使用它应该很容易,只需做一点工作,谢谢分配!这看起来应该可以帮助我修复我的加载程序。是的,它们非常相似:)