【问题标题】:c++ opengl bmp and alpha channelc++ opengl bmp和alpha通道
【发布时间】:2012-01-12 15:51:06
【问题描述】:

我有一些通过 Photoshop 以 32 位保存的 bmp。 前 24 位应代表 RGB,后 8 位应代表 Alpha 通道。 我正在加载我的位图:

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

我正在加载文件并设置 Alpha 通道的值

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

设置纹理:

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

初始化的东西:

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

我可以在我的应用程序中看到我的纹理,但它们的背景不透明……它是一种浅蓝色。 我做错了什么?

【问题讨论】:

    标签: c++ opengl bmp alpha-transparency


    【解决方案1】:

    我有一些通过 Photoshop 以 32 位保存的 bmp。前 24 位代表 RGB,后 8 位代表 Alpha 通道。

    虽然这是可以实现的,但从未正确指定 DIB 文件中的这种 alpha 通道(实际调用 .BMPs 的结构)。所以它可能根本不存储。但这并不重要,因为您在 DIB 加载器中用 1 覆盖了 alpha 值......我的意思是那部分:

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

    【讨论】:

    • 是的,第 4 个通道被声明为 X,我红色它应该存储一些关于填充的信息......这就是我自己覆盖字节的原因。
    • @skelle:那么你认为 alpha 存储在哪里?这也与您问题的第一句话相矛盾。
    • @TomWhittock alpha 通道应该存储在第 4 个通道中,我为每个像素用 255 覆盖。我也告诉我的纹理使用 RGBA,但我没有得到透明背景
    • @skelle: 255 alpha == 0% 透明度。通过覆盖 Alpha 通道,我不确定您的期望。
    • @TomWhittock Ahhhh 好的,我必须输入 0 而不是 255 >_
    猜你喜欢
    • 2014-01-18
    • 2011-10-29
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多