Help using SDL_ttf to create an OpenGL texture

Hi There,

I’m trying to create an OpenGL texture using SDL_ttf, and am having some
troubles. I can manage to get SDL_ttf to create a surface with the
proper text, and can convert it to a corresponding surface with even
pixel widths and heights. I know that the text is rendering properly,
because I can do a SDL_SaveBMP on the surface. How should I run the
surface through SDL_ConvertSurface to make it OpenGL compatible, and
what corresponding glTexImage2D parameters should I use for the pixel data?

Here’s a snippit of what I’m trying to do. (yeah, the code’s ugly …)

SDL_Color black = { 0x00, 0x00, 0x00, 0 };
SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
TTF_Font *font;
SDL_Surface *text, *temp;

font = TTF_OpenFont(FONT_FILE, DEFAULT_PTSIZE);
TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
text = TTF_RenderText_Blended(font, DEFAULT_TEXT, white);
if (!text) exit(255);

TTF_CloseFont(font);

int newwidth, newheight;
if (text->w%2!=0)
	newwidth = text->w-1;
else
	newwidth = text->w;
if (text->h%2!=0)
	newheight = text->h-1;
else
	newwidth = text->h;

temp = 

SDL_CreateRGBSurfaceFrom(text->pixels,newwidth,newheight,text->format->BitsPerPixel,text->pitch,text->format->Rmask,text->format->Gmask,text->format->Bmask,text->format->Amask);
SDL_PixelFormat *fmt;
fmt = temp->format;
fmt->BitsPerPixel=32;
fmt->BytesPerPixel=4;
text = SDL_ConvertSurface(temp,fmt,0);
if (!text)
exit(254);
SDL_FreeSurface(temp);

SDL_SaveBMP( text, "test.bmp");

glBindTexture(GL_TEXTURE_2D, texture[1]);
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, GL_RGBA, text->w, text->h, 0, 

GL_RGBA, GL_UNSIGNED_BYTE, text->pixels);

<<<<<<<<<<<

Help!

Thanks,
Stephen Hassard

Stephen Hassard a ?crit :

I’m trying to create an OpenGL texture using SDL_ttf, and am
having some troubles.

I had the same troubles.

I know that the text is rendering properly,
because I can do a SDL_SaveBMP on the surface.

Then open the saved surface again and convert it to a GL compatible
bitmap using the following function. I don’t know why, but saving then
reloading the bmp works.

// Load Bitmaps And Convert To Textures
GLint GLTexture(SDL_Surface *surface, GLint textur)
{
Uint8 *pixels, *rowhi, *rowlo, *tmpbuf, tmpch;
int i, j, size, pitch, sh, sw;

if (!VideoModeGL) return(0);

sh = surface->h; sw = surface->w; size = sw * sh * 3;
pitch = surface->pitch;

/* GL surfaces are upsidedown and RGB, not BGR :slight_smile: */

pixels = (Uint8 *)malloc(size);
memcpy((unsigned char *)pixels,
(unsigned char *)surface->pixels, size);

tmpbuf = (Uint8 *)malloc(pitch);
if ( tmpbuf == NULL ) {
    fprintf(stderr, "Not enough memory!\n");
    return((GLint)NULL);
}
rowhi = pixels;
rowlo = rowhi + (sh * pitch) - pitch;
for ( i=0; i<(int)(sh/2); ++i ) {
    for ( j=0; j<sw; ++j ) {
        tmpch = rowhi[j*3];
        rowhi[j*3] = rowhi[j*3+2];
        rowhi[j*3+2] = tmpch;
        tmpch = rowlo[j*3];
        rowlo[j*3] = rowlo[j*3+2];
        rowlo[j*3+2] = tmpch;
    }
    memcpy(tmpbuf, rowhi, pitch);
    memcpy(rowhi, rowlo, pitch);
    memcpy(rowlo, tmpbuf, pitch);
    rowhi += pitch;
    rowlo -= pitch;
}

free(tmpbuf);

// Delete texture if it already exists
if (textur) glDeleteTextures(1, &textur);
// texture 0 (mipmapped scaling)
glGenTextures(1, &textur); // Create Texture
// 2d texture (x and y size)
glBindTexture(GL_TEXTURE_2D, textur);
// scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// scale linearly + mipmap when image smaller than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, sw, sh, 0, GL_RGB,
GL_UNSIGNED_BYTE, pixels);
// 2d texture, 3 colors, width, height, RGB in that order,
// byte data, and the data.
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sw, sh, GL_RGB,
GL_UNSIGNED_BYTE, pixels);

free(pixels);
return(textur);
}–
Marc Lavall?e

Marc Lavall?e wrote:

Then open the saved surface again and convert it to a GL compatible
bitmap using the following function. I don’t know why, but saving then
reloading the bmp works.

yikes! that’s really goofy. It’d be really cool if somebody would
address this so that we wouldn’t have to use such a hack.

Thanks for the info …

later,
Steve