Java: FloatBuffer to OpenGL - wrap() vs. allocate(

2019-02-26 09:35发布

问题:

Datasource:

float[] v = { ... };

Working example:

FloatBuffer buf = BufferUtils.createFloatBuffer(v.length);
buf.put(v);
buf.flip(); // or buf.rewind()

The buffer can now be uploaded to opengl and works fine:

...
glBufferData(..., buf, ...);
...

Why do the following examples of the buffer creation not also work?

Not working 1:

FloatBuffer buf = FloatBuffer.wrap(v);

Not working 2:

FloatBuffer buf = FloatBuffer.allocate(v.length);
buf.put(v);
buf.flip(); // or buf.rewind()

Edit:

After browsing the API code, I found out:

In case of the working example memAddress(buf) returns a valid address, but in the other cases it returns just 0.

Additional infos:

why am i getting a FloatBuffer is not direct error?

回答1:

BufferUtils returns a direct buffer whereas the others might not.

You can check the directness of the wrap and allocate methods using isDirect() method. Wrappers are non direct.