c - Sprite 纹理中的透明度不起作用

标签 c opengl bmp

我正在尝试使用 OpenGL 将 Sprite 编程到我的 3D 场景中。我遇到的问题是,当我的 Sprite 出现在 3D 对象顶部的场景中时, Sprite 周围有一个黑框,而黑色应该是透明的。

我用 GIMP 创建了我的 sprite 表作为一个 32 位的 BMP 文件 (A8 B8 G8 R8)。此外,我还使用 GIMP 将每个应该透明的像素的 alpha 值设置为 0。我将我的 BMP 文件加载到我的程序中,如下所示:

unsigned int LoadSpriteTexBMP(const char* file)
{
    unsigned int   texture;    // Texture name
    FILE*          f;          // File pointer
    unsigned short magic;      // Image magic
    unsigned int   dx,dy,size; // Image dimensions
    unsigned short nbp,bpp;    // Planes and bits per pixel
    unsigned char* image;      // Image data
    unsigned int   k;          // Counter
    int            max;        // Maximum texture dimensions

    //  Open file
    f = fopen(file,"rb");

    //  Check image magic
    if (fread(&magic,2,1,f)!=1)
        Fatal("Cannot read magic from %s\n",file);

    if (magic!=0x4D42 && magic!=0x424D)
        Fatal("Image magic not BMP in %s\n",file);

    //  Seek to and read header
    if (fseek(f,16,SEEK_CUR) || fread(&dx ,4,1,f)!=1 || fread(&dy ,4,1,f)!=1 ||
    fread(&nbp,2,1,f)!=1 || fread(&bpp,2,1,f)!=1 || fread(&k,4,1,f)!=1)
        Fatal("Cannot read header from %s\n",file);

    //  Reverse bytes on big endian hardware (detected by backwards magic)
    if (magic==0x424D)
    {
        Reverse(&dx,4);
        Reverse(&dy,4);
        Reverse(&nbp,2);
        Reverse(&bpp,2);
        Reverse(&k,4);
    }

    //  Check image parameters
    glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);
    if (dx<1 || dx>max)
        Fatal("%s image width %d out of range 1-%d\n",file,dx,max);

    if (dy<1 || dy>max)
        Fatal("%s image height %d out of range 1-%d\n",file,dy,max);

    if (nbp!=1)
        Fatal("%s bit planes is not 1: %d\n",file,nbp);

    if (bpp!=32)
        Fatal("%s bits per pixel is not 32: %d\n",file,bpp);

    //  Allocate image memory
    size = 4*dx*dy;
    image = (unsigned char*) malloc(size);
    if (!image)
        Fatal("Cannot allocate %d bytes of memory for image %s\n",size,file);

    //  Seek to and read image
    if (fseek(f,20,SEEK_CUR) || fread(image,size,1,f)!=1)
        Fatal("Error reading data from image %s\n",file);
    fclose(f);

    //  Reverse colors (ABGR -> BGRA)
    for (k=0;k<size;k+=4)
    {
        unsigned char temp = image[k];

        image[k]   = image[k+1];
        image[k+1] = image[k+2];
        image[k+2] = image[k+3];
        image[k+3] = temp;
    }

    //  Generate 2D texture
    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D,texture);

    //  Copy image
    glTexImage2D(GL_TEXTURE_2D,0,3,dx,dy,0,GL_BGRA,GL_UNSIGNED_BYTE,image);
    if (glGetError())
        Fatal("Error in glTexImage2D %s %dx%d\n",file,dx,dy);

    //  Scale linearly when image size doesn't match
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

    //  Free image memory
    free(image);

    //  Return texture name
    return texture;

要设置在场景中显示我的 Sprite ,我使用以下内容:

unsigned int texture = LoadSpriteTexBMP("Sprite.bmp");
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV , GL_TEXTURE_ENV_MODE , GL_MODULATE);
glBindTexture(GL_TEXTURE_2D,texture[textureIndex]);

知道为什么我没有在 sprite 周围获得所需的透明效果吗?

最佳答案

尝试使用 glTexParameteri 将 GL_TEXTURE_WRAP_S 和 GL_TEXTURE_WRAP_T 设置为 GL_CLAMP_TO_EDGE。可能由于浮点误差,多边形边缘的纹理坐标略小于 0 或略大于 1。

关于c - Sprite 纹理中的透明度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762507/

相关文章:

c - 有什么方法可以保护变量在运行时在 C 中被修改吗?

c++ - FPS 风格相机

C编程读取多行并暂停,直到按下键盘上的任意键

c - 属性错误: opencv python on raspberry pi

c++ - 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str':非标准语法;使用 '&' 创建指向成员的指针

C++ OpenGL SDL 屏幕问题

c++ - 读取bmp文件中的像素值

java - 如何用java保存一个巨大的bmp文件?

c - 难以写入位图文件

c - 如何取消引用字符串指针