glTexImage2D and NIO Buffer problem

I have a problem I haven’t been able to figure out, and can’t seem to find a solution with google either.

In the first method I retrieve some floating point values from a FloatBuffer and place them into an array, which I pass to the method which it then places back into a direct byte buffer with the native byte ordering.

This works but seems horribly inefficient so after looking at the JCanyon demo, and a little bit of coding, I create method two.

Method two takes the float buffer that I would other wise be reading the values from and passes it straight to glTexImage2D, but for some reason unknown to me this creates a garbled texture. It seems like it should work, but there’s definitely something I’m missing.



//depending on available extensions
//float_texture_format = GL.GL_LUMINANCE_FLOAT16_ATI || GL.GL_LUMINANCE16F_ARB

public void method_1(int[] texture_id, float[] texture_data, int id_index, int dimx, int dimy, GLDrawable drawable)
{
            ByteBuffer b = ByteBuffer.allocateDirect(texture_data.length * 4);
            b.order(ByteOrder.nativeOrder());
            FloatBuffer fbTex = b.asFloatBuffer();
            
            fbTex.put(texture_data);
            
            gl.glBindTexture(GL.GL_TEXTURE_2D, texture_id[id_index]);
            gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, float_texture_format, dimx, dimy, 0, GL.GL_LUMINANCE, GL.GL_FLOAT, fbTex);
}

that works…
…but this doesn’t work



//depending on available extensions
//float_texture_format = GL.GL_LUMINANCE_FLOAT16_ATI || GL.GL_LUMINANCE16F_ARB

public void method_2(int[] texture_id, FloatBuffer texture_data, int id_index, int dimx, int dimy, GLDrawable drawable)
{
        GL gl = drawable.getGL();
        
        gl.glBindTexture(GL.GL_TEXTURE_2D, texture_id[id_index]);
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, float_texture_format, dimx, dimy, 0, GL.GL_LUMINANCE, GL.GL_FLOAT, texture_data);
}

If anyone could please help me out I’d really appreciate it.

Thank you in advance.

It sounds like the data in the second buffer might have been placed in it using the wrong byte order. That would be in the code surrounding your second example, not directly in that code. Keep in mind that if you’ve memory-mapped a file from disk, switching the byte order of the ByteBuffer before viewing it as another type will have no effect when passing that Buffer down to JOGL. The native code for the OpenGL binding ignores the byte order of direct buffers, so the byte order of the data on disk is what will matter.

That’s what I was afraid of, and I am using a memory mapped file. Is there some way of changing the byte ordering that OpenGL expects? It would seem that glPixelStorei with GL_UNPACK_LSB_FIRST would be the setting I want but that didn’t have any affect.

I think GL_UNPACK_LSB_FIRST refers to bits within a byte, not bytes within a word. You may be able to get what you want with GL_UNPACK_SWAP_BYTES.

That worked perfectly, thank you very much. Though I could have swore that I tried that once before, well apparently not.

Thank you again!