2014-12-06 68 views
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接收紋理,但不能完全呈現它,結果落得像enter image description here

嘗試它未能在質地的不同部位不同的圖像後。 有沒有人認識到這個錯誤?

+3

我會建議使用圖像庫,而不是重新發明輪子。至少在那時,如果它失敗了,你會在渲染方面知道它。 – Borgleader 2014-12-06 15:44:52

+0

「openGL調整紋理大小」是什麼意思? – BWG 2014-12-06 15:45:31

+1

'imageSize'對於初學者來說是錯誤的。 '.bmp'(Device Independent Bitmap)存儲4字節邊界上的掃描線。圖像的實際尺寸大於簡單的「width * height」,您必須在每條掃描線的末尾考慮額外的存儲空間以滿足4字節對齊。這對於具有alpha通道的位圖來說不是一個大問題,但對於RGB(24位)來說這是一個巨大的問題。 – 2014-12-06 17:05:24

回答

0

我認爲這個問題可能是位圖的位深度。你應該嘗試使用這個24位(位深度)bmp文件。以bmp格式保存文件時,可以更改位深度。我個人不知道爲什麼只有24位工作,但我建議嘗試它。