渲染TTF SDL2.0 opengl 3.1

时间:2013-10-30 15:09:41

标签: opengl fonts sdl sdl-2 opengl-3

我正在使用SDL2.0,并使用(半现代)opengl(3.1)。我想在我的应用程序中添加文本覆盖,并在应用程序中呈现TTF。我将如何使用现代OpenGL进行此操作?

编辑: 根据genpfault的建议,我尝试使用SDL_TTF库,但我得到的只是屏幕上的垃圾http://i.stack.imgur.com/FqyCT.png 我附上了我的着色器的要点,这对于这个程序非常简单,还有我正在使用的剪切将文本加载到表面,并将其绑定到纹理。我根本不想在这里做任何疯狂的事情。你能看到我做错了什么吗?我不太确定如何调试着色器等。

https://gist.github.com/anonymous/7284430

2 个答案:

答案 0 :(得分:3)

我花了太多时间在黑屏上才发现实际文本数据是在alpha通道中。

GLuint shader_program_text;

void drawText()
{
    //Render the message to an SDL_Surface, as that's what TTF_RenderText_X returns
    text_font = TTF_OpenFont("bpl_binary/waltographUI.ttf", 50);
    SDL_Color color = {255, 0, 0, 0};
    SDL_Surface* sdl_surface = TTF_RenderText_Blended(text_font, "hello world", color);
    GLuint texture_id;
    glGenTextures(1, &texture_id);
    glBindTexture(GL_TEXTURE_2D, texture_id);
    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, sdl_surface->w, sdl_surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, sdl_surface->pixels);
    glBindTexture(GL_TEXTURE_2D, 0);
    SDL_FreeSurface(sdl_surface);
    TTF_CloseFont(text_font);

    //glDrawPixels(sdl_surface->w, sdl_surface->h, GL_RGBA, GL_UNSIGNED_BYTE, sdl_surface->pixels);
    //FILE* file = fopen("output.txt", "w+");

    //for(int h=0; h<sdl_surface->h; h++)
    //{
    //  
    //  for(int w=0; w<sdl_surface->w; w++)
    //  {
    //      unsigned int xxx = ((unsigned int*)sdl_surface->pixels)[h*sdl_surface->w + w];
    //      /*if(xxx != 0)
    //          fprintf(file, "x", xxx);
    //      else
    //          fprintf(file, " ", xxx);*/
    //      fprintf(file, "%08x ", xxx);
    //  }
    //  fprintf(file, "\n");
    //}

    //fclose(file);

    //MULTIPLY WITH ALPHA TO ACTUALLY SEE SOMETHING

    glUseProgram(shader_program_text);
    glEnable(GL_TEXTURE_2D);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture_id);

    glBegin(GL_TRIANGLE_STRIP);
    //texcoord; position;
    glVertexAttrib2f(1, 0, 1); glVertexAttrib2f(0, -1, -1); //top left
    glVertexAttrib2f(1, 1, 1); glVertexAttrib2f(0, +1, -1); //top right
    glVertexAttrib2f(1, 0, 0); glVertexAttrib2f(0, -1, +1); //bottom left
    glVertexAttrib2f(1, 1, 0); glVertexAttrib2f(0, +1, +1); //bottom right
    glEnd();

    glDisable(GL_TEXTURE_2D);
    glUseProgram(0);
} 

答案 1 :(得分:1)

使用TTF_Render*()调用返回的the pixels member of the SDL_Surface来填充纹理。