gpt4 book ai didi

java - FloatBuffer 溢出异常 java

转载 作者:行者123 更新时间:2023-11-29 09:11:28 27 4
gpt4 key购买 nike

我这里有这段代码:

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。我该如何解决这个问题?

最佳答案

我在别处找到了这个问题的答案。在我放入 (FloatBuffer)bytes.asFloatBuffer().put(lightAmbient).flip() 的方法中。这更短并且有效。

关于java - FloatBuffer 溢出异常 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12149193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com