[英]Saving a single color OpenGL texture to a file results in a 64x64 black box
此代码片段旨在创建具有单一颜色的 GL 纹理,然后将原始像素数据保存到磁盘。 然后,我使用 ffmpeg 将其转换为 PNG。我尝试了多种生成纹理的方法,以及多种保存纹理数据的方法,但结果始终相同 - 一个 1920x1080 图像,角落有一个 64x64 黑框。 我期望的是 1920x1080 的单色图像。
我究竟做错了什么?
转换命令:
ffmpeg -pix_fmt rgba -s 1920x1080 -i texture.raw -f image2 output.png
代码:
gpu::gles2::GLES2Interface* gl = GetContextProvider()->ContextGL();
GLuint texture;
gl->GenTextures(1, &texture);
gl->BindTexture(GL_TEXTURE_2D, texture);
int width = 1920;
int height = 1080;
std::vector<unsigned char> data(width * height * 4, 0);
for (size_t i = 2; i < data.size(); i += 4) {
data[i] = 255; // blue channel
}
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
std::vector<unsigned char> buffer(width * height * 4);
gl->ReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data());
std::ofstream file("texture.raw", std::ios::binary);
file.write(reinterpret_cast<char*>(buffer.data()), buffer.size());
file.close();
从这里的主要文档中查看glReadPixels
的描述: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glReadPixels.xhtml 。
本质上glReadPixels
用于从当前帧缓冲区获取像素,而不是从当前绑定的GL_TEXTURE_2D
获取像素。 如果没有更多代码和上下文,我无法 100% 自信地回答,但看起来您拥有的代码在任何内容实际渲染到帧缓冲区之前就已经存在,而您只是在进行设置。 您最有可能得到一个黑框,因为保存到缓冲区的数据无效。
希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.