我在这里有这个代码:
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[]转换为浮点数,但这是我知道的唯一方法。在c++中有glLightfv,但在lwjgl中只有glLightf。我该如何解决这个问题?
发布于 2012-09-07 04:27:05
我在别的地方找到了这个问题的答案。在这个方法中,我放入了(FloatBuffer)bytes.asFloatBuffer().put(lightAmbient).flip()。这个更短,也更有效。
https://stackoverflow.com/questions/12149193
复制相似问题