我以前从来没有遇到过这个错误,我glTexImage2D
在项目的其他地方使用没有错误。下面是 Visual Studio 显示的错误截图,以及反汇编视图:
鉴于该行包含ptr
在其中,我假设存在指针错误,但我不知道我做错了什么。下面是我用来从SDL_surface
一个纹理转换为纹理的函数。
void surfaceToTexture(SDL_Surface *&surface, GLuint &texture) {
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
glDisable(GL_TEXTURE_2D);
}
此函数在程序的其他地方成功,例如在加载文本时:
SDL_Surface *surface;
surface = TTF_RenderText_Blended(tempFont, message.c_str(), color);
if (surface == NULL)
printf("Unable to generate text surface using font: %s! SDL_ttf Error: %s\n", font.c_str(), TTF_GetError());
else {
SDL_LockSurface(surface);
width = surface->w;
height = surface->h;
if (style != TTF_STYLE_NORMAL)
TTF_SetFontStyle(tempFont, TTF_STYLE_NORMAL);
surfaceToTexture(surface, texture);
SDL_UnlockSurface(surface);
}
SDL_FreeSurface(surface);
但不是在加载图像时:
SDL_Surface* surface = IMG_Load(path.c_str());
if (surface == NULL)
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
else{
SDL_LockSurface(surface);
width = (w==0)?surface->w:w;
height = (h==0)?surface->h/4:h;
surfaceToTexture(surface, texture);
SDL_UnlockSurface(surface);
}
SDL_FreeSurface(surface);
这两个示例都是从texture
定义的类中提取的。图像的路径是正确的。我知道这glTexImage2D
是导致问题的原因,因为我在开始时添加了一个断点surfaceToTexture
并逐步执行该函数。即使它不起作用,texture
并且surface
确实具有看似正确的值/属性。
有任何想法吗?