简体   繁体   中英

FloatBuffer throwing exception? How can I fix this?

Code:

FloatBuffer buf = BufferTools.reserveData(4);
float[] fl = new float[]{0.1f, 0.1f, 0.1f, 1.0f};
buf.put(fl);

where buf.put(fl) is line 2039 that the error is talking about:

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)

What am I doing wrong? I would just like to have a FloatBuffer with 4 elements in it so that I can set the OpenGL ambient lighting.

LWJGL throws this error it the buffer is too small for the requested operation. See here

My guess is that BufferTools.reserveData(4) reserves 4 byte and not 4*sizeof(float) == 16 byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM