0

I just solved a bug and I'm not sure why. Creating a 4x4 projection matrix in LWJGL, for use in a vertex shader..

This line causes problems. It fails silently and my mat4 in the shader is stuck as all zeros (as if it was never written).

FloatBuffer mProj = ByteBuffer.allocate(4*16).asFloatBuffer();

This works as expected.

FloatBuffer mProj = BufferUtils.createFloatBuffer(16);

As a sanity-check, I confirmed that my floats are 4 bytes. So what's the difference?

2
  • What is BufferUtils? A lwjgl class? Commented Jun 10, 2014 at 0:29
  • Good question. The class is org.lwjgl.BufferUtils Commented Jun 10, 2014 at 0:30

1 Answer 1

3

The only difference between those two is possibly the byte order. You can set it as

FloatBuffer mProj = ByteBuffer.allocate(4 * 16).order(ByteOrder.nativeOrder()).asFloatBuffer();
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly it, thank you! I had looked at the documentation for ByteBuffer.asFloatBuffer, but it made no mention of byte order there.

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.