0

For allocating ByteBuffer memory for vertices, colors, lighting, etc everytime I need to do the following.

      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);
        .......

So I tried making a helper function something like this.

   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);

}

But it didnt work. it gave me a java.lang.NullPointerException pointing the source at gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff); inside the void draw(GL10 gl) method.

jmishra
  • 2,086
  • 2
  • 24
  • 38

1 Answers1

1

You're trying to use theBuff as an output parameter, but Java doesn't have these. Instead, return bBuff.

And there's no need to pass in size. Floats are always 4 bytes in Java. If you don't want the literal, use Float.SIZE / 8 (because it's a count of bits).

parsifal
  • 11
  • 1