soupdiver
soupdiver

Reputation: 3673

c++ opengl bmp and alpha channel

I have some bmp's saved in 32bit via photoshop. The first 24 bit should represent RGB and the last 8 bit should represent the alpha channel. I'm loading my bitmap:

Bitmap* bStart = new Bitmap("starten.bmp");

I'm loading the file and setting the values for the alpha channel

Bitmap::Bitmap(const char* filename) {
    FILE* file;
    file = fopen(filename, "rb");

    if(file != NULL) { // file opened
        BITMAPFILEHEADER h;
        fread(&h, sizeof(BITMAPFILEHEADER), 1, file); //reading the FILEHEADER
        fread(&this->ih, sizeof(BITMAPINFOHEADER), 1, file);

        this->pixels = new GLbyte[this->ih.biHeight * this->ih.biWidth * this->ih.biBitCount / 8];
        fread(this->pixels, this->ih.biBitCount / 8 * this->ih.biHeight * this->ih.biWidth, 1, file);
        fclose(file);

        for (int i = 3; i < this->ih.biHeight * this->ih.biWidth * this->ih.biBitCount / 8; i+=4) {
            this->pixels[i] = 255;
        }
    }
}

Setting the texture:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bStart->ih.biWidth, bStart->ih.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, bStart->pixels);

the init stuff:

glutInit(argcp, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);

//not necessary because we using fullscreen
//glutInitWindowSize(550, 550);
//glutInitWindowPosition(100, 100);

glutCreateWindow(argv[0]); //creating the window
glutFullScreen(); //go into fullscreen mode

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //art und weise wie texturen gespeicehrt werden

glClearColor (1.0, 0.6, 0.4, 0.0);

//setting the glut functions
glutDisplayFunc(displayCallback);
glutKeyboardFunc(keyboardCallback);

glutMainLoop(); //starting the opengl mainloop... and there we go

I can see my textures in my app but their background isn't transparent... it is kind of light blue. What am I doing wrong?

Upvotes: 1

Views: 2066

Answers (1)

datenwolf
datenwolf

Reputation: 162164

I have some bmp's saved in 32bit via photoshop. The first 24 bit should represent RGB and the last 8 bit should represent the alpha channel.

While this is possible to implement, this kind of alpha channel in a DIB file (that's what the structure of .BMPs is actually called) has never been properly specified. So it might simply be not stored at all. But that doesn't matter as you overwrite the alpha values with 1 in your DIB loader... I mean that part:

for (int i = 3; i < this->ih.biHeight * this->ih.biWidth * this->ih.biBitCount / 8; i+=4) {
        this->pixels[i] = 255;
    }

Upvotes: 2

Related Questions