Java 将ShortBuffer更改为IntBuffer时的BufferOverFlowException
我正在尝试修补一个OBJ加载类,因为它只支持短数字,并且我有一个超过短数字限制的大OBJ 我不得不更改代码以使用整数向量(而不是短向量),并将ShortBuffer更改为IntBuffer 现在,我有一个基本的OBJ(10000行),用来在3秒钟内加载,它不再加载,并给我一个BufferOverflowException 下面是有问题的代码块:Java 将ShortBuffer更改为IntBuffer时的BufferOverFlowException,java,android,buffer,buffer-overflow,wavefront,Java,Android,Buffer,Buffer Overflow,Wavefront,我正在尝试修补一个OBJ加载类,因为它只支持短数字,并且我有一个超过短数字限制的大OBJ 我不得不更改代码以使用整数向量(而不是短向量),并将ShortBuffer更改为IntBuffer 现在,我有一个基本的OBJ(10000行),用来在3秒钟内加载,它不再加载,并给我一个BufferOverflowException 下面是有问题的代码块: ByteBuffer fBuf = ByteBuffer.allocateDirect(faces.size() * 2); fBuf.order(By
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);
下面是TopPrimitiveArray的函数
private static int[]toprimitivearray(向量){
int[]s;
s=新的整数[vector.size()];
对于(int i=0;i您为每个面索引分配两个字节(16位),您应该分配四个,因为您为每个索引使用一个int(int是32位整数,四个字节),您正在尝试分配faceBuffer可以处理的字节数的两倍,那么我需要修复什么?我不能使用ShortBuffer,但使用IntBuffer不起作用,我需要它来将索引上载到图形卡时,您是否指示它们现在是无符号整数?老实说,不知道您的意思。我只是更改了ShortBuffer到IntBuffer。阅读我的问题,这是我所有的信息。我以为你在使用OpenGLES,你是否尝试将TopPrimitiveArray从int[]更改为short[]?该函数已从short[]更改为int[]。所有现在是int的东西,过去都很短。short是旧方法,我需要它是int。是的,它是opengl es,但我是新手,我在android sdk中这样做。我没有将任何东西“上传”到图形卡
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 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;
}
}