[Solved]Illegal Argument Exception

I’m working on a 3d game and I’ve been having some trouble with my Texture Manager.
Here is the code I’m using (Sorry its a bit sloppy):
[spoiler]


import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.IOException;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;




public class TextureMananger {
	public static final boolean DBG = true; //Debug
	public static TextureMananger img;
	public static ResourceLoader resourceLoader = new ResourceLoader();
	/* Number Of Textures Loaded */
	public static int totalImagesLoaded = 0;
	private static HashMap<String, Number> loadedImages = new HashMap<String, Number>();
	
	public static int loadTexture(String texture) throws IOException {
		if (loadedImages.get(texture) != null) {
			if (DBG) System.out.println("[Textures] Texture '" + texture.toString() + "' was already loaded! ID is " + loadedImages.get(texture.toString()).intValue() + ".");

			return loadedImages.get(texture.toString()).intValue();

		} else {
			BufferedImage image = ImageIO.read(resourceLoader.loadResource(texture));
			int width = image.getWidth();
			int height = image.getHeight();
			DataBufferByte rawData =  (DataBufferByte) image.getData().getDataBuffer();
			
			ByteBuffer data = BufferUtils.createByteBuffer(rawData.getSize());
	        data.put(rawData.getData());
	        data.rewind();
	        //data.clear();
	        IntBuffer scratch = BufferUtils.createIntBuffer(1);
	        
	        GL11.glGenTextures(scratch);
	        GL11.glBindTexture(GL11.GL_TEXTURE_2D, scratch.get(0));
	        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
	        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
	        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
	        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
	        totalImagesLoaded++;
	        loadedImages.put(texture.toString(), scratch.get(0));
	        System.out.println(TextureMananger.class.getClass().getResource(texture));
			if (DBG) System.out.println("[Textures] Texture '" + texture.toString() + "' was Loaded! ID is " + loadedImages.get(texture.toString()).intValue() + ".");
	        return scratch.get(0);  
	        
		}
		
	}

		
	
		
	
	
}

[/spoiler]

And here is the error im getting:

java.lang.IllegalArgumentException: Number of remaining buffer elements is 768, must be at least 1024. Because at most 1024 elements can be returned, a buffer with at least 1024 elements is required, regardless of actual returned element count

I’m not to good with buffers and rendering stuff, so any help would be appreciated.

  1. Which line of code is the exception thrown from?
  2. Try [icode]data.flip()[/icode] instead of [icode]data.rewind()[/icode]

It points me to this line:
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);

and,

data.flip(), data.rewind, and data.clear() all make it say 768

Here’s my texture loading code:


int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

for(int y = 0; y < image.getHeight(); y++)
{
	for(int x = 0; x < image.getWidth(); x++)
	{
		int pixel = pixels[(image.getHeight() - 1 - y) * image.getWidth() + x];
		buffer.put((byte) ((pixel >> 16) & 0xFF));
		buffer.put((byte) ((pixel >> 8) & 0xFF));
		buffer.put((byte) ((pixel) & 0xFF));
		buffer.put((byte) ((pixel >> 24) & 0xFF));
	}
}

buffer.flip();

// Do OpenGL texture commands with buffer from here.

Not sure if it will help you find the problem, but it works.

I think I found something:

Change

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

to

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

(Make sure to include [icode]GL11.[/icode]. I use static imports)

You don’t need GL_RGBA8 – I would stick to plain old GL_RGBA.

Your code looks botched in a number of places. I would suggest sticking with a more object-oriented approach.

You can read about LWJGL texture loading here:

Also, if you use mipmapping you will need to generate the mipmaps, otherwise the texture is invalid.

If you want to stick to AWT for decoding, here is a clever trick from nsigma:

Changing the both of the GL_RGBA to GL_RGBA8 worked. Thanks