如果我尝试将透明的.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);
};
答案 0 :(得分:0)
您的加载代码应如下所示:
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);
}
答案 1 :(得分:0)
我已经弄清楚了。我需要将所有加载的图像临时表面,然后使其等于自己的SDL_DisplayFormatAlpha。例如:
SDL_Surface * surface;
surface = IMG_Load(bla.png);
surface = SDL_DisplayFormatAlpha(surface);