我一直在尝试使用SDL2在我的OpenGL场景中进行文本渲染。我找到的教程是这个 Rendering text
我遵循了相同的代码,文本渲染正常。然而,我的问题是OpenGL渲染和代码中使用的SDL_Renderer
之间显然存在“冲突”。当我运行场景时,文字会显示出来,但其他所有东西都在闪烁。以下gif显示了此问题:
有没有办法在仅使用SDL2
和OpenGL
而不使用其他库或任何内容的情况下解决这个问题。
我一直在尝试使用SDL2在我的OpenGL场景中进行文本渲染。我找到的教程是这个 Rendering text
我遵循了相同的代码,文本渲染正常。然而,我的问题是OpenGL渲染和代码中使用的SDL_Renderer
之间显然存在“冲突”。当我运行场景时,文字会显示出来,但其他所有东西都在闪烁。以下gif显示了此问题:
有没有办法在仅使用SDL2
和OpenGL
而不使用其他库或任何内容的情况下解决这个问题。
SDL_Texture* renderText(const std::string &message, const std::string &fontFile,
SDL_Color color, int fontSize, SDL_Renderer *renderer)
{
//Open the font
TTF_Font *font = TTF_OpenFont(fontFile.c_str(), fontSize);
if (font == nullptr){
logSDLError(std::cout, "TTF_OpenFont");
return nullptr;
}
//We need to first render to a surface as that's what TTF_RenderText
//returns, then load that surface into a texture
SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
if (surf == nullptr){
TTF_CloseFont(font);
logSDLError(std::cout, "TTF_RenderText");
return nullptr;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
if (texture == nullptr){
logSDLError(std::cout, "CreateTexture");
}
//Clean up the surface and font
SDL_FreeSurface(surf);
TTF_CloseFont(font);
return texture;
}
请参见http://www.willusher.io/sdl2%20tutorials/2013/12/18/lesson-6-true-type-fonts-with-sdl_ttf/,了解如何使用SDL_TTF实现True Type字体。
绑定SDL_Texture。
void SDL_BindTexture(SDL_Texture *t)
{
glBindTexture(GL_TEXTURE_2D, t->id);
}
const GLfloat quadVertices[] = { -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f,-1.0f, 0.0f,
-1.0f,-1.0f, 0.0f
};
glVertexPointer(3, GL_FLOAT, 0, quadVertices);
glDrawArrays(GL_QUADS, 0, 4);