Login  Register

Re: gl.bufferData seems not working correctly when floating value was passed as ByteBuffer

Posted by kyasbal on Oct 03, 2018; 4:10am
URL: https://forum.jogamp.org/gl-bufferData-seems-not-working-correctly-when-floating-value-was-passed-as-ByteBuffer-tp4039246p4039250.html

I conclude this was not bug of JOGL. I apologize I have doubted this library.

## Solution and why this problem was happen

I found a solution for this problem. I conclude this problem was happen by difference of byte order between java and native environment.

The floating buffer created as a byte buffer must be ordered with native order with following buffer creation code.

     val bufferSource2 = ByteBuffer.allocate(4 * 6)
     bufferSource2.order(ByteOrder.nativeOrder())
     val floatingSource2 = bufferSource2.asFloatBuffer()
     floatingSource2.put(FloatBuffer.wrap(floatArrayOf(0.5f,0.5f,0.5f,0.5f,-0.5f,0.5f)))
     val buffer2 = gl.createBuffer()
     gl.glBindBuffer(GL3.GL_ARRAY_BUFFER,buffer2)
     gl.glBufferData(GL3.GL_ARRAY_BUFFER,bufferSource2.limit().toLong(),bufferSource2,GL3.GL_STATIC_DRAW)

I think JOGL will consider the order of passed buffer if it was passed as typed buffers. If the floating values are represented as LITTLE_ENDIAN and it should be swapped for native environment, this seems to be converted as BIG_ENDIAN automatically during calling bufferData method. But, if I passed as a byte buffer, it is obviously the library will not be able to know which is floating value and should be swapped. This was a reason this problem was happen.

**Therefore, this was not a problem of JOGL actually.**