使用SDL_ttf和OpenGL

19

我正在使用OpenGL和SDL创建程序窗口。

如何在OpenGL窗口中使用SDL_ttf?

举个例子,我想要加载一个字体并渲染一些文本。我希望使用一个SDL OpenGL表面来绘制这个文本。

2个回答

34

以下是实现的步骤:

  1. 初始化SDL和SDL_ttf,并使用SDL_SetVideoMode()创建一个窗口。确保传递了SDL_OPENGL标志。
  2. 初始化你的OpenGL场景(例如glViewport()glMatrixMode()等)。
  3. 使用SDL_ttf渲染你的文本,例如TTF_RenderUTF8_Blended()。渲染函数将返回一个SDL_surface,你需要通过将指向数据的指针(surface->pixels)以及数据的格式传递给OpenGL,将其转换为OpenGL纹理。就像这样:
colors = surface->format->BytesPerPixel;
if (colors == 4) {   // alpha
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGBA;
    else
        texture_format = GL_BGRA;
} else {             // no alpha
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGB;
    else
        texture_format = GL_BGR;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); 
glTexImage2D(GL_TEXTURE_2D, 0, colors, surface->w, surface->h, 0,
                    texture_format, GL_UNSIGNED_BYTE, surface->pixels);
  1. 之后你可以使用glBindTexture()等函数在OpenGL中使用纹理。确保在绘制结束后调用SDL_GL_SwapBuffers()

我按照你说的做了,但是它无法工作。它给我一个白色矩形而不是文本。 - Donald Duck
嗨@DonaldDuck,我猜你在http://stackoverflow.com/questions/38750624/text-in-opengl-using-sdl-ttf得到了答案。 - Antti
我曾经使用TTF_RenderText()函数,但是得到了疯狂的像素。使用混合模式GL_RGBA对我很有帮助。 - Luminaire

2

基于:http://content.gpwiki.org/index.php/SDL_ttf:Tutorials:Fonts_in_OpenGL

下面的代码是一个示例,展示了如何在你已经构建好的3D模型上渲染文本。

#include "SDL.h"
#include "SDL_ttf.h"

/.../

void RenderText(std::string message, SDL_Color color, int x, int y, int size) {
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  gluOrtho2D(0, m_Width, 0, m_Height); // m_Width and m_Height is the resolution of window
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();

  glDisable(GL_DEPTH_TEST);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  GLuint texture;
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);

  TTF_Font * font = TTF_OpenFont("pathToFont.ttf", size);
  SDL_Surface * sFont = TTF_RenderText_Blended(font, message, color);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sFont->w, sFont->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, sFont->pixels);

  glBegin(GL_QUADS);
  {
    glTexCoord2f(0,0); glVertex2f(x, y);
    glTexCoord2f(1,0); glVertex2f(x + sFont->w, y);
    glTexCoord2f(1,1); glVertex2f(x + sFont->w, y + sFont->h);
    glTexCoord2f(0,1); glVertex2f(x, y + sFont->h);
  }
  glEnd();

  glDisable(GL_BLEND);
  glDisable(GL_TEXTURE_2D);
  glEnable(GL_DEPTH_TEST);

  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  glDeleteTextures(1, &texture);
  TTF_CloseFont(font);
  SDL_FreeSurface(sFont);
}

/.../

int main() {

/.../ Render 3D stuff here

  // Prints out "Hello World" at location (5,10) at font size 12!
  SDL_Color color = {255, 0, 0, 0}; // Red
  RenderText("Hello World", color, 5, 10, 12); 

/.../

  return 0;
}

3
这个解决方案会导致代码每次调用RenderText()时都从磁盘重新加载.ttf文件并重新生成字形纹理。我无法想象这会有任何效率上的提升。 - stix
直到我按照您的链接并阅读了强制设置步骤,才成功运行上述答案。glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - Michael Shaffer
那个链接已经失效。 - activedecay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接