【问题标题】:Converting transparent pngs Crashes SDL/OpenGL转换透明 png 会导致 SDL/OpenGL 崩溃
【发布时间】:2012-03-25 17:13:49
【问题描述】:

如果我尝试将透明的 .png SDL_Surface 转换为 OpenGL 纹理,那么当程序尝试从 SDL_Surface 访问任何数据时,它就会崩溃。根据其他提问者的说法,我观察到其他人的代码涉及透明 png,但它们是相同的并且不会崩溃。这是我的代码,程序崩溃的部分。它在“n0fColors = surface->format->BytesPerPixel”处崩溃。

GLuint CONVERT_IMAGE(SDL_Surface * surface)
{
    GLuint texture;
    GLenum texture_format;
    GLint nOfColors;

    nOfColors = surface->format->BytesPerPixel;
    if (nOfColors == 4)
    {
        if (surface->format->Rmask == 0x000000ff)
            texture_format = GL_RGBA;
        else
            texture_format = GL_BGRA;
    }
    else if (nOfColors == 3)
    {
        if (surface->format->Rmask == 0x000000ff)
            texture_format = GL_RGB;
        else
            texture_format = GL_BGR;
    }

    glGenTextures( 1, &texture );

glBindTexture( GL_TEXTURE_2D, texture );

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(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);

glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );

    delete surface;
    return texture;
};

编辑:我应该注意 PNG 的透明部分是 %100 透明的。 编辑:这是我对图像的调用:

void image_init()
{
    wall_clay_0 = IMG_Load("images/walls/clay.png");

    walls_image.clay = CONVERT_IMAGE(wall_clay_0);
};

【问题讨论】:

  • 检查surface和surface->format不是NULL指针。
  • 它返回 NULL。没有一个 SDL_Surface 不返回 null,即使是不透明的也是如此。
  • 这意味着问题出在调用代码上,而不是在这个函数上。
  • 顺便说一句,同样的代码可以在 bmps 上运行,不会崩溃或出现任何问题。
  • 图像没有返回 NULL,只是表面->格式->bytesperpixels。

标签: c++ opengl png sdl transparent


【解决方案1】:

你的加载代码应该是这样的:

void image_init()
{
    wall_clay_0 = IMG_Load("images/walls/clay.png");
    if (!wall_clay_0)
    {
        printf("Error loading mage: %s\n", IMG_GetError());
        abort(); // or whatever your application should do
    }
    walls_image.clay = CONVERT_IMAGE(wall_clay_0);
}

【讨论】:

  • 嗯,我明白你说表面是 NULL。更具体地说明什么是什么。
  • 表面加载正常,不为空,但表面->格式为空。
【解决方案2】:

我想通了。我需要使所有加载的图像成为临时表面,然后使其等于自身的 SDL_DisplayFormatAlpha。例如:

SDL_Surface * surface;
surface = IMG_Load(bla.png);
surface = SDL_DisplayFormatAlpha(surface);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    相关资源
    最近更新 更多