[英]opengl : glGetTexImage for 2d textures and 1d texture array confusion
對於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);
所以這非常適用於 2D 紋理。
現在跳轉到紋理 1D Arrays 我讀取特定 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);
}
Colors 全部不正確,甚至將紋理格式更改為 GL_RGBA 也沒有解決問題。但是當我將常量從 4 更改為 3 [在紋理一維數組的 readMethod() 中每個像素讀取 3 個字節] 一切再次正常工作。 所以我在這里真的很困惑,因為我所有的測試紋理都是 RGB 格式,而我觀察到的是
-> 對於 2D 紋理,在 glGetTexImage() 中每個像素讀取 4 個字節,但后來只為紋理格式指定了 RGB
->對於 1D 紋理 Arrays 在 glGetTexImage() 中每個像素讀取 3 個字節,但后來只為紋理格式指定 RGB
但是規范說它默認為所有紋理類型讀取每個像素 4 個字節,除非您使用 pixelStorei() 更改該行為
而且我只使用這種方法來創建 2D 紋理,而不是其他任何地方。
有人可以解釋為什么會有差異嗎?
根據文檔,它每像素寫入 4 個字節 [...]
不,不是的。 glGetTexImage(...GL_RGB, GL_UNSIGNED_BYTE...)
每個像素讀取 3 個字節。 一行的長度對齊到 4 個字節(如果GL_PACK_ALIGNMENT
為 4)。
glGetTexImage的glGetTexImage
格式和_type沒有指定源紋理的格式,而是目標緩沖區的格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.