Render TTF SDL2.0 opengl 3.1

回眸只為那壹抹淺笑 提交于 2019-12-07 13:59:58

问题


I'm working with SDL2.0, and using a (semi modern) opengl (3.1). I'm looking to add a text overlay to my application, and to render TTF in the application. How would I go about this using modern OpenGL?

EDIT: As per the suggestion of genpfault, I've tried using the SDL_TTF library, but All I'm getting is garbage on screen http://i.stack.imgur.com/FqyCT.png I've attached a gist of my shaders, which are very simple for this program, and also the snipped I'm using to load the text into surface, and to bind it to the texture. I'm not trying to do anything crazy here at all. Is there anything I'm doing wrong you can see? I'm not really too sure how to debug shaders etc.

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


回答1:


i spent too much time with a black screen before i figured out the actually text data was in the alpha channel.

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);
} 



回答2:


Use the pixels member of the SDL_Surface returned by a TTF_Render*() call to populate a texture.



来源:https://stackoverflow.com/questions/19686531/render-ttf-sdl2-0-opengl-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!