将SDL表面转换为GL纹理出现问题

3

我找不到我的错误,为什么没有创建文本?当我使用纹理而不是文本时,我得到了空白或黑色背景与彩色的点,请帮忙。

GLuint texture;
SDL_Surface *text = NULL;
TTF_Font *font = NULL;
SDL_Color color = {0, 0, 0};


font = TTF_OpenFont("../test.ttf", 20);
text = TTF_RenderText_Solid(font, "Hello, SDL !!!", color);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text->w, text->h, 0, GL_RGB, GL_UNSIGNED_BYTE, text->pixels);

SDL_FreeSurface(text);

1
你为什么要声称在 glTexImage2D() 调用中,TTF_RenderText_Solid() 中的 text->pixels 包含 RGB 数据? - genpfault
2个回答

2
您可以添加的一件事是指定纹理过滤器,例如:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

0

首先需要检查以下几点:

  1. 字体是否已正确加载?检查一下 "font == NULL",也许你的字体路径有误。
  2. 着色器(如果使用了着色器)是否设置正确?

我猜测你在 glTexImage2D 中设置了错误的像素格式类型,导致纹理上出现随机颜色点。

以下是我用 SDL_image 加载图像供 OpenGL 使用的代码,我认为这是找出你遗漏或忘记的步骤的好起点。

顺便说一下,这段代码并不完美。像素格式的类型不止四种(如索引颜色),而我只处理了其中的一些。

/*
 * object_, originalWidth_ and originalHeight_ are private variables in
 * this class, don't panic.
 */
void
Texture::Load(string filePath, GLint minMagFilter,  GLint wrapMode)              
{
    SDL_Surface* image;                                                      
    GLenum textureFormat;                                                    
    GLint bpp;              //Byte Per Pixel                                 

    /* Load image file */                                                    
    image = IMG_Load(filePath.c_str());                                      
    if (image == nullptr) {                                                  
            string msg("IMG error: ");                                       
            msg += IMG_GetError();                                           
            throw runtime_error(msg.c_str());                                
    }                                                                        

    /* Find out pixel format type */                                         
    bpp = image->format->BytesPerPixel;                                      
    if (bpp == 4) {                                                          
            if (image->format->Rmask == 0x000000ff)                          
                    textureFormat = GL_RGBA;                                 
            else                                                             
                    textureFormat = GL_BGRA;                                 
    } else if (bpp == 3) {                                                   
            if (image->format->Rmask == 0x000000ff)                          
                    textureFormat = GL_RGB;                                  
            else                                                             
                    textureFormat = GL_BGR;                                  
    } else {                                                                 
            string msg("IMG error: Unknow pixel format, bpp = ");            
            msg += bpp;                                                      
            throw runtime_error(msg.c_str());                                
    }                                                                        

    /* Store widht and height */                                             
    originalWidth_ = image->w;                                               
    originalHeight_ = image->h;

    /* Make OpenGL texture */                                                
    glEnable(GL_TEXTURE_2D);                                                 
    glGenTextures(1, &object_);                                              
    glBindTexture(GL_TEXTURE_2D, object_);                                   
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minMagFilter);     
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, minMagFilter);     
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);             
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);             
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);             
    glTexImage2D(                                                            
            GL_TEXTURE_2D,                  // texture type                  
            0,                              // level                         
            bpp,                            // internal format               
            image->w,                       // width                         
            image->h,                       // height                        
            0,                              // border                        
            textureFormat,                  // format(in this texture?)      
            GL_UNSIGNED_BYTE,               // data type                     
            image->pixels                   // pointer to data               
            );                                                               

    /* Clean these mess up */                                                
    glBindTexture(GL_TEXTURE_2D, 0);                                         
    glDisable(GL_TEXTURE_2D);                                                
    SDL_FreeSurface(image);
}

如需更多信息,您可以查看SDL wiki,或深入研究其源代码以全面了解SDL_Surface的架构。


我尝试了你说的,发现我的字体表面的BPP为1。我不知道该怎么办 :-( - Binary Mind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接