对于texture2D,我提取mipmap的纹理,如下所示
pixels=ByteBuffer.allocateDirect(4*width*height);
GL11.glGetTexImage(
GL11.GL_TEXTURE_2D,mipMap
,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE
,pixels
);
根据文档,如果纹理不是4个组件,则它每像素写入4个字节,将所需组件设置为零。
稍后,我可以使用以下像素创建一个2D纹理
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT,4); //4 bytes since RGBA bytes were read from texture
GL11.glTexImage2D(GL11.GL_TEXTURE_2D,0
,GL11.GL_RGB,width,height,0
,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,pixels);
这对于二维纹理来说是非常有效的。
现在跳到纹理一维数组,我读取了特定mipmap所有层的图像,如下所示
pixels=ByteBuffer.allocateDirect(4*image.width*layers); //again creating RGBA byte buffer because thats the default behaviour
GL11.glGetTexImage( //this will return the texture images of all layers of mipmap level 0
GL30.GL_TEXTURE_1D_ARRAY,0
,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE
,pixels
);
ByteBuffer levelN=ByteBuffer.allocateDirect(4*image.width);
int offset=4*image.width*level; //level is layer 0,layer 1 so on this part reads only the texels of an specific layer
for(int i=offset;i<offset+(image.width*4);i++){levelN.put(pixels.get(i));}
pixels=levelN;
但是稍后,当我创建texture1D数组时,如下所示
ByteBuffer[] layers=//read using above method
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT,4); //4 bytes since RGBA bytes were read from texture
GL11.glTexImage2D(GL30.GL_TEXTURE_1D_ARRAY,0 //allocate enough storage for all layers
,GL11.GL_RGB,width,layers.length,0
,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,(ByteBuffer)null);
for(int layer=0;i<layers.length;layer++)
{
ByteBuffer image=layers[i];
GL11.glTexSubImage2D(GL30.GL_TEXTURE_1D_ARRAY,0 //Update individual layers using texSubImage
,0,layer,width,1
,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,image);
}
颜色完全不正确,甚至将纹理格式更改为GL_RGBA也没有解决problem.but问题,当我将常数从4改为3时,在纹理1d数组的readMethod()中每像素只读取3个字节,一切都再次正确工作。所以我真的很困惑,因为我所有的测试纹理都是RGB格式,我观察到的是
->For 2D纹理在glGetTexImage()中每像素读取4个字节,但之后只为纹理格式指定RGB
->For 1D纹理数组在glGetTexImage()中读取每个像素3个字节,但之后只为纹理格式指定RGB。
但是规格说,它默认每个像素读取所有纹理类型的4个字节,除非使用pixelStorei()改变这种行为。
我只使用这种方法来创建2D纹理,而不是其他任何地方。
谁能解释一下差异的原因吗?
发布于 2020-12-07 09:46:37
根据文档,它每像素写4个字节.
不,不需要。glGetTexImage(...GL_RGB, GL_UNSIGNED_BYTE...)
每像素读取3个字节。一行的长度对齐为4个字节(如果GL_PACK_ALIGNMENT
为4)。
参数格式和_type of glGetTexImage
没有指定源纹理的格式,而是指定目标缓冲区的格式。
https://stackoverflow.com/questions/65186482
复制相似问题