Simol
Simol

Reputation: 639

load texture using opengl and c++

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

Upvotes: 0

Views: 7565

Answers (2)

Kahler
Kahler

Reputation: 1150

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

Upvotes: 2

Anton Duzenko
Anton Duzenko

Reputation: 2616

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

Upvotes: -1

Related Questions