【发布时间】: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