Opengl 使用在纹理中转换的SDL_曲面显示文本

Opengl 使用在纹理中转换的SDL_曲面显示文本,opengl,textures,sdl,truetype,geometry-surface,Opengl,Textures,Sdl,Truetype,Geometry Surface,如上所述,我正在尝试使用在纹理中转换的SDL_曲面显示文本以下是我的代码: if (SDL_Init(SDL_INIT_VIDEO) == 0) { //Create a surface switch (method) { case 2: // load a texture with SDL_Image: { surface = IMG_Load("../../../../../data/box3.png"); } bre

如上所述,我正在尝试使用在纹理中转换的SDL_曲面显示文本以下是我的代码:

if (SDL_Init(SDL_INIT_VIDEO) == 0) {

    //Create a surface
    switch (method) {
    case 2: // load a texture with SDL_Image:
    {
        surface = IMG_Load("../../../../../data/box3.png");
    }
        break;
    case 3: // load a texture with SDL_TTF:

        SDL_Color textColor={ 255, 255, 0, 1 };
        if (TTF_Init() == 0){;
            TTF_Font *font;
            font = TTF_OpenFont("../../../../../data/Bedizen.ttf", 20);
            if (font != NULL){
                qDebug() << TTF_FontFaceFamilyName(font);
                surface = TTF_RenderText_Solid(font, ".....", textColor );
            }
            else
                qDebug() << "Error (Font) : " << TTF_GetError();
        }
        else
            qDebug() << "Error (Font) : " << TTF_GetError();

        break;
    }

    if (surface != NULL){
        GLint  nbOfColors;
        GLenum texture_format = 0;

        qDebug("surface : %dx%d / %dbpp / %d", surface->w, surface->h,
                       surface->format->BytesPerPixel, surface->pitch);

        MemoryDump(surface->pixels, surface->pitch, surface->h, surface->format->BytesPerPixel);

        // get the number of channels in the SDL surface
        nbOfColors = surface->format->BytesPerPixel;

        switch (nbOfColors) {
        case 1:
            texture_format = GL_ALPHA;
            break;
        case 3:     // no alpha channel
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGB;
            else
                texture_format = GL_BGR;
            break;
        case 4:     // contains an alpha channel
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGBA;
            else
                texture_format = GL_BGRA;
            break;
        default:
            qDebug() << "Warning: the image is not truecolor...";
            break;
        }

        glEnable( GL_TEXTURE_2D );
        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &texture );

        // Bind the texture object
        glBindTexture( GL_TEXTURE_2D, texture );

        // Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

        // Edit the texture object's image data using the information SDL_Surface gives us
        glTexImage2D( GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0,
                              texture_format, GL_UNSIGNED_BYTE, surface->pixels );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    }
    else{
        qDebug() << "Error (SDL) : " << SDL_GetError();
    }
}
if(SDL_Init(SDL_Init_VIDEO)==0){
//创建曲面
开关(方法){
案例2://使用SDL_图像加载纹理:
{
surface=IMG_Load(“../../../../../data/box3.png”);
}
打破
案例3://使用SDL_TTF加载纹理:
SDL_Color textColor={255,255,0,1};
如果(TTF_Init()==0){;
TTF_字体*字体;
font=TTF_OpenFont(“../../../../../data/Bedizen.TTF”,20);
如果(字体!=NULL){
qDebug()音高);
内存转储(表面->像素,表面->节距,表面->h,表面->格式->字节/像素);
//获取SDL曲面中的通道数
nbOfColors=表面->格式->字节/像素;
开关(nbOfColors){
案例1:
纹理_格式=GL_ALPHA;
打破
案例3://无阿尔法通道
如果(曲面->格式->Rmask==0x000000ff)
纹理_格式=GL_RGB;
其他的
纹理\u格式=GL\u BGR;
打破
案例4://包含一个alpha通道
如果(曲面->格式->Rmask==0x000000ff)
纹理_格式=GL_RGBA;
其他的
纹理\格式=GL \ U BGRA;
打破
违约:
qDebug()w,曲面->h,0,
纹理格式,GL\u无符号字节,表面->像素);
glTexParameteri(GL\u纹理\u 2D,GL\u纹理\u最小\u过滤器,GL\u最近);
glTexParameteri(GL_纹理2D,GL_纹理MAG_过滤器,GL_最近);
}
否则{

qDebug()好,我的渲染循环中缺少两行:

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

-1、过度缩进。谢谢你,这帮了我很大的忙。但你是对的,我编辑了我的帖子:)