我正在尝试修补OBJ加载类,因为它只支持短号,并且我有一个超过短数限制的大OBJ。
我必须更改代码以使用整数向量(而不是短向),并将ShortBuffer更改为IntBuffer。
现在,基本的OBJ(10,000行)曾经在3秒内加载,根本不再加载并给我一个BufferOverflowException。
以下是有问题的代码块:
ByteBuffer fBuf = ByteBuffer.allocateDirect(faces.size() * 2);
fBuf.order(ByteOrder.nativeOrder());
faceBuffer = fBuf.asIntBuffer();
faceBuffer.put(toPrimitiveArrayS(faces)); // this gives the exception
faceBuffer.position(0);
下面是函数 toPrimitiveArrayS
private static int[] toPrimitiveArrayS(Vector<Integer> vector){
int[] s;
s = new int[vector.size()];
for (int i=0; i<vector.size(); i++){
s[i] = vector.get(i);
}
return s;
}
奇怪的是,如果我在allocateDirect中将2更改为4,那么模型会加载,但会被错误地渲染(以前,它加载时该数字为2,所以我只是将其更改为看)
以下完整课程代码:
public class TDModelPart {
Vector<Integer> faces;
Vector<Integer> vtPointer;
Vector<Integer> vnPointer;
Material material;
private FloatBuffer normalBuffer;
IntBuffer faceBuffer;
public TDModelPart(Vector<Integer> faces, Vector<Integer> vtPointer, Vector<Integer> vnPointer, Material material, Vector<Float> vn) {
super();
this.faces = faces;
this.vtPointer = vtPointer;
this.vnPointer = vnPointer;
this.material = material;
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vnPointer.size() * 4 * 3);
byteBuf.order(ByteOrder.nativeOrder());
normalBuffer = byteBuf.asFloatBuffer();
for(int i=0; i<vnPointer.size(); i++){
float x=vn.get(vnPointer.get(i)*3);
float y=vn.get(vnPointer.get(i)*3+1);
float z=vn.get(vnPointer.get(i)*3+2);
normalBuffer.put(x);
normalBuffer.put(y);
normalBuffer.put(z);
}
normalBuffer.position(0);
ByteBuffer fBuf = ByteBuffer.allocateDirect(faces.size() * 2);
fBuf.order(ByteOrder.nativeOrder());
faceBuffer = fBuf.asIntBuffer();
faceBuffer.put(toPrimitiveArrayS(faces));
faceBuffer.position(0);
}
public String toString(){
String str=new String();
if(material!=null)
str+="Material name:"+material.getName();
else
str+="Material not defined!";
str+="\nNumber of faces:"+faces.size();
str+="\nNumber of vnPointers:"+vnPointer.size();
str+="\nNumber of vtPointers:"+vtPointer.size();
return str;
}
public IntBuffer getFaceBuffer(){
return faceBuffer;
}
public FloatBuffer getNormalBuffer(){
return normalBuffer;
}
private static int[] toPrimitiveArrayS(Vector<Integer> vector){
int[] s;
s = new int[vector.size()];
for (int i=0; i<vector.size(); i++){
s[i] = vector.get(i);
}
return s;
}
public int getFacesCount(){
return faces.size();
}
public Material getMaterial(){
return material;
}
}
答案 0 :(得分:0)
你为每个面部索引分配两个字节(16位),你应该分配四个,因为你每个索引使用一个int(一个int是32位整数,四个字节),你试图分配双倍数量你的faceBuffer可以处理的字节数