gpt4 book ai didi

c++ - 从 .bmp openGL 加载纹理的奇怪错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:19 24 4
gpt4 key购买 nike

我的 openGL 程序只渲染了一半的纹理。我使用以下代码加载 24 位 .bmp。

unsigned char header[54]; // Each BMP file begins by a 54-bytes header
unsigned int dataPos; // Position in the file where the actual data begins
unsigned int width, height;
unsigned int imageSize; // = width*height*3
// Actual RGB data
unsigned char * data;

std::ifstream file("fnai.bmp");

if (!file.is_open()) {
std::cout << "Could not open file: " << "C:\\Users\\Danne\\Documents\\Visual Studio 2013\\Projects\\02 - OpenGL\\BTH24.bmp "<< std::endl;
}
char c;
for (int i = 0; i < 54; i++) {
file.get(c);
header[i] = c;
}
if (header[0] != 'B' || header[1] != 'M') {
std::cout << "Incorrect or corrupt bmp file" << std::endl;
}
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) {
imageSize = width*height * 3;
}
if (dataPos == 0) {
dataPos = 54;
}
data = new unsigned char[imageSize*3];

for (int i = 0; i < imageSize*3; i++) {
file.get(c);
data[i] = c;
}
file.close();

加载纹理时,纹理加载并且 openGL 接收纹理但未能完全渲染它,结果最终类似于 enter image description here

尝试不同的图像后,它在纹理的不同部分失败了。有没有人认识到这个错误?

最佳答案

我认为问题可能出在位图的位深度上。您应该尝试将其与 24 位(位深度)bmp 文件一起使用。在 photoshop 中将文件保存为 bmp 时,您可以更改位深度。我个人不知道为什么只能使用 24 位,但我建议尝试一下。

关于c++ - 从 .bmp openGL 加载纹理的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27333275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com