Render TTF SDL2.0 opengl 3.1

回眸只為那壹抹淺笑 提交于 2019-12-06 03:31:04

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

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

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