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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

WarrenFaith
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...)
Reply | Threaded
Open this post in threaded view
|

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

gouessej
Administrator
Hi!

Use AWTTextureIO and Texture classes. The Texture class has a setTexParameteri method. Let us know whether it fixes your problem.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

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

WarrenFaith
do you have a tutorial I can follow? I tried it for some minutes but couldn't figure it out...

Thanks
Reply | Threaded
Open this post in threaded view
|

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

WarrenFaith
When I try it with this:
BufferedImage bufferedImage;
try {
	bufferedImage = ImageIO.read(new URI("http://192.168.0.39/images/test.png").toURL());
	Texture texture = AWTTextureIO.newTexture(gl.getGLProfile(), bufferedImage, true);
	texture.setTexParameteri(GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
	texture.setTexParameteri(GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_NEAREST);
	texture.bind();
} catch (Exception e) {
	e.printStackTrace();
}

I get this:

javax.media.opengl.GLException: INT_BGR n.a.
	at com.jogamp.opengl.util.texture.awt.AWTTextureData.createFromImage(AWTTextureData.java:317)
	at com.jogamp.opengl.util.texture.awt.AWTTextureData.<init>(AWTTextureData.java:105)
	at com.jogamp.opengl.util.texture.awt.AWTTextureIO.newTextureDataImpl(AWTTextureIO.java:127)
	at com.jogamp.opengl.util.texture.awt.AWTTextureIO.newTextureData(AWTTextureIO.java:64)
	at com.jogamp.opengl.util.texture.awt.AWTTextureIO.newTexture(AWTTextureIO.java:116)

Ok, so I understand that somehow the colormodel is different, but I have no idea how I can change the color model of my texture image?!
Reply | Threaded
Open this post in threaded view
|

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

WarrenFaith
In reply to this post by WarrenFaith
When I try it with this:
BufferedImage bufferedImage;
try {
	bufferedImage = ImageIO.read(new URI("http://192.168.0.39/images/test.png").toURL());
	Texture texture = AWTTextureIO.newTexture(gl.getGLProfile(), bufferedImage, true);
	texture.setTexParameteri(GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
	texture.setTexParameteri(GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_NEAREST);
	texture.bind();
} catch (Exception e) {
	e.printStackTrace();
}

I get this:

javax.media.opengl.GLException: INT_BGR n.a.
	at com.jogamp.opengl.util.texture.awt.AWTTextureData.createFromImage(AWTTextureData.java:317)
	at com.jogamp.opengl.util.texture.awt.AWTTextureData.<init>(AWTTextureData.java:105)
	at com.jogamp.opengl.util.texture.awt.AWTTextureIO.newTextureDataImpl(AWTTextureIO.java:127)
	at com.jogamp.opengl.util.texture.awt.AWTTextureIO.newTextureData(AWTTextureIO.java:64)
	at com.jogamp.opengl.util.texture.awt.AWTTextureIO.newTexture(AWTTextureIO.java:116)

Ok, so I understand that somehow the colormodel is different, but I have no idea how I can change the color model of my texture image?!
Reply | Threaded
Open this post in threaded view
|

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

gouessej
Administrator
The exception is not very explicit and this color model should be supported by your graphics card. Try to add an alpha channel to your image.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

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

Demoscene Passivist
Administrator
In reply to this post by WarrenFaith
For a couple of utility methods dealing with texture io u could take a look at my TextureUtils class.

I guess loadImageAsTexture_UNMODIFIED() is what u might want.

If u like to do bytebuffer stuff u also might take a look at convertARGBBufferedImageToJOGLRGBADirectByteBuffer().

Happy coding
Reply | Threaded
Open this post in threaded view
|

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

WarrenFaith
That gave me the right direction...

my solution:

Texture texture;

private void initTexture(GL2ES2 gl) {
    try {
        texture = TextureIO.newTexture(new URI("http://192.168.0.39/images/box.gif").toURL(),true,null);
        texture.setTexParameterf(GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_LINEAR);
        texture.setTexParameterf(GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void display(GL2ES2 gl) {
    // code snipped
    if (texture != null) {
        texture.enable();
        texture.bind();
    }
    // code snipped
}
Reply | Threaded
Open this post in threaded view
|

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

gouessej
Administrator
Sorry I thought you had enabled textures somewhere else.
Julien Gouesse | Personal blog | Website