JNI GetDirectBuffer won´t work for me

Hi!

I tried to use the Java NIO buffers in a real simple way and it didn´t work :frowning:

I am doing this on the c++ side:

 /*
     * Class:     Renderer_RenderDevice
     * Method:    setModelViewProjectionMatrix
     * Signature: (Ljava/nio/FloatBuffer;)V
     */
    JNIEXPORT void JNICALL Java_Renderer_RenderDevice_setModelViewProjectionMatrix
            (JNIEnv* env, jclass c, jobject matrixBuffer) {
        
        float* matrix = (float*)env->GetDirectBufferAddress(matrixBuffer);
        //glLoadMatrixf( matrix );

		if ( matrix == 0 ) {
			std::cout << "Error" << std::endl;
			return;
		}

		for(int i=0; i<16; i++) {
			std::cout << matrix[i] << std::endl;
		}
    }

and that on the java side:

 RenderDevice.setModelViewProjectionMatrix(FloatBuffer.allocate(16));

This is MEANT to work, but it will always return “Error” because matrix* is null.
All code examples I found on the net were not different to mine.

I am using java 1.6 and other libs like lwjgl work - so its not the runtime.

But where is my mistake ? I am puzzled - theres not much to do wrong.

Help is very appreciated,
Frederick

[EDIT: I am using MSVC++ Express Edition to compile the c++]

FloatBuffer.allocate() is NOT direct.

Try this:
ByteBuffer.allocateDirect(floats * 4).byteOrder(ByteOrder.nativeOrder()).asFloatBuffer();

wow! wow whoaaaa! :o :o :o

That was lightning speed ! Thanks !

I did not get that nio buffers may be direct or non-direct. I assumed them all as “direct” accessible. I was so confused
because I used lwjgl before, which works with buffers allocated by FloatBuffer.allocate(). The library seems to wrap them
internally.

Btw. there was one small typo:

FloatBuffer f = ByteBuffer.allocateDirect(#floats * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();

So “order” instead of “byteOrder”.

Complicated stuff… I would have had a hard time figuring that out on my own… so big THANKS to you!

LJWGL has BufferUtils.newFloatBuffer(int). It returns a direct buffer.

Aren’t you confused with this one?

Hi Riven,

I checked it - I actually used “FloatBuffer.allocate” and it works. So I assume LWJGL does some
wrapping internally. Out of my memory, there are function calls in the jni code, which possibly serve
this purpose.

a recent commit allows lwjgl to work with either type