【问题标题】:Using SDL_ttf with OpenGL在 OpenGL 中使用 SDL_ttf
【发布时间】:2011-07-14 10:49:48
【问题描述】:

我正在使用 OpenGL 和 SDL 在我的程序中创建一个窗口。

如何在 OpenGL 窗口中使用 SDL_ttf?

例如,我想加载一种字体并渲染一些文本。我想使用 SDL OpenGL 表面绘制文本。

【问题讨论】:

    标签: opengl sdl sdl-ttf


    【解决方案1】:

    这是怎么做的:

    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);
      
    4. 然后你可以使用glBindTexture()等在OpenGL中使用纹理。请确保在绘制完成后调用SDL_GL_SwapBuffers()

    【讨论】:

    • 很棒的帖子,我刚用过。我注意到 GL_BGRA 和 GL_BGR 不再使用,您必须为两者都添加一个额外的 _EXT。 GL_BGRA_EXT 和 GL_BGR_EXT。不管怎么说,多谢拉! :)
    • format 变量的用途是什么?我没有看到它在调用glTexImage2D 的任何地方使用。
    • 很好,它应该是纹理格式。现已修复。
    • 我完全按照你说的做了,但我无法让它工作。它给了我一个白色矩形而不是文本。
    • 嗨@DonaldDuck,我猜你在stackoverflow.com/questions/38750624/…得到了答案
    【解决方案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;
    }
    

    【讨论】:

    • 此解决方案将让代码不断地从磁盘重新加载 .ttf 文件在开发人员每次想要调用 RenderText() 时重新生成字形纹理。我无法想象这将是最有效的。
    • 在我点击您的链接并阅读强制设置步骤之前无法获得上述答案。glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    • 那个链接坏了
    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多