我有一个很长的问题,希望有人可以帮助我。我在将obj文件加载到我想要的格式时遇到了一些问题。我正在尝试将文件的顶点和纹理坐标加载到以下数组中:
public class Vertex {
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);
}
}
当我使用这个加载器从obj文件中绘制一个立方体时,在立方体区域中随机绘制一组非常扭曲的三角形。
最终我问在装载机中我是否出错了,我该如何解决呢。
感谢您的帮助!!!