简体   繁体   中英

Problems Loading 32 bit .bmp texture in opengl c++

I'm having some trouble loading a 32 bit .bmp image in my opengl game. So far i can load and display 24 bit perfectly. I now want to load a bit map with portions of its texture being transparent or invisible.

This function has no problems with 24 bit. but 32 bit with alpha channel .bmp seem to distort the colors and cause transparently in unintended places.

Texture LoadTexture( const char * filename, int width, int height, bool alpha) 
    {
        GLuint texture;
        GLuint* data;
        FILE* file;

        fopen_s(&file, filename, "rb");  
        if(!file)
        {
            std::cout << filename <<": Load Texture Fail!\n";
            exit(0);
        }

        data = new GLuint[width * height];

        fread( data, width * height * sizeof(GLuint), 1, file ); 
        fclose(file);

        glGenTextures( 1, &texture );
        glBindTexture( GL_TEXTURE_2D, texture);
        glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        if(alpha) //for .bmp 32 bit
        {
            glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
            glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
        }
        else //for .bmp 24 bit
        {
            glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 
            glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
        }

        std::cout<<"Texture: "<<filename<< " loaded"<<std::endl;
        delete data;

        return Texture(texture); 
    }

In Game Texture, drawn on a flat plane 在此处输入图片说明

this might look like its working but the 0xff00ff color is the one that should be transparent. and if i reverse the alpha channel in photoshop the result is the same the iner sphere is always transparent.

i also enabled:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

there is no problems with transparency the problems seems to be with loading the bitmap with an alpha channel. also all bit maps that I load seem to be off a bit to the right. Just wondering if there was a reason for this?

I'm going to answer this on the assumption that your file "parsing" is in-fact correct. That the file data is just the image part of a .BMP without any of the header information.

glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

I find it curious that your 3-component data is in BGR order, yet your 4-component data is in RGBA order. Especially if this data comes from a .BMP file (though they don't allow alpha channels). Are you sure that your data isn't in a different ordering? For example, perhaps ABGR order?

Also, stop using numbers for image formats . You should use a real, sized internal format for your textures, not "3" or "4".

So code that i use work for 100% for me compiled by visual c 2012

glBindTexture(GL_TEXTURE_2D, texture_id);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //Thia is very important!!!!
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  imag_ptr,  ptr->image->height, 0,GL_RGBA, GL_UNSIGNED_BYTE, imag_ptr); 

and than in render i use

glPushMatrix();
glTranslatef(0,0,0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 1);
glEnable(GL_BLEND);
glColor4ub(255,255,255,255);  //This is veryveryvery importent !!!!!!!!!!! (try to play and you see)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord3d(1,  1,  0);
glVertex2f(8,8);
glTexCoord3d(0,  1,  0);
glVertex2f(-8,8);
glTexCoord3d(0,  0,  0);
glVertex2f(-8,-8);
glTexCoord3d(1,  0,  0);
glVertex2f(8,-8);
glEnd();
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();

and i use for example android png icon and another i try to post another image but i have no peputation for this so if you want i send it to you

So all of this in png format but in bmp format and tga you must swap colors from ARGB to RGBA without this its not working

for( x=0;x<bmp_in_fheader.width;x++)
    {
        t=pixel[i];
        t1=pixel[i+1];
        t2=pixel[i+2];
        t3=pixel[i+3];

        pixel_temp[j]=t2;
        pixel_temp[j+1]=t1;
        pixel_temp[j+2]=t;
        pixel_temp[j+3]=t3;

        i+=4;
        j+=4;
    }

==Next== To crate them in photoshop you must delete your background and draw on new layer than add alpha layer in channels REMEMBER !! Very important to that in alpha all black color is represent transparency and your image must be under white color only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM