0
我希望對此有所幫助。我開發了這個程序,用於讀取四個.obj文件和一個紋理文件。它在我的IDE(Eclipse)中正常工作,但是當我導出它並使用JavaSplice創建最終的可執行jar時,它會啓動,顯示窗口幾分之一秒,然後崩潰。通過一些測試和仔細評論,我確定它不在拼接結束,而是我的代碼。隨着評論,我已經確定,問題在於,在四大.OBJ讀取顯示列表:LWJGL可執行文件啓動然後崩潰
int objectOneDisplayList = glGenLists(1);
glNewList(objectOneDisplayList, GL_COMPILE);
{
Model m = null;
try
{
m = OBJLoader.loadModel(new File("res/circle.obj"));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
Display.destroy();
}
catch(IOException e)
{
e.printStackTrace();
Display.destroy();
}
glBegin(GL_TRIANGLES);
for(Face face : m.faces)
{
glColor3f(0.0f, 0.75f, 0.0f);
Vector3f n1 = m.normals.get((int) face.normal.x - 1);
glNormal3f(n1.x, n1.y, n1.z);
Vector3f v1 = m.vertices.get((int) face.vertex.x - 1);
glVertex3f(v1.x, v1.y, v1.z);
Vector3f n2 = m.normals.get((int) face.normal.y - 1);
glNormal3f(n2.x, n2.y, n2.z);
Vector3f v2 = m.vertices.get((int) face.vertex.y - 1);
glVertex3f(v2.x, v2.y, v2.z);
Vector3f n3 = m.normals.get((int) face.normal.z - 1);
glNormal3f(n3.x, n3.y, n3.z);
Vector3f v3 = m.vertices.get((int) face.vertex.z - 1);
glVertex3f(v3.x, v3.y, v3.z);
}
glEnd();
}
glEndList();
還有那些只是用不同的文件名和目錄名中的四個。現在這裏是我的OBJLoader類:
package base;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.lwjgl.util.vector.Vector3f;
public class OBJLoader
{
public static Model loadModel(File f) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new FileReader(f));
Model m = new Model();
String line;
while((line = reader.readLine()) != null)
{
if(line.startsWith("v "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.vertices.add(new Vector3f(x, y, z));
}
else if(line.startsWith("vn "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.normals.add(new Vector3f(x, y, z));
}
else if(line.startsWith("f "))
{
Vector3f vertexIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
Float.valueOf(line.split(" ")[2].split("/")[0]),
Float.valueOf(line.split(" ")[3].split("/")[0]));
Vector3f normalIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
Float.valueOf(line.split(" ")[2].split("/")[2]),
Float.valueOf(line.split(" ")[3].split("/")[2]));
m.faces.add(new Face(vertexIndices, normalIndices));
}
}
reader.close();
return m;
}
}
我認爲這是因爲我需要使用InputFileStream。有人能告訴我如何使用它來重寫?
編輯:重寫OBJLoader不使用FileReader,仍然沒有工作。我想我不能使用任何種類的Reader。我怎樣才能讀取線然後?:
public class OBJLoader
{
public static Model loadModel(File f) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
Model m = new Model();
String line;
while((line = reader.readLine()) != null)
{
if(line.startsWith("v "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.vertices.add(new Vector3f(x, y, z));
}
else if(line.startsWith("vn "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.normals.add(new Vector3f(x, y, z));
}
else if(line.startsWith("f "))
{
Vector3f vertexIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
Float.valueOf(line.split(" ")[2].split("/")[0]),
Float.valueOf(line.split(" ")[3].split("/")[0]));
Vector3f normalIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
Float.valueOf(line.split(" ")[2].split("/")[2]),
Float.valueOf(line.split(" ")[3].split("/")[2]));
m.faces.add(new Face(vertexIndices, normalIndices));
}
}
reader.close();
return m;
}
}