java.lang.IllegalArgumentException: Number of remaining buffer elements is 3, must be at least 4
I am trying to set spot light here is the code:
public void attenuatedSpotlight(){
float light1_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
float light1_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float light1_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float light1_position[] = {-2.0f, 2.0f, 1.0f, 1.0f };
float spot_direction[] = { -1.0f, -1.0f, 10f };
GL11.glLight( GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer) buffer
.asFloatBuffer().put( light1_ambient).flip() );
GL11.glLight( GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer) buffer
.asFloatBuffer().put( light1_diffuse ).flip() );
GL11.glLight( GL11.GL_LIGHT1, GL11.GL_SPECULAR, (FloatBuffer) buffer
.asFloatBuffer().put( light1_specular ).flip() );
GL11.glLight( GL11.GL_LIGHT1, GL11.GL_POSITION, (FloatBuffer) buffer
.asFloatBuffer().put( light1_position).flip() );
/* Attenuation */
GL11.glLightf( GL11.GL_LIGHT1, GL11.GL_CONSTANT_ATTENUATION, 1.5f );
GL11.glLightf( GL11.GL_LIGHT1, GL11.GL_LINEAR_ATTENUATION, 0.5f );
GL11.glLightf( GL11.GL_LIGHT1, GL11.GL_QUADRATIC_ATTENUATION, 0.2f );
/* Control angle of the light, spread, simulate flashlight*/
GL11.glLightf( GL11.GL_LIGHT1, GL11.GL_SPOT_CUTOFF, 45f );
GL11.glLight( GL11.GL_LIGHT1, GL11.GL_SPOT_DIRECTION, (FloatBuffer) buffer
.asFloatBuffer().put( spot_direction ). flip() );
GL11.glLightf( GL11.GL_LIGHT1, GL11.GL_SPOT_EXPONENT, 2f );
}
But all I get is the there is a problem with the Buffer
Buffer is set to 16 if the number is less than 12 then I get overflow.
ByteBuffer buffer = ByteBuffer.allocateDirect(16);
buffer.order(ByteOrder.nativeOrder());
Any ideas?
first of all, consider using org.lwjgl.BufferUtils.createFloatBuffer(4);
secondly, the error you're stumbling into, is a lwjgl issue. It does some internal testing, to assert correct sizes of passed buffers.
It usually expect a buffer of 4 elements, even though the function might only require 3 (in the case of glLight, GL_SPOT_DIRECTION).
so to "fix", just add the 3 elements to a 4 elements big float buffer.
...what line does the exception occur on...
Cas :)
Thanks I try to do it.
Regards
Hi, it works but now, light does not work at all. I mean I have two lights, one directional and the other spot. When I turn off the spot, directional works with the original buffer I used. But when I switch back to the one you guys suggested light does not work at all. Althought spot and directional can be turned on without errors.
Puzzled, a bit.
this is the line I was getting err at before
GL11.glLight( GL11.GL_LIGHT1, GL11.GL_SPOT_DIRECTION, buffer
);