OBJ Loader IndexOutOfBounds

时间:2017-05-01 05:00:48

标签: java 3d lwjgl 3d-model

我正在编写一个3D引擎,我的OBJ LoaderClass似乎遇到了更复杂模型的问题。

我得到一个IndexOutOfBoundsException,我无法弄清楚原因。 索引3522上的ArrayList纹理的值似乎会导致此异常,但为什么?

这是我的OBJ Loader Class

        package graphics.renderEngine;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;

    import org.joml.Vector2f;
    import org.joml.Vector3f;

    import graphics.models.RawModel;

    public class OBJLoader 
    {

        public static RawModel loadObjModel(String fileName, Loader loader)
        {
            FileReader fr = null;
            try 
            {
                fr = new FileReader(new File("Ressources/Models/"+fileName+".obj"));
            } 
            catch (FileNotFoundException e) 
            {
                System.err.println("Could not load File!");
                e.printStackTrace();
            }
            BufferedReader reader = new BufferedReader(fr);
            String line;
            List<Vector3f> vertices = new ArrayList<Vector3f>();
            List<Vector2f> textures = new ArrayList<Vector2f>();
            List<Vector3f> normals = new ArrayList<Vector3f>();
            List<Integer> indices = new ArrayList<Integer>();
            float[] verticesArray = null;
            float[] normalsArray = null;
            float[] texturesArray = null;
            int[] indicesArray = null;

            try
            {
                while(true)
                {
                    line = reader.readLine();
                    String[] currentLine = line.split(" ");
                    if(line.startsWith("v "))
                    {
                        Vector3f vertex = new Vector3f(Float.parseFloat(currentLine[1]),Float.parseFloat(currentLine[2]), Float.parseFloat(currentLine[3]));
                        vertices.add(vertex);
                    }
                    else if(line.startsWith("vt "))
                    {
                        Vector2f texture = new Vector2f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]));
                        textures.add(texture);
                    }
                    else if(line.startsWith("vn "))
                    {
                        Vector3f normal = new Vector3f(Float.parseFloat(currentLine[1]),Float.parseFloat(currentLine[2]), Float.parseFloat(currentLine[3]));
                        normals.add(normal);
                    }
                    else if(line.startsWith("f "))
                    {
                        texturesArray = new float[vertices.size()*2];
                        normalsArray = new float[vertices.size()*3];
                        break;
                    }
                }

                while(line != null)
                {
                    if(!line.startsWith("f "))
                    {
                        line = reader.readLine();
                        continue;
                    }
                    String[] currentLine = line.split(" ");
                    String[] vertex1 = currentLine[1].split("/");
                    String[] vertex2 = currentLine[2].split("/");
                    String[] vertex3 = currentLine[3].split("/");

                    processVertex(vertex1, indices, textures, normals, texturesArray, normalsArray);
                    processVertex(vertex2, indices, textures, normals, texturesArray, normalsArray);
                    processVertex(vertex3, indices, textures, normals, texturesArray, normalsArray);
                    line = reader.readLine();
                }
                reader.close();

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            verticesArray = new float[vertices.size()*3];
            indicesArray = new int[indices.size()];

            int vertexPointer = 0;
            for (Vector3f vertex:vertices)
            {
                verticesArray[vertexPointer++] = vertex.x;
                verticesArray[vertexPointer++] = vertex.y;
                verticesArray[vertexPointer++] = vertex.z;
            }

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

            }
            return 
                    loader.loadToVAO
                    (verticesArray, 
                            texturesArray, 
                            normalsArray,
                            indicesArray);

        }

        private static void processVertex(String[] vertexData, List<Integer> indices, List<Vector2f> textures, List<Vector3f> normals, float[] textureArray, float[] normalsArray)
        {
            System.out.println(textures.get(3522));
            int currentvertexPointer = Integer.parseInt(vertexData[0]) -1;
            indices.add(currentvertexPointer);
            Vector2f currentTex = textures.get(Integer.parseInt(vertexData[1])-1);
            textureArray[currentvertexPointer*2] = currentTex.x;
            textureArray[currentvertexPointer*2+1] = 1 - currentTex.y;
            Vector3f currentNorm = normals.get(Integer.parseInt(vertexData[2])-1);
            normalsArray[currentvertexPointer*3] = currentNorm.x;
            normalsArray[currentvertexPointer*3+1] = currentNorm.y;
            normalsArray[currentvertexPointer*3+2] = currentNorm.z;
        }
 }

This is the OBJFile of the Model im trying to load

每当我通过以下方式读出纹理值时:

System.out.println(textures.get(Integer.parseInt(vertexData[1])-1));

我在异常之前得到的最后一个向量是:

( 4.260E-1  1.275E-1)
( 4.650E-1  1.664E-1)
( 4.706E-1  1.621E-1)
( 4.650E-1  1.664E-1)
( 4.925E-1  2.140E-1)
( 1.340E-1  8.170E-2)
( 1.947E-1  4.650E-2)
( 1.902E-1  3.560E-2)

在看了OBJ FIle后,除了最后两个,我无法顺序找到。

这是我得到的例外

java.lang.IndexOutOfBoundsException: Index: 3522, Size: 3522
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at graphics.renderEngine.OBJLoader.processVertex(OBJLoader.java:122)
    at graphics.renderEngine.OBJLoader.loadObjModel(OBJLoader.java:82)
    at main.Main.init(Main.java:150)
    at main.Main.<init>(Main.java:82)
    at main.Main.main(Main.java:75)

我只是感到困惑,我不知道为什么我得到这个例外,提前感谢任何线索

4 个答案:

答案 0 :(得分:1)

列表的大小为3522,索引值的范围为0到3521.但是您尝试使用3522的不存在的索引来访问列表中的元素,因此例外。在访问list元素之前,您需要具备检查索引是否小于size的条件。

答案 1 :(得分:1)

实际问题是你停止阅读&#39; v&#39; vt&#39;和&#39; vn&#39;一旦你看到第一个&#39; f&#39; (面子)声明。你不能这样做,因为Wavefront OBJ规范没有说明&#39; v *&#39;声明需要在“f&f;声明。实际上,在您的示例文件中,它们是混合在一起的。 这意味着,当你停止阅读&#39; v *&#39;你的第一个循环中的声明,你最终会找到&#39; f&#39;声明引用其他&#39; v *&#39;您没有阅读的声明,因此您将获得IndexOutOfBounds异常。 你应该重新构建你的循环,以便阅读&#39; v *&#39;以及&#39; f&#39;声明可以随时进行。

答案 2 :(得分:0)

看起来您正在遵循https://www.youtube.com/user/ThinMatrix

的教程

教程很好;它完美地运作。 (我以前用过它)

问题

问题在于他的OBJLoader特定于他所选择的OBJ文件的格式,而不是&#34;通用的&#34; OBJ-装载机。

他的格式如下: V / VT / VN / F / EOF

而你更像是: V / VT / VN / F / V / VT / VN / F /.../ EOF

您发布的代码假设在找到带有&#39; f&#39;的第一行后,将不再有顶点数据,之后只会有面部数据。

因此,代码不会接收所有顶点数据,因此当您尝试索引该数据时,它就不存在。

修复

有两种可能的解决方案:

  1. 通过允许代码接受文件中的所有数据然后将其加载到缓冲区中,重新配置代码以更一般的方式工作。
  2. 重新构建OBJ文件,使其正确位于ThinMatrix概述的格式中。最简单的方法可能是复制/粘贴,尽管你可以创建一个为你做这个的程序。
  3. 我猜测,因为您正在关注这个主题的教程,所以您没有多少经验,最简单的方法就是2号解决方案。

答案 3 :(得分:0)

检查
返回loader.loadToVAO(verticesArray,textureArray,indicesArray);

这里有一些小的拼写错误,请参见textureArray的拼写 并且您定义了texturesArray,其中有一个。