码:
FloatBuffer buf = BufferTools.reserveData(4);
float[] fl = new float[]{0.1f, 0.1f, 0.1f, 1.0f};
buf.put(fl);
其中
buf.put(fl)
是该错误正在讨论的第2039行:Exception in thread "main" java.lang.IllegalArgumentException: Number of remaining buffer elements is 0, must be at least 4. Because at most 4 elements can be returned, a buffer with at least 4 elements is required, regardless of actual returned element count
at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:162)
at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:189)
at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:258)
at org.lwjgl.opengl.GL11.glLight(GL11.java:2039)
at Joehot200.TerrainDemo.setUpLighting(TerrainDemo.java:1543)
at Joehot200.TerrainDemo.enterGameLoop(TerrainDemo.java:1984)
at Joehot200.TerrainDemo.startGame(TerrainDemo.java:2109)
at Joehot200.Main.main(Main.java:56)
我究竟做错了什么?我只想拥有一个包含4个元素的FloatBuffer,以便可以设置OpenGL环境照明。
最佳答案
LWJGL抛出此错误,因为缓冲区对于请求的操作而言太小。见here
我的猜测是BufferTools.reserveData(4)
保留4个字节,而不是4 * sizeof(float)== 16个字节。
关于java - FloatBuffer抛出异常?我怎样才能解决这个问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24489637/