gpt4 book ai didi

jar - LWJGL 可执行 jar 启动然后崩溃

转载 作者:行者123 更新时间:2023-12-03 01:21:00 24 4
gpt4 key购买 nike

我希望得到一些帮助。我为类开发了这个程序,它读取四个 .obj 文件和一个纹理文件。它在我的 IDE (Eclipse) 中工作正常,但是当我导出它并使用 JavaSplice 创建最终的可执行 jar 时,它会启动,显示一个显示窗口一小会儿,然后就崩溃了。经过一些测试和仔细的评论,我确定它不是在拼接端,而是在我的代码上。通过评论,我确定问题出在读取四个 .obj 的显示列表中:

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,但仍然不起作用。我想我不能使用任何类型的阅读器。那么我怎样才能阅读这些行呢?:

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;
}
}

最佳答案

打开命令提示符,输入:

java -jar "pathToYourJar.jar"

通过这个你可以看到代码抛出了什么异常。

关于崩溃,我认为可以使用InputStreams而不是文件来解决问题。

更改:

BufferedReader reader = new BufferedReader(new InputStreamReader
(new FileInputStream(f)));

进入

String path = "/package1/package2/file.obj";
InputStream source = ClassLoader.class.getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader
(source);

这在 Eclipse 和 JAR 中都可以工作。

关于jar - LWJGL 可执行 jar 启动然后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com