2012-03-28 41 views
0

对于为顶点,颜色,光照等分配ByteBuffer内存,每次我需要执行以下操作。为ByteBuffer分配一个辅助函数android OpenGL ES

 ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);  
     bBuff.order(ByteOrder.nativeOrder()); 
     vertBuff=bBuff.asFloatBuffer(); 
     vertBuff.put(vertices); 
     vertBuff.position(0); 

     ByteBuffer bColorBuff=ByteBuffer.allocateDirect(colorVals.length*4); 
     bColorBuff.order(ByteOrder.nativeOrder()); 
     colorBuff=bColorBuff.asFloatBuffer(); 
     colorBuff.put(colorVals); 
     colorBuff.position(0); 
     ....... 

所以我尝试了一个这样的辅助函数。

private void fBFromFA(float array[], int size, FloatBuffer theBuff){ 

     ByteBuffer bBuff=ByteBuffer.allocateDirect(array.length*size);  
     bBuff.order(ByteOrder.nativeOrder()); 
     theBuff=bBuff.asFloatBuffer(); 
     theBuff.put(array); 
     theBuff.position(0); 

} 

但它没有工作。它给了我一个java.lang.NullPointerException指出源 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);里面的空洞draw(GL10 gl)方法。

回答

1

您试图使用theBuff作为输出参数,但Java没有这些参数。相反,返回bBuff

而且没有必要通过size。 Java中的浮点数总是4个字节。如果你不想要文字,使用Float.SIZE/8(因为它是位数)。