我正在編寫一個3D引擎,我的OBJ LoaderClass似乎在更復雜的模型中遇到了問題。OBJ Loader IndexOutOfBounds
我得到一個IndexOutOfBoundsException,我不知道爲什麼。 索引3522上ArrayList紋理的值似乎會導致此異常,但爲什麼?
這裏是我的OBJ裝載機類
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文件後,除了最後兩個,我無法順序查找。
這裏是例外即時得到
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)
我只是困惑,我不知道爲什麼我得到這個例外,在此先感謝任何線索
如果有興趣,[點擊這裏](https://github.com/java-graphics/assimp)我們有一個jimm端口的assimp,obj已經被支持了 – elect