Search code examples
javaniolwjglfloatbuffer

FloatBuffer OverflowException java


I have this code here:

static ByteBuffer bytes = ByteBuffer.allocateDirect(16).order(ByteOrder.nativeOrder());
static FloatBuffer matAmbientB = bytes.asFloatBuffer();
static FloatBuffer matAmbientC = bytes.asFloatBuffer();
static FloatBuffer matAmbientD = bytes.asFloatBuffer();
static FloatBuffer matAmbientE = bytes.asFloatBuffer();
static FloatBuffer matAmbientF = bytes.asFloatBuffer();
static FloatBuffer matAmbientG = bytes.asFloatBuffer();
static FloatBuffer matAmbientH = bytes.asFloatBuffer();

    private void initGL()
    { 
        matAmbientB.put(redDiffuseMaterial);
        matAmbientB.rewind();
        matAmbientC.put(whiteSpecularMaterial);
        matAmbientC.rewind();
        matAmbientD.put(greenEmissiveMaterial);
        matAmbientD.rewind();
        matAmbientE.put(whiteSpecularLight);
        matAmbientE.rewind();
        matAmbientF.put(blankMaterial);
        matAmbientF.rewind();
        matAmbientG.put(whiteDiffuseLight);
        matAmbientG.rewind();
        matAmbientH.put(blackAmbientLight);
        matAmbientH.rewind();
    }

    void light ()
    {
        glLightf(GL_LIGHT0, GL_SPECULAR, matAmbientE.get());
        glLightf(GL_LIGHT0, GL_AMBIENT, matAmbientH.get());
        glLightf(GL_LIGHT0, GL_DIFFUSE, matAmbientG.get());
    }

When I try to run it, I get this error:

Exception in thread "main" java.nio.BufferOverflowException
    at java.nio.DirectFloatBufferU.put(Unknown Source)
    at java.nio.FloatBuffer.put(Unknown Source)
    at src.Main.initGL(Main.java:76)
    at src.Main.run(Main.java:45)
    at src.Main.main(Main.java:232)

I have looked all over the internet I can't find how to fix the problem (I also don't know what it is). My goal is to convert a float[] to a float, but this is the only way I know how. In c++ there is glLightfv, but in lwjgl there is only glLightf. How do I fix this?


Solution

  • I found the answer of this question somewhere else. In the method I put (FloatBuffer)bytes.asFloatBuffer().put(lightAmbient).flip(). This is shorter and works.