java - 打开 : glGetTexImage for 2d textures and 1d texture array confusion

标签 java opengl textures

对于 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 纹理。

现在跳转到纹理一维数组,我读取特定 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 也没有解决问题。但是当我将常量从 4 更改为 3 时 [每个像素仅读取 3 个字节纹理 1d 数组的 readMethod()] 一切再次正常工作。所以我在这里真的很困惑,因为我所有的测试纹理都是 RGB 格式,而我观察到的是

->对于 2D 纹理,在 glGetTexImage() 中每个像素读取 4 个字节,但随后仅指定 RGB 作为纹理格式有效

->对于一维纹理阵列,在 glGetTexImage() 中每个像素读取 3 个字节,但后来仅指定 RGB 纹理格式有效

但是specs假设它默认为所有纹理类型读取每个像素 4 个字节,除非您使用 pixelStorei() 更改该行为

而且我只使用该方法创建 2D 纹理,而不是其他任何地方。

谁能解释一下为什么会有差异?

最佳答案

As per the documentation it writes 4 bytes per pixels [...]

不,它没有。 glGetTexImage(...GL_RGB, GL_UNSIGNED_BYTE...)每个像素读取 3 个字节。一行的长度对齐到 4 个字节(如果 GL_PACK_ALIGNMENT 是 4)。
glGetTexImage 的参数 format 和 _type 不指定源纹理的格式,而是指定目标缓冲区的格式。

关于java - 打开 : glGetTexImage for 2d textures and 1d texture array confusion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65186482/

相关文章:

java - Spring Boot JPA ManyToMany 导致 OutOfMemoryError

opengl - GBUFFER 贴花投影和缩放

macos - SDL2 窗口在调整大小时变黑

CSS mask-image 不工作(试过 firefox 和 safari)

caching - CUDA:FERMI 上的纹理缓存是否与 L1 缓存分开?

java - Servlet 3.1 - 多部分异步处理

java - 添加两个在java中声明为char类型的数字怎么可能?

c++ - 关于direct3d关于D3DXCreateTextureFromFileEx的方法

java - Maven编译插件错误v3.0.3

c++ - OpenGL 使用 glNewList 和 glDrawElements