Pointer issues

I’m still having trouble dealing with “pointers” in LWJGL. Specifically, my current issue:

I’m trying to use some billboarding, so I need to retrieve the model view matrix using glGetFloatv…

Here’s what I tried to do:


float[] matrix = new float[16];
FloatBuffer buf = ByteBuffer.allocateDirect(16*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
buf.put(matrix);
int bufPtr = Sys.getDirectBufferAddress(buf);
gl.getFloatv(GL.MODELVIEW_MATRIX, bufPtr);

yet, matrix is still filled with 0’s. I’m assuming that I am not filling the matrix correctly, but I thought that was the point of getting the pointer.

What am I forgetting/doing wrong?

Without checking myself, i’d say you’ve misunderstood how the buffers work. They’re a linear array in their own right, so you can treat them as such for filling and retreiving info.

The creation looks right, but the buff.put() isn’t needed, you’re basically filling it with the blank matrix you created earlier. Getting the pointer looks fine as well, you’ll probably find the values you want are sitting waiting for you in the buffer, so do something like buff.get(matrix) to fill your plain float array with the values. (may need a rewind() beforehand, but i doubt it).

get() and put() are just memory copys, you don’t bind an existing array to a buffer (for that, you’d need something like Cas’ struct…)

Your code should look like this for it to work, I fell over on this very topic myself till Cas helped me out.


float[] proj = new float[16];
float[] modl = new float[16];

FloatBuffer projBuf = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
FloatBuffer modlBuf = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();

gl.getFloatv(GL.PROJECTION_MATRIX, Sys.getDirectBufferAddress(projBuf));

gl.getFloatv(GL.MODELVIEW_MATRIX, Sys.getDirectBufferAddress(modlBuf));

projBuf.rewind();
modlBuf.rewind();

projBuf.get(proj);
modlBuf.get(modl);

...rest of code here...


Regards,

Andy.

Thanks for the help you two. Although, I found that I didn’t need to rewind anything as Andy suggested. Any ideas when you would need to and when you wouldn’t? Is there a speed difference?

You didn’t need it because the buffers were newly created. They are needed whenever you perform a relative operation on them, and I can hardly imagine that they are a performance bottleneck.

  • elias

Elias is right; you need to have the rewind() in there if the code is called more than once, my logic was in my calculateFrustum call which is done each frame, hence the rewind.

Andy