Binary Mind
Binary Mind

Reputation: 329

Something wrong with converting SDL surface to GL texture

I can't find my mistake, why text has not been created? When using texture instead of text I get nothing or black background with colored points, please help

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

Upvotes: 3

Views: 711

Answers (2)

TheKK
TheKK

Reputation: 1

Few things you have to check first

  1. is the font loaded properly? check if "font == NULL", maybe your font path is wrong
  2. is the shader (if you use a shader) setup properly?

My guess is that you set the wrong pixel format type in glTexImage2D cause random color dots apear on your texture

Below is my code that load image via SDL_image for OpenGL use, I think it would be a good start to figure out what step you missed or forgot.

BTW, this code is not perfect. The types of pixel format is more than four (like index color) and I only handle some of them.

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

For more information, you should check out SDL wiki or deep into it's source code to fully understand the architecture of SDL_Surface.

Upvotes: 0

MaKo
MaKo

Reputation: 786

One thing you could add is to specify texture filters, e.g.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Upvotes: 2

Related Questions