More on this lighting stuff,
I setup lighting and have a similar experience, "darkness." The background was set to red and when lighting is enabled the object is totally black against my red background. When disabled, prety object. Other posts had similar issues and flipping the buffers solved their dilemma. Not the case for me.
Using a flip() causes an error which basically says my buffer was already drained. So, I don't flip() and it likes that.
This is my basic buffer setup:
float[] lightPosition = {0.0f, 0.0f, 5.0f, 1.0f};
FloatBuffer light_position = BufferUtils.createFloatBuffer(4);
light_position.wrap(lightPosition);
Then I plug them into the needed calls:
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, light_ambient);
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, light_diffuse);
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, light_position);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT1);
So, any ideas?
Thanks,
Ben
I once had an issues where I needed to rewind a buffer instead of flipping it - I didn't investigate it further though. Try rewinding it instead of flipping it...
This is what worked for me:
private float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // Ambient Light Values ( NEW )
private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values ( NEW )
private float lightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position ( NEW )
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip()); // Position The Light
GL11.glEnable(GL11.GL_LIGHT1); // Enable Light One
I'm not exactly sure what wrap does, but I use the put() method instead and it always works for me.
just change
light_position.wrap(lightPosition);
to
light_position.put(lightPosition).rewind();
and change the other buffer calls as well.... (I realize that its basically the same as CaptainJester's post, but it seems like a smaller change to me :lol: )
Thanks for the help,
Turns out the the "light_position.put(lightPosition).rewind();" solved the issue. I did try to use the ByteBuffer method suggested by CaptainJester, but it didn't work for some reason. Appears that I am just plainly missing some of the nuaunces of this.
Again, thanks for everyone's help. Your responses are most appreciated.
Ben
You'd misunderstood the wrap method, I'm afraid! Wrap is a static method on the Buffer subclasses which takes an array and returns a new buffer (of the appropiate type) that wraps that array. The following code:
light_position.wrap(lightPosition);
Will do nothing, as you're throwing away the result of the method call. Note that any decent IDE should complain that you accessed a static member in a non-static way. It would encourage you to write the following:
FloatBuffer.wrap(lightPosition);
Which is obviously not using the FloatBuffer you'd created. So your lighting was dark because all the buffers you passed the GL contained zeros - the default contents of unaltered float storage space. Note also that if you'd tried the following:
light_position = FloatBuffer.wrap(lightPosition);
It also would have failed! Buffers that wrap arrays use the array as a backing store, thus they are by definition non-direct buffers, which don't work with LWJGL. Although at least then you would have got an error message! :wink: