1
我的openGL程序只渲染了一半的纹理。我使用下面的代码来加载一个24Bit .bmp。奇怪的错误从.bmp加载纹理openGL
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接收纹理,但不能完全呈现它,结果落得像
尝试它未能在质地的不同部位不同的图像后。 有没有人认识到这个错误?
我会建议使用图像库,而不是重新发明轮子。至少在那时,如果它失败了,你会在渲染方面知道它。 – Borgleader 2014-12-06 15:44:52
“openGL调整纹理大小”是什么意思? – BWG 2014-12-06 15:45:31
'imageSize'对于初学者来说是错误的。 '.bmp'(Device Independent Bitmap)存储4字节边界上的扫描线。图像的实际尺寸大于简单的“width * height”,您必须在每条扫描线的末尾考虑额外的存储空间以满足4字节对齐。这对于具有alpha通道的位图来说不是一个大问题,但对于RGB(24位)来说这是一个巨大的问题。 – 2014-12-06 17:05:24