Andrew Weir
Andrew Weir

Reputation: 1030

Android, OpenGL ES 2.0 - Why do you set 4 bytes per float for the ByteBuffer.allocateDirect call?

It's one thing to copy and paste the source code from examples, but I'm looking some answers to why the examples are the way they are.

One question I can't answer is why the ByteBuffer for a triangle requires four bytes per coordinate.

In the example presented by Google a ByteBuffer is instantiated with four bytes per triangle coordinate.

    // initialize vertex byte buffer for shape coordinates
    // (number of coordinate values * 4 bytes per float)
    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);

    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());
  1. Why is it four bytes per coordinate rather than 2 or 8?
  2. What is the purpose of the nativeOrder() call?

Upvotes: 0

Views: 882

Answers (1)

Geobits
Geobits

Reputation: 22342

A float is represented by 32 bits, which equals four bytes.

The call to native order rearranges the bytes in the buffer to whatever the native system's order is, big endian, little endian, etc.

Upvotes: 1

Related Questions