2

I have this code here:

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

When I try to run it, I get this error:

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)

I have looked all over the internet I can't find how to fix the problem (I also don't know what it is). My goal is to convert a float[] to a float, but this is the only way I know how. In c++ there is glLightfv, but in lwjgl there is only glLightf. How do I fix this?

Coupon22
  • 395
  • 8
  • 24
  • I think you need to use public final ByteBuffer put(byte[] src) instead of just put(ByteBuffer src) read this http://www.cs.duke.edu/csed/java/jdk1.5/docs/api/java/nio/ByteBuffer.html#put(java.nio.ByteBuffer) – kosa Aug 27 '12 at 20:39
  • 2
    Quite aside from your error, do you really want to be overwriting the same 16 bytes? – parsifal Aug 27 '12 at 21:05
  • I found the answer to this question. I put the byte buffer in the method like this: (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip() – Coupon22 Sep 03 '12 at 14:45

1 Answers1

0

I found the answer of this question somewhere else. In the method I put (FloatBuffer)bytes.asFloatBuffer().put(lightAmbient).flip(). This is shorter and works.

bemeyer
  • 6,154
  • 4
  • 36
  • 86
Coupon22
  • 395
  • 8
  • 24