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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.