OpenGL glTexSubImage2D无法正确写入像素[复制]

inn6fuwd  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(161)

此问题在此处已有答案

How to load a bmp on GLUT to use it as a texture?(5个答案)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1) Disadvantages?(3个答案)
Failing to map a simple unsigned byte rgb texture to a quad:(1个答案)
四个月前关门了。
我有一个无符号字节数组,代表字母q的像素。我想使用glTexImage2D将这些像素写入OpenGL纹理。在下面的代码中,我还添加了一些检查,以确保像素数据有效。我检查了宽度乘以高度是否与数据长度匹配,甚至将像素打印到终端。(代码是用rust编写的,但是GL调用与它们在任何其他绑定中的调用相同)。

// create the array of pixels
let data: &[u8] = &[
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 37, 2, 0, 0, 0, 0, 0, 0, 0, 119,
    248, 252, 241, 92, 223, 130, 0, 0, 0, 84, 253, 108, 8, 36, 202, 248, 130, 0, 0, 0,
    198, 182, 0, 0, 0, 52, 255, 130, 0, 0, 0, 241, 120, 0, 0, 0, 0, 245, 130, 0, 0, 3,
    252, 108, 0, 0, 0, 0, 233, 130, 0, 0, 0, 223, 143, 0, 0, 0, 14, 253, 130, 0, 0, 0,
    144, 234, 20, 0, 0, 126, 255, 130, 0, 0, 0, 23, 219, 223, 142, 173, 194, 231, 130,
    0, 0, 0, 0, 18, 120, 156, 108, 13, 223, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 223, 130,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 223, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 87, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
];

// check that WxH equals the number of pixels
let data_w = 11;
let data_h = 15;
assert_eq!(data_w * data_h, data.len());

// write the pixels to the texture
glBindTexture(GL_TEXTURE_2D, texture);

// initialize the texture
glTexImage2D(
    GL_TEXTURE_2D,
    0,
    GL_RED as _,
    256,
    256,
    0,
    GL_RED,
    GL_UNSIGNED_BYTE,
    ptr::null(),
);

// write the pixels
glTexSubImage2D(
    GL_TEXTURE_2D,
    0,
    0,
    0,
    data_w as _,
    data_h as _,
    GL_RED,
    GL_UNSIGNED_BYTE,
    data.as_ptr() as _,
);

// print the pixels to the terminal
let mut counter = 0;
for _ in 0..data_h {
    for _ in 0..data_w {
        if data[counter] > 100 {
            print!("+");
        } else {
            print!(" ");
        }
        counter += 1;
    }
    println!()
}

终端测试的输出为:

++++ ++
   ++  +++
  ++    ++
  ++    ++
  ++    ++
  ++    ++
  ++   +++
   +++++++
    +++ ++
        ++
        ++
        +

因此,问题显然不在像素数据。
以下是将纹理渲染到屏幕时的外观:

为什么不能正确渲染?

bkkx9g8r

bkkx9g8r1#

事实证明,OpenGL在将数据写入纹理时会进行一些字节行对齐。解决方案是在glTexImage2D之前添加以下行:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

链接至:documentation

相关问题