Using SDL_ttf and OpenGL, TTF_RenderUTF8_Blended print Red rectangle

When I make my text with TTF_RenderUTF8_Blended, I get a solid rectangle on the screen. The color depends on which one I choose, in my case the rectangle is red.

Result using TTF_RenderUTF8_Blended

My question

What am I missing? It looks like I'm not getting the correct Alpha values ​​from the surface created with SDL_DisplayFormatAlpha(TTF_RenderUTF8_Blended( ... )), or am I? Does anyone know or know the problem?

Additional Information

If I use TTF_RenderUTF8_Solidor TTF_RenderUTF8_Shaded, the text is drawn correctly, but does not mix, of course.

I also draw other textures on the screen, so I draw the last text to ensure that the blend will respect the current surface.

Edit: SDL_Color g_textColor = {255, 0, 0, 0}; <- I tried with and without an alpha value, but I get the same result.

I tried to generalize the code without removing too many details. Variables prefixed with "g_" are global.

Init () function

// This function creates the required texture.
bool Init()
{
    // ...

    g_pFont = TTF_OpenFont("../arial.ttf", 12);
    if(g_pFont == NULL)
        return false;

    // Write text to surface
    g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor)); //< Doesn't work

    // Note that Solid and Shaded Does work properly if I uncomment them.
    //g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Solid(g_pFont, "My first Text!", g_textColor));
    //g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Shaded(g_pFont, "My first Text!", g_textColor, g_bgColor));

    if(g_pText == NULL)
        return false;

    // Prepare the texture for the font
    GLenum textFormat;
    if(g_pText->format->BytesPerPixel == 4)
    {
        // alpha
        if(g_pText->format->Rmask == 0x000000ff)
            textFormat = GL_RGBA;
        else
            textFormat = GL_BGRA_EXT;
    }

    // Create the font texture
    glGenTextures(1, &g_FontTextureId);
    glBindTexture(GL_TEXTURE_2D, g_FontTextureId);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, g_pText->format->BytesPerPixel, g_pText->w, g_pText->h, 0, textFormat, GL_UNSIGNED_BYTE, g_pText->pixels);

    // ...
}

DrawText () function

// this function is called each frame
void DrawText()
{
    SDL_Rect sourceRect;
    sourceRect.x = 0;
    sourceRect.y = 0;
    sourceRect.h = 10;
    sourceRect.w = 173;

    // DestRect is null so the rect is drawn at 0,0
    SDL_BlitSurface(g_pText, &sourceRect, g_pSurfaceDisplay, NULL);

    glBindTexture(GL_TEXTURE_2D, g_FontTextureId);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBegin( GL_QUADS );

        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(0.0f, 0.0f);

        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(0.0f, 10.0f);

        glTexCoord2f(1.0f, 1.0f);
        glVertex2f(173.0f, 10.0f);

        glTexCoord2f(1.0f, 0.0f);
        glVertex2f(173.0f, 0.0f);

    glEnd();
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
}
+4
source share
1 answer

You made a fairly common mistake. This is at the end of OpenGL.

When you execute a textured box in DrawText(), you enable the OpenGL blending feature, but you never specify the blending function (that is, how it should be mixed)!

You need this code to enable regular alpha blending in OpenGL:

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

This information used to be on the OpenGL website, but I cannot find it now.

. , , - , -, - , . , -, .

.

DrawText() blitting SDL OpenGL. SDL OpenGL; . :

SDL_BlitSurface(g_pText, &sourceRect, g_pSurfaceDisplay, NULL);

, :

g_pText = SDL_DisplayFormatAlpha( TTF_RenderUTF8_Blended(...) );

TTF_RenderUTF8_Blended() SDL_Surface, SDL_FreeSurface(). SDL_DisplayFormatAlpha(), , (, ).

, SDL_DisplayFormatAlpha, TTF_RenderUTF8_Blended 32- -! :

g_pText = TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor);
+4

All Articles