0

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)

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: when I comment out those 2 lines

4
  • Maybe the easier way would be to use some library for loading images. I recomend lodepng for png images. Commented Apr 5, 2014 at 20:35
  • Can try this: pastebin.com/sNXF1QXx it should work.. Commented Apr 5, 2014 at 20:44
  • Hah, I know I have no solution for your question but your code of loading a bitmap helped me to understand how to load a texture. (Works fine for me). Thanks Commented Dec 3, 2014 at 13:09
  • Thanks for the BMP loading function, too. Commented Jan 25, 2017 at 17:04

2 Answers 2

2

Two points:

You probably need to swap the BMP channels from BGR to RGB (because BGR is the default color order of bmp files)

There is a command glColor3f(0.0, 0.0, 0.0); before the drawing, isn´t it making all the colors black?


Extra:

Make sure you are running in compatibility mode to be able to draw between glBegin(); and glEnd();

Sign up to request clarification or add additional context in comments.

Comments

-1

Probably forgot to

glEnable( GL_TEXTURE_2D );

Since OpenGL 1.2 (1998) you can do

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

Are you going to delete that data?

OpenGL 1.0 compatible version

    static bool 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 );

        for ( unsigned int i = 0; i < size; i += 3 ) {
            unsigned char red = data[i];
            data[i] = data[i + 2];
            data[i + 2] = red;
        }

        fclose( file );

        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 );

        delete data;
        return true;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.