【发布时间】:2014-12-06 15:37:58
【问题描述】:
我的 openGL 程序只渲染了一半的纹理。我使用以下代码加载了 24Bit .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 接收到纹理但无法完全渲染,结果类似于
在尝试了不同的图像后,它在纹理的不同部分都失败了。 有人认出这个错误吗?
【问题讨论】:
-
我建议使用图像库而不是重新发明轮子。至少,如果它失败了,你会在渲染方面知道它。
-
“openGL 调整纹理大小”是什么意思?
-
imageSize是错误的,对于初学者来说。.bmp(与设备无关的位图)将扫描线存储在 4 字节边界上。图像的实际大小比简单的width * height大,您必须在每条扫描线的末尾考虑额外的存储空间以满足 4 字节对齐。这对于带有 Alpha 通道的位图来说不是什么大问题,但对于 RGB(24 位)来说却是一个大问题。
标签: c++ opengl bitmap ifstream texture-mapping