如果我试图将透明.png SDL_Surface转换为OpenGL纹理,那么当程序试图访问SDL_Surfaces中的任何数据时,它就会崩溃。据其他询问者称,我观察到其他人的代码涉及透明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);
};
您的加载代码应该是这样的:
void image_init()
{
wall_clay_0 = IMG_Load("images/walls/clay.png");
if (!wall_clay_0)
{
printf("Error loading mage: %sn", IMG_GetError());
abort(); // or whatever your application should do
}
walls_image.clay = CONVERT_IMAGE(wall_clay_0);
}
我已经想通了。我需要使所有加载的图像成为临时曲面,然后使其等于SDL_DisplayFormatAlpha本身。例如:
SDL_Surface * surface;
surface = IMG_Load(bla.png);
surface = SDL_DisplayFormatAlpha(surface);