Chemistpp
Chemistpp

Reputation: 2046

openGL texture from SDL Surface

I pulled out my font system from a large project and fixed it up to work properly. Inserted it back into my main project and now the font is not rendering properly. I am left with white boxes instead of glyph textures rendering. Here is the code that pertains to this problem.

bool KFont::MapGlyph(char a) { 
    const char* T = &a;
    SDL_Surface* ThisGlyph;
    //Font is a struct that holds the font information Font->Font is the TTF pointer.
    if(!(ThisGlyph = TTF_RenderUTF8_Blended(Font->Font, T, GlyphColor))) {
        //std::cout << "!" << std::endl;
        return false;
    }

    KTexture* Temp;
    Temp = new KTexture;

    if(!(Temp->LoadImage(ThisGlyph))) {     
        //std::cout<< "!!" << std::endl;
        return false;
    }

    SDL_FreeSurface(ThisGlyph);
    Glyphs->Glyphs.insert( std::pair<char,KTexture*>(a,Temp) );
    return true;

}

The LoadImage function is:

bool KTexture::LoadImage(SDL_Surface* Surface) {

    glEnable(GL_TEXTURE_2D);
    if(TextureID > 0) {
        glDeleteTextures(1, &TextureID);
        TextureID = 0;
    }

    if(Surface == NULL) return false;

    GLenum Mode = 0;
    switch (Surface->format->BytesPerPixel) {
    case 1: {
        Mode = GL_ALPHA;
            break;
            }
    case 3: {
        Mode = GL_RGB;
        break;
            }
    case 4: {
        Mode = GL_RGBA;
        break;
            }
    default: { break;
             }
    }

    glGenTextures(1, &TextureID);

    if(TextureID & GL_INVALID_VALUE) exit(1);

    glBindTexture(GL_TEXTURE_2D, TextureID);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

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

    _Width = Surface->w;
    _Height = Surface->h;

    glDisable(GL_TEXTURE_2D);
    return true;

}

During rendering, the characters are right, the X/Y locations to render are correct, the Surface->w/h are all the same? The TextureID always remains 0 while in the working version it changes. There seems to be a problem with glBindTexture() but I have looked at the different codes numerous times and cannot figure out why this isn't rendering.

The rendering function if it helps

void KTexture::RenderQuad(int x, int y, int w, int h) {
    glPushMatrix();
    glEnable(GL_TEXTURE_2D);

    if(TextureID & GL_INVALID_VALUE) exit(1);

    glBindTexture(GL_TEXTURE_2D,TextureID);

    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

    glBegin(GL_QUADS);
        glTexCoord2f(0,0); glVertex3f(x,y,0);
        glTexCoord2f(1,0); glVertex3f(x+w,y,0);
        glTexCoord2f(1,1); glVertex3f(x+w,y+h,0);
        glTexCoord2f(0,1); glVertex3f(x,y+h,0);
    glEnd();

    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
    glFlush();
}

And these are my inits

if(SDL_Init(SDL_INIT_EVERYTHING) < 0 ) { 
        exit(0x1);
    }

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,           8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,         8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,          8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,         8);

    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,        16);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,       32);

    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,     8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,   8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,    8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,   8);

    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

    if((Surf_Display = SDL_SetVideoMode(winWidth,winHeight,32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER |SDL_OPENGL | SDL_RESIZABLE )) == NULL) {
        exit(2);
    }

    glClearColor(0, 0, 0, 0);
    glClearDepth(1.0f);

    glViewport(0, 0, winWidth, winHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, winWidth, winHeight, 0, 1, -1);

    glMatrixMode(GL_MODELVIEW);
    glEnable (GL_BLEND); 

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glLoadIdentity();

Any idea why my font will not render? I've tried changing the glTexEnvf mode to decal, modulate, and blend none of which have any effect. Just little white squares.

Upvotes: 1

Views: 1618

Answers (1)

genpfault
genpfault

Reputation: 52084

Make sure KTexture::LoadImage() being called with after SDL_SetVideoMode() so it has a current GL context.

Upvotes: 2

Related Questions