Load matrix using float buffer

Started by HermanssoN, March 29, 2010, 19:11:33

Previous topic - Next topic

HermanssoN

Hello, Im trying to store my matrix as a floatbuffer and send it to open gl:

  public FloatBuffer toBuffer()
    {
        FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
       
        buffer.put(m_[0]);
        buffer.put(m_[1]);
        buffer.put(m_[2]);
        buffer.put(m_[3]);
        buffer.put(m_[4]);
        buffer.put(m_[5]);
        buffer.put(m_[6]);
        buffer.put(m_[7]);
        buffer.put(m_[8]);
        buffer.put(m_[9]);
        buffer.put(m_[10]);
        buffer.put(m_[11]);
        buffer.put(m_[12]);
        buffer.put(m_[13]);
        buffer.put(m_[14]);
        buffer.put(m_[15]);

        
        return buffer;
    }

and load it:

GL11.glLoadMatrix(matrix.toBuffer());


I get the following error:
Quote
java.lang.IllegalArgumentException: Number of remaining buffer elements is 0, must be at least 16
at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:136)
at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:151)
at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:176)

I'm new to LWJGL and I guess I'm creating the buffer the wrong way.
Any help would be appreciated.

Elviz

Call buffer.flip(); after putting the last float value in the buffer. This will reset the position, ensuring that the next read access will start at index 0.

Also, for performance reasons it might be advantageous to reuse a buffer instead of creating a new one each time. If you reuse a buffer, be sure to invoke buffer.clear(); before transferring your matrix elements to it.

Finally, since your matrix data seem to be stored as a float array, it may be simpler to just use the FloatBuffer.put(float[]) method.