Main Menu

glTexImage2D error

Started by Unknown, December 03, 2005, 10:23:45

Previous topic - Next topic

Unknown

Whenever I try to load a texture with DevIL, I get the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Number of remaining buffer elements is 1408, must be at least 1536
   at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:177)
   at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:182)
   at org.lwjgl.opengl.GL11.glTexImage2D(GL11.java:2153)

Here's source of my texture loader class. I know I'm doing something wrong, but what? And yes, DevIL is initialized (although not in this class), so that shouldn't cause the problem.

I create a ByteBuffer called TextureBuffer and after loading the image with DevIL I set it to ilGetData() Then later when I call glTexImage2D() with the TextureBuffer, I get the error shown before. Is there something I should do with the ByteBuffer before using it with glTexImage2D()? Or is my loader just seriously messed up?

package Test;
import org.lwjgl.opengl.GL11;

import java.net.URL;
import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Hashtable;

import org.lwjgl.BufferUtils;
import org.lwjgl.devil.IL;
import org.lwjgl.devil.ILU;
import org.lwjgl.devil.ILUT;
import org.lwjgl.devil.ILinfo;


////////////////////////////
//  Texture Loader Class  //
////////////////////////////

public class TextureLoader {

	// Table Containing Loaded Textures
    private HashMap TextureTable = new HashMap();

	// IntBuffer For Texture ID's
    private IntBuffer TextureIDBuffer = BufferUtils.createIntBuffer(1); 

	// Loads A Texture From Disk
	private Texture GenerateTexture(String path) {

		// Load Given Image With DevIL
		IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
		IL.ilGenImages(image);
		IL.ilBindImage(image.get(0));
		IL.ilLoadImage(path);
		ByteBuffer TextureBuffer = IL.ilGetData();

		// Create a New Texture Object
        int TextureID = CreateTextureID(); 
        Texture texture = new Texture(GL11.GL_TEXTURE_2D, TextureID); 
        texture.SetWidth(IL.ilGetInteger(IL.IL_IMAGE_WIDTH));
        texture.SetHeight(IL.ilGetInteger(IL.IL_IMAGE_HEIGHT));
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureID);

		// Apply Linear Filtering
		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);

		// Generate The Texture
		GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB,
						  Get2Fold(texture.GetWidth()),
						  Get2Fold(texture.GetHeight()),
						  0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, TextureBuffer);

		// Return Generated OGL Texture
		return texture;
	}


	// Generates A New Texture ID
    private int CreateTextureID() {
      GL11.glGenTextures(TextureIDBuffer); 
      return TextureIDBuffer.get(0);
    }

	// Returns Nearest Power Of 2
    private int Get2Fold(int fold) {
        int ret = 2;
        while (ret < fold) {
            ret *= 2;
        }
        return ret;
    } 

	// Loads And Returns A Texture
    public Texture LoadTexture(String resource) {

		// Texture Exists?
        Texture texture = (Texture) TextureTable.get(resource);
        
		// Return Existing, Or...
        if (texture != null) {
            return texture;
        }
        
        // Load A New Texture From Disk
        texture = GenerateTexture(resource);
        TextureTable.put(resource, texture);
        
        return texture;
    }
}

Fool Running

This is a guess :D
TextureBuffer contains the data of the texture you are loading which isn't a power of 2.
You are creating a Texture object with the correct (non-power-of-2) size for the data you get, but then are trying to create a OpenGL texture object with TextureBuffer specifiying the size as the closest power of 2 so the check is saying that there is not enough data for the OpenGL texture you are trying to create.

Try adding a IL.ResizeImage() (or whatever its called  :lol: Might be in ILU) with the power of 2 size before you get the data from DevIL.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D