我有一个生成obj纹理的函数,另一个显示它的函数。为了不泄漏内存,我尝试只在第一个函数中调用一次,然后在第二个函数中使用 GLuint importText(const std::string &text,int font_size,int red,int green,int blue, texto_data *texto){
SDL_Color font_color = {blue,green,red};
TTF_Font* font=TTF_OpenFont("arial.ttf",font_size);
SDL_Surface *image = TTF_RenderText_Blended(font,text.c_str(),font_color);
SDL_DisplayFormatAlpha(image);
glGenTextures(1,&texto->texture);
glBindTexture(GL_TEXTURE_2D, texto->texture);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA,image->w,image->h,0,GL_RGBA,GL_UNSIGNED_BYTE,image->pixels);
SDL_FreeSurface(image);
TTF_CloseFont(font);
return texto->texture;
}
,问题是此函数不适用于我。当我启动程序时,纹理没有更新。
这是生成纹理的功能:
int carrega_texto(texto_data *texto) {
SDL_Color font_color = {texto->b,texto->g,texto->r};
TTF_Font* font = TTF_OpenFont("arial.ttf",texto->tamanho);
SDL_Surface *image = TTF_RenderText_Blended(font,texto->string,font_color);
SDL_DisplayFormatAlpha(image);
glBindTexture(GL_TEXTURE_2D, texto->texture);
glTexSubImage2D(GL_TEXTURE_2D, 0,0,image->w,image->h,0,GL_RGBA,GL_UNSIGNED_BYTE,image->pixels);
SDL_FreeSurface(image);
TTF_CloseFont(font);
glBindTexture(GL_TEXTURE_2D, texto->texture);
glColor4ub(255, 255, 255, 255);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2f(texto->area.a.x, texto->area.a.y);
glTexCoord2d(1,0); glVertex2f(texto->area.b.x, texto->area.b.y);
glTexCoord2d(1,1); glVertex2f(texto->area.c.x, texto->area.c.y);
glTexCoord2d(0,1); glVertex2f(texto->area.d.x, texto->area.d.y);
glEnd();
}
这是显示功能:
glDeleteTextures
Nt:为了不使用glTexSubImage2D
,我一直尝试仅调用生成的纹理并使用glGenTexture
进行删除。问题是pip
总是增加变量 texto-> texture ,然后溢出。