Login  Register

Loading texture doesn't work with the correct texture width/height

Posted by WarrenFaith on Jan 27, 2011; 12:34pm
URL: https://forum.jogamp.org/Loading-texture-doesn-t-work-with-the-correct-texture-width-height-tp2362327.html

I am trying to load a texture for a cube and I have trouble with the dimensions I use. The texture has the power of two (256x256). When it should use 256 as width and height it throws an exception:

java.lang.IndexOutOfBoundsException: Required 262144 remaining bytes in buffer, only had 68998
        at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:828)

The code:

    private void initTexture(GL2ES2 gl) {
	try {
		BufferedImage bufferedImage = ImageIO.read(new URI("http://192.168.0.39/images/box.gif").toURL());
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ImageIO.write(bufferedImage, "gif", byteArrayOutputStream);
		byte[] imageData = byteArrayOutputStream.toByteArray();
		imageBuffer = ByteBuffer.wrap(imageData);
	} catch (Exception e) {
		e.printStackTrace();
	}
	imageBuffer.rewind();
	gl.glGenTextures(1, textureIds, 0);
	gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, textureIds[0]);
	gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer);
        gl.glTexParameteri(GL2ES2.GL_TEXTURE_2D, GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
        gl.glTexParameteri(GL2ES2.GL_TEXTURE_2D, GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_NEAREST);
        gl.glGenerateMipmap(GL2ES2.GL_TEXTURE_2D);
	gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, 0);
    }

When I change the parameter width/height to 128 the exception disappears but the cubes show wrong colors which looks like random color for each vertex.

Is the way I read the file wrong? (I know I don't use JOGL Texture...)