简体   繁体   English

加载纹理无法使用正确的纹理宽度/高度

[英]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). 纹理具有2的幂(256x256)。 When it should use 256 as width and height it throws an exception: 当它应使用256作为宽度和高度时,将引发异常:

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: 当我将参数width / height更改为128时,异常消失,但多维数据集显示错误的颜色:

在此处输入图片说明

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. 使用gimp创建了它们(在ubuntu上工作),但例外始终是相同的。 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): 我的解决方案(使用JOGL类TextureIO和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. 但是,有关该API的零线索。 I can bet the expected format is some raw one NOT gif since 262144 =2^18 (or 256*256*4). 我敢打赌,期望的格式是一些原始的NOT gif,因为262144 = 2 ^ 18(或256 * 256 * 4)。 RGB+Alpha are 4bytes. RGB + Alpha是4个字节。

edit: again, gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer); 编辑:再次, 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. 只是猜测而已,请看一下常量:GL2ES2.GL_RGBA,GL2ES2.GL_RGBA,GL2ES2.GL_UNSIGNED_BYTE-都支持字节缓冲区中字节的RGBA格式,看看还有哪些其他竞争者可用,我相信使用NIO的方式仅适用于以常量指定的格式包含栅格的直接缓冲区。 (ie no other formats for image storage/transmission like jpeg/bif/png will help) (即,没有其他像jpeg / bif / png这样的图像存储/传输格式会有所帮助)

So read the documentation again, look for tutorial, examples and proceed (the way you load the image is not very good either) 因此,请再次阅读文档,查找教程,示例并继续(加载图像的方式也不是很好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM