Will P
Will P

Reputation: 192

Render TTF SDL2.0 opengl 3.1

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 https://i.sstatic.net/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

Upvotes: 3

Views: 3072

Answers (2)

ColacX
ColacX

Reputation: 4032

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

Upvotes: 3

genpfault
genpfault

Reputation: 52094

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

Upvotes: 1

Related Questions