0
votes

I'm a beginner in OpenGL and I've created a function to write text in OpenGL using SDL_TTF. Here is the code for my function:

#define WINDOW_WIDTH 832
#define WINDOW_HEIGHT 487
#define glVertex2px(x,y) glVertex2d((double)(x) * 2.0 / (double)WINDOW_WIDTH - 1.0, 1.0 - (double)(y) * 2.0 / (double)WINDOW_HEIGHT);
void glWriteText2d(TTF_Font *font,const char *text,SDL_Color color,const int x,const int y){
    GLuint texture;
    SDL_Surface *surface;
    surface = TTF_RenderText_Blended(font,text,color);
    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D,texture);
    gluBuild2DMipmaps(GL_TEXTURE_2D,4,surface->w,surface->h,GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    GLboolean textureIsEnabled = glIsEnabled(GL_TEXTURE_2D);
    glEnable(GL_TEXTURE_2D);
    glColor3ub(255,255,255);
    glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex2px(x,y);
        glTexCoord2f(0,1);
        glVertex2px(x,y + surface->h);
        glTexCoord2f(1,1);
        glVertex2px(x + surface->w,y + surface->h);
        glTexCoord2f(1,0);
        glVertex2px(x + surface->w,y);
    glEnd();
    if(!textureIsEnabled){
        glDisable(GL_TEXTURE_2D);
    }
}

This function doesn't work because when I use it, it draws a rectangle which is the color of color at the place where the text should be. What should I change in my code so that it works?

1
Sorry, but I think you should first learn more about OpenGL before you try to make what are currently trying. First of all OpenGL has no build in text functions, also not modern OpenGL. Moreover you should not use this old OpenGL 1.1 with it's fixed function pipeline. It's definitely completely deprecated. And I never saw glVertex2px before. Also I can't find something about it. So finally it's hard to say what's wrong from this context without further information. Maybe you set up the Modelview/Projection matrix wrong so you can only see a small section of the whole? - Michael
@Michael About glVertex2px, it's a macro that I've made myself which draws things using the number of pixels from the top left corner of the window like in SDL. In 2D, I find that more practical. I forgot to include its definition in the code, but I edited to correct that. - Donald Duck

1 Answers

2
votes

Your resulting texture is filled with solid colour. What makes it shape the text is alpha channel, but you have no alpha blending enabled so you don't see it. Add

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

or at least alpha test (will produce sharper edges due to lack of blending - for some fonts it even look better that way).

Also note that you didn't freed neither generated SDL_Surface nor GL texture; that way you'll quickly run out of memory. Texture could be reused and SDL_Surface may be freed as soon as you copied data to GL texture.