我已经将旧代码转换为新代码并删除了丑陋的代码部分,但它不再起作用了。愿你能帮助我。太好了!
OLD ONE - Header
private ArrayList vertexsets;
private ArrayList vertexsetsnorms;
private ArrayList vertexsetstexs;
private ArrayList faces;
private ArrayList facestexs;
private ArrayList facesnorms;
private ArrayList mattimings;
private MtlLoader materials;
private String mtl_path;
老功能
if (newline.startsWith("f ")) {
facecounter++;
newline = newline.substring(2, newline.length());
StringTokenizer st = new StringTokenizer(newline, " ");
int count = st.countTokens();
int v[] = new int[count];
int vt[] = new int[count];
int vn[] = new int[count];
for (int i = 0; i < count; i++) {
char chars[] = st.nextToken().toCharArray();
StringBuffer sb = new StringBuffer();
char lc = 'x';
for (int k = 0; k < chars.length; k++) {
if (chars[k] == '/' && lc == '/')
sb.append('0');
lc = chars[k];
sb.append(lc);
}
StringTokenizer st2 = new StringTokenizer(sb.toString(), "/");
int num = st2.countTokens();
v[i] = Integer.parseInt(st2.nextToken());
if (num > 1)
vt[i] = Integer.parseInt(st2.nextToken());
else
vt[i] = 0;
if (num > 2)
vn[i] = Integer.parseInt(st2.nextToken());
else
vn[i] = 0;
}
faces.add(v);
facestexs.add(vt);
facesnorms.add(vn);
}
我添加了一个名为Face的新类,它包含v,vt和vn。
public class Face {
public ArrayList<Float[]> normalIndicies; //vn
public ArrayList<Float[]> textureCoordIndicies; //vt
public ArrayList<Float[]> vertexIndicies; //v
}
NEW ONE - Header
protected ArrayList<Vector3f> verticies;
protected ArrayList<Vector3f> normals;
protected ArrayList<Vector3f> textureCoords;
protected LinkedHashMap<String, Integer> material;
protected ArrayList<Face> faces;
protected String mtlLib;
NEW ONE - 功能
if(line.startsWith("f ")) {
Face face = new Face();
face.vertexIndicies = new ArrayList<Float[]>();
face.textureCoordIndicies = new ArrayList<Float[]>();
face.normalIndicies = new ArrayList<Float[]>();
faceCounter++;
Float[] vertexIndices = new Float[3];
Float[] textureIndex = new Float[3];
Float[] normalIndices = new Float[3];
String[] firstVec = line.split(" ")[1].split("/");
String[] secondVec = line.split(" ")[2].split("/");
String[] thirdVec = line.split(" ")[3].split("/");
for(int i = 0; i < firstVec.length; i++) {
if(i == 0) vertexIndices[0] = Float.valueOf(firstVec[i]);
if(i == 1) textureIndex[0] = Float.valueOf(firstVec[i]);
if(i == 2) normalIndices[0] = Float.valueOf(firstVec[i]);
}
for(int i = 0; i < secondVec.length; i++) {
if(i == 0) vertexIndices[1] = Float.valueOf(secondVec[i]);
if(i == 1) textureIndex[1] = Float.valueOf(secondVec[i]);
if(i == 2) normalIndices[1] = Float.valueOf(secondVec[i]);
}
for(int i = 0; i < thirdVec.length; i++) {
if(i == 0) vertexIndices[2] = Float.valueOf(thirdVec[i]);
if(i == 1) textureIndex[2] = Float.valueOf(thirdVec[i]);
if(i == 2) normalIndices[2] = Float.valueOf(thirdVec[i]);
}
face.vertexIndicies.add(vertexIndices);
face.textureCoordIndicies.add(textureIndex);
face.normalIndicies.add(normalIndices);
this.faces.add(face);
}
答案 0 :(得分:0)
原始数据以整数格式提供,但您希望新代码中的数据是浮点数。由于此代码似乎是obj解析器的一部分,因此正确的数据类型是Integers。面线将索引存储到其他数组(位置,tex-coords等)。
关于重写的其他评论:
分裂本身非常慢,并且您沿着“”(空格)符号将相同的字符串拆分三次。 (更不用说分裂的记忆含义)。
此外,使用循环和if语句看起来很奇怪:
for(int i = 0; i < firstVec.length; i++) {
if(i == 0) vertexIndices[0] = Float.valueOf(firstVec[i]);
if(i == 1) textureIndex[0] = Float.valueOf(firstVec[i]);
if(i == 2) normalIndices[0] = Float.valueOf(firstVec[i]);
}
与
相同vertexIndices[0] = Float.valueOf(firstVec[0]);
textureIndex[0] = Float.valueOf(firstVec[1]);
normalIndices[0] = Float.valueOf(firstVec[2]);
没有不必要的循环和分支。