LWGL getFloatv usage?

I’m implementing my Occulsion/View Frustum Culling on LWGL, but I can’t seem to figure out how gl.getFloatv works.

It returns ints, which I believe connect somehow to Matrix4f and the float values m04 and so on.

Now my problem is, is there any way to load from getFloatv directly into Matrix4f? I can’t figure out how those ints in the docs work, and I scanned through all the significant documents and found nothing. Chman’s site seems to be down.

No, there’s no way to do it directly sadly.

glFloatv returns nothing but it takes a pointer - to a direct floatbuffer no less - from which you can load the Matrix4f.


FloatBuffer fb = ByteBuffer.allocateDirect(16 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
gl.getFloatv(GL.PROJECTION_MATRIX, Sys.getDirectBufferAddress(fb));
Matrix4f projectionMatrix = new Matrix4f();
projectionMatrix.load(fb);

or something like that. Don’t forget to cache everything.

Edit: updated the example to say 16 * 4 as elias points out below, it’s in bytes not floats :-[

Cas :slight_smile:

What exactly do you mean by cahce everything?

That code doesn’t work as it is, so perpahs I should learn this NIO stuff from the beginning. If I use get() on the fb I get float number around 1.8f, but every time I get BufferUnderflowException. Should I have some sort of loop, which gives me all of the numbers in the proj matrix?

I’m basically step dancing on this ice right here, obviously I don’t understand this whole thing. Can someone point me to a float buffer tutorial?

I think that it’s ByteBuffer.allocateDirect(16*4), not 16. It’s counting in bytes afaik.

  • elias

Hmm…

it doesn’t work with *4 either… when I set it to 8 it works, but my matrix is empty… Now what exactly does the load function do? It doesn’t set the matrix values doesn’t it?

my code
FloatBuffer fb = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asFloatBuffer();
gl.getFloatv(GL.PROJECTION_MATRIX, Sys.getDirectBufferAddress(fb));
System.out.println(fb.get(gaybuffer));
Matrix4f projectionMatrix = new Matrix4f();

        projectionMatrix.load(fb);
        System.out.println(projectionMatrix.m00 + "," + projectionMatrix.m01 + "," + projectionMatrix.m02 + "," + projectionMatrix.m03);

The gl function requires just a pointer to a bit of memory - we’ve reserved just enough to fit 16 floats in (64 bytes) and wrapped it for Java’s sake with FloatBuffer.

The glGetFloatfv function copies the data directly into the memory you’ve specified. From there, it’s not much use.

The matrix load() function reads from the FloatBuffer at its current position. If you go doing a get() beforehand you’ll advance the position by 1 float and screw it up; you’d have to do a rewind() on the buffer to put it back at the beginning - that’s how Buffers work in Java: they maintain a current position at which the next read or write will occur with put() and get().

Note that wrapping buffers up with asFloatBuffer() creates a direct view of the original data but maintains an independent position.

Get a good read of the Java docs on NIO buffers.

Cas :slight_smile:

Thanks cas… your sheer of knowledge is insane.

I think I got it, thank god its weekend so I can concentrate on java 100%.