负载纹理使用opengl和c++。

时间:2022-03-31 04:58:24

I've used such code to load a .bmp file as a texture and I want to fill a rectangle(for example the one on the right wall with it)

我使用了这样的代码来加载一个.bmp文件作为纹理,我想填充一个矩形(例如右边的那个)

GLuint LoadBMP(const char *fileName)
{
    FILE *file;
    unsigned char header[54];
    unsigned int dataPos;
    unsigned int size;
    unsigned int width, height;
    unsigned char *data;


    file = fopen(fileName, "rb");

    if (file == NULL)
    {
        //MessageBox(NULL, L"Error: Invaild file path!", L"Error", MB_OK);
        return false;
    }

    if (fread(header, 1, 54, file) != 54)
    {
        //MessageBox(NULL, L"Error: Invaild file!", L"Error", MB_OK);
        return false;
    }

    if (header[0] != 'B' || header[1] != 'M')
    {
        //MessageBox(NULL, L"Error: Invaild file!", L"Error", MB_OK);
        return false;
    }

    dataPos     = *(int*)&(header[0x0A]);
    size        = *(int*)&(header[0x22]);
    width       = *(int*)&(header[0x12]);
    height      = *(int*)&(header[0x16]);

    if (size == NULL)
        size = width * height * 3;
    if (dataPos == NULL)
        dataPos = 54;

    data = new unsigned char[size];

    fread(data, 1, size, file);

    fclose(file);
    GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
return texture;
}

and use it like this:

像这样使用它:

glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();

            glColor3f(0.0, 0.0, 0.0);

            GLuint texture = LoadBMP("mina.bmp");
            glEnable(GL_TEXTURE_2D);

            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
            glBindTexture(GL_TEXTURE_2D, texture);
            glBegin(GL_QUADS);
                glTexCoord2i(0, 0); glVertex2i(0, 0);
                glTexCoord2i(0, 1); glVertex2i(0, 5);
                glTexCoord2i(1, 1); glVertex2i(5, 5);
                glTexCoord2i(1, 0); glVertex2i(5, 0);
            glEnd();

            glDisable(GL_TEXTURE_2D);

but when I run it, it does nothing and when I comment out these 2 lines:

但是当我运行它时,它什么也不做,当我注释掉这两行:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

the ouput is a black rectangle not a textured rectangle. I don't know what is wrong! Is it about the .bmp file that I use? I changed format of a jpeg with microsoft paint to .bmp file. I even tried with a .bmp file created by visual studio. here is the second output I said: 负载纹理使用opengl和c++。

外框是一个黑色矩形而不是纹理矩形。我不知道怎么了!是关于我使用的。bmp文件吗?我将带有microsoft paint的jpeg格式更改为.bmp文件。我甚至尝试使用visual studio创建的.bmp文件。这是我说的第二个输出:

1 个解决方案

#1


3  

Couple of stuff:

两个东西:

You probably need to swap the BMP channels from BGR to RGB

您可能需要将BMP通道从BGR交换到RGB

Isn't that glColor3f(0.0, 0.0, 0.0); the culprit blacking it all out?

是不是glColor3f(0 0 0 0 0 0 0 0 0)罪犯把一切都搞砸了?


Extra:

额外的:

I too miss (kinda) the times of glBegin(); glEnd(); but make sure you are running in compatibility mode. And be ready to move to shaders.

我也想念glBegin()的时代;glEnd();但是要确保在兼容性模式下运行。准备好转移到阴影部分。

#1


3  

Couple of stuff:

两个东西:

You probably need to swap the BMP channels from BGR to RGB

您可能需要将BMP通道从BGR交换到RGB

Isn't that glColor3f(0.0, 0.0, 0.0); the culprit blacking it all out?

是不是glColor3f(0 0 0 0 0 0 0 0 0)罪犯把一切都搞砸了?


Extra:

额外的:

I too miss (kinda) the times of glBegin(); glEnd(); but make sure you are running in compatibility mode. And be ready to move to shaders.

我也想念glBegin()的时代;glEnd();但是要确保在兼容性模式下运行。准备好转移到阴影部分。