FloatBuffer OverflowException java

时间:2012-08-27 20:33:47

标签: java nio lwjgl floatbuffer

我这里有这个代码:

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());
    }

当我尝试运行它时,我收到此错误:

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)

我已经浏览了整个互联网,我无法找到解决问题的方法(我也不知道它是什么)。我的目标是将float []转换为float,但这是我知道的唯一方法。在c ++中有glLightfv,但在lwjgl中只有glLightf。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我在其他地方找到了这个问题的答案。在方法中我放了(FloatBuffer)bytes.asFloatBuffer().put(lightAmbient).flip()。这个更短,更有效。