开发者

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

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 cub开发者_StackOverflowes show wrong colors:

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

As bestsss mentioned, the reason might be some raw format. The problem: I can't fix this. I tried multiple images and formats. Created them with gimp (working on ubuntu) but the exception is always the same. So I guess the reason for that is that I read the image in a wrong way. Some ideas?

Update

My solution (which uses JOGL classes TextureIO and Texture):

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
}


Zero clue about the API however. I can bet the expected format is some raw one NOT gif since 262144 =2^18 (or 256*256*4). RGB+Alpha are 4bytes.

edit: again, gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer);

just guessting but look at the constants: GL2ES2.GL_RGBA, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE - all support RGBA format for the bytes in the byte buffer,see what other contestants are available, the way I believe using NIO would have point only with direct buffers containing the raster in the format specified by the constants. (i.e. no other formats for image storage/transmission like jpeg/bif/png will help)

So read the documentation again, look for tutorial, examples and proceed (the way you load the image is not very good either)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜