簡體   English   中英

無法在OpenGL中正確使用位圖

[英]Can't correctly use bitmap in OpenGL

我制作了自己的位圖加載器。 有一個功能可以從文件中加載數據。 它是24位。

這是文件的十六進制數據:

42 4d 46 00 00 00 00 00 00 00 36 00 00 00 28 00
00 00 02 00 00 00 02 00 00 00 01 00 18 00 00 00
00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 ff ff ff ff 00 00 ff 00
00 00 ff 00 00 00

這是我的加載程序(還將信息輸出到文件中):

void load_texture(std::string path)
{
    std::ifstream f;
    f.open(path, std::ios_base::binary);
    std::vector<unsigned char> store;
    char buf[255];
    while(!f.eof())
    {
        unsigned char g;
        f >> g;
        store.push_back(g);
    }
    f.close();

std::ofstream l("info.txt");
    int offset = store[0xA];
    int width = store[0x12];
    int height = store[0x16];
    int sizeraw = ((width*3)*height)+height-1*2;
    int a = store[(int)offset+1];
    int pad = 0;
    std::vector<BGR*> imageraw;
    std::vector<int*> image;
    for(int y = 0; y < height;y++)
    {
        for(int x = pad; x < pad + (width*3); x+=3)
        {
            imageraw.push_back(new BGR(store[offset +x], store[offset +x+1], store[offset +x+2]));
        }
        pad += (width*3)+2;
    }
    for(int i = 0; i < imageraw.size(); i++)
    {
        l << "---------------------------------\n";
        image.push_back(new int(imageraw[i]->B));
        image.push_back(new int(imageraw[i]->G));
        image.push_back(new int(imageraw[i]->R));
        l << "B : "; l << imageraw[i]->B; l << " \n";
        l << "G : "; l << imageraw[i]->G; l << " \n";
        l << "R : "; l << imageraw[i]->R; l << " \n";
    }
    glEnable(GL_TEXTURE_2D);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.data());

l.close();

}

這是BGR結構:

struct BGR
{
    int B;
    int G;
    int R;

    BGR(int a, int b, int c): B(a), G(b), R(c) {};
};

我不知道為什么,但是OpenGL用錯誤的顏色顯示圖像。 我還必須指出,我是使用文件中十六進制數據的初學者。 此外,輸出到文件(info.txt)的輸出正確。

OpenGL為什么顯示錯誤的顏色?

另一件事,我想給OGL的是BGR的整數值,例如info.txt中的值。 沒有十六進制也沒有bin。 編輯:

這是文件結果:

---------------------------------
B : 0 
G : 0 
R : 255 
---------------------------------
B : 255 
G : 255 
R : 255 
---------------------------------
B : 255 
G : 0 
R : 0 
---------------------------------
B : 0 
G : 255 
R : 0 

GL_UNPACK_ALIGNMENT假定圖像的每一行都是4的倍數,如果不是,則將導致像素未對准。

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

您的像素結構為3個字節,將解壓縮對齊方式更改為1將確保行之間沒有移位。

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

您也可以自己(在創建bmp時)在每行之間移動字節,以使其字節計數為4的倍數。

您正在向GL發送完全錯誤的數據:

std::vector<int*> image;

在這里,您使用的是指向整數指針向量,並為每個像素向新分配的圖像添加3個指針 這不是GL能夠使用的所有功能。(此外,您還會在這里泄漏大量的內存)。

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.data())

現在,您告訴GL,它將找到一個width乘以包含BGR圖像數據的height像素的圖像,每個分量都將是GL_UNSIGNED_BYTE ,因此類型為GLubyte (或者在任何特殊情況下為unsigned char )始於地址image.data() 因此,您不需要int ,並且您當然不需要int*指針。

您的std::vector<BGR*> imageraw; std::vector<int*> image; 數據結構實際上是完全無用的,您可以直接使用store向量的相關部分,該部分將已經包含正確格式的數據(assui = ming非壓縮標准BMP文件,但是其余代碼假定相同)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM