加载纹理不适合正确的纹理宽度/高度
-
25-10-2019 - |
题
我正在尝试为立方体加载纹理,并且在使用的尺寸上遇到麻烦。纹理具有两个(256x256)的力量。当它应使用256作为宽度和高度时,它会引发例外:
java.lang.IndexOutOfBoundsException: Required 262144 remaining bytes in buffer, only had 68998
at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:828)
编码:
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);
}
当我将参数宽度/高度更改为128时,异常消失了,但立方体显示错误的颜色:
正如Bestss提到的那样,原因可能是某种原始格式。问题:我无法解决。我尝试了多个图像和格式。用gimp创建它们(在Ubuntu上工作),但例外总是相同的。因此,我想这样做的原因是我以错误的方式阅读了图像。一些想法?
更新
我的解决方案(使用JOGL类纹理和纹理):
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
}
解决方案
但是,关于API的零线索。我敢打赌,预期的格式是一些原始的格式 不是gif 自262144 = 2^18(或256*256*4)。 RGB+alpha为4个。
编辑:再次gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer);
只是猜测但要看常数:gl2es2.gl_rgba,gl2es2.gl_rgba,gl2es2.gl_unsigned_byte-所有支持Byte Buffer中的字节格式的所有支持RGBA格式 直接的 缓冲区包含常数指定格式的栅格。 (即没有其他用于图像存储/变速器(例如JPEG/BIF/PNG)的格式将有所帮助)
因此,再次阅读文档,查找教程,示例并继续(加载方式也不是很好)
不隶属于 StackOverflow