Sign in to follow this  

OpenGL SDL_TTF with OpenGL together not working

Recommended Posts


Here´s my code. I use SDL_TTF and it worked before. Later on i added lighting and materials, and textures to my scene, and this started to be black for the first time.

But anyways, disabling or enabling them, even glColorMaterial, i get a black surface on the screen. What??. I tried to raise the effect of the ambient light ... maybe it had relation with materials, or something. But nothing happens.

Sorry for the explanations. They are in spanish. :(


void Vista::dibujarTexto(){

// obtengo los valores de GL_Viewport
int vPort[4];
glGetIntegerv(GL_VIEWPORT, vPort);

// guardo la matriz de proyeccion para ponerla a identidad
// y establezco la proyeccion a ortografica con los datos de GL_Viewport
glOrtho(0, vPort[2], 0, vPort[3], -1, 1);

// establezco a la identidad la matriz de modelado vista

// desactivo cosas que pueden afectar a la visibilidad

// Creo un color y una posicion
SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;

// (origen abajo a la izquierda).
// renderizo todos los trozos de texto
SDL_Rect position;
position.x = 10;
position.y = _altoVentana - 35;
renderTexto("RATON y CLICKS rotar y mover -- FLECHAS: deslizar -- NORMALES Y RELLENO: q,w -- PUERTA: g", _fuente, color, &position);
position.y -= position.h;
renderTexto("VISTAS: oblicua i, ortogonal o, perspectiva p. lateral 4, frontal 5, cenital 6, esquina 7.", _fuente, color, &position);
position.y -= position.h;
renderTexto("Luz global: l -- luz focal: k -- luz direccional: j -- niebla: h -- SALIR: Esc o Espacio", _fuente, color, &position);
position.y -= position.h;
renderTexto("Mover lampara: z x -- Escalar lampara: a s -- SALIR: Esc o Espacio", _fuente, color, &position);

// muestra los fps
char str[222];
sprintf(str, "FPS: %f", _fps);
position.y -= position.h;
renderTexto(str, _fuente, color, &position);

// enciendo todo como estaba



void Vista::renderTexto(std::string text,TTF_Font *font,SDL_Color color,
SDL_Rect *const location)
// uso SDL_TTF para renderizar el texto en una superficie
const char* text_C = text.c_str();
SDL_Surface *initial = TTF_RenderText_Blended(font, text_C, color);

// convierto el texto a un formato conocido, potencia de 2
int w = round(pow(2,ceil(log(initial->w) / log(2))));
int h = round(pow(2,ceil(log(initial->h) / log(2))));

// creo una superficie RGB intermediaria
SDL_Surface *intermediary = SDL_CreateRGBSurface(0, w, h, 32,
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);

// pego la inicial con el texto sobre la intermedia que es la base
SDL_BlitSurface(initial, 0, intermediary, 0);

// creo la textura
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
GL_UNSIGNED_BYTE, intermediary->pixels );

// dibujo los cuadrados

glTexCoord2f(0.0f, 1.0f);
glVertex2f(location->x , location->y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(location->x + w, location->y);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(location->x + w, location->y + h);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(location->x , location->y + h);


location->w = initial->w;
location->h = initial->h;

// libero los recursos.
glDeleteTextures(1, &texture);


Share this post

Link to post
Share on other sites
Two things:

You might need to call SDL_SetAlpha(initial, 0, 0) before you blit to the intermediary surface (so that everything is just copied over, not blended).

Are you sure your intermediary surface is BGRA (your masks look more like ARBG)?

Share this post

Link to post
Share on other sites
Hey! nice answers.

I tried the alpha thing. I´ll check if it works later. But tell me... what about the masks? you mean:

SDL_Surface *intermediary = SDL_CreateRGBSurface(0, w, h, 32,
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);

I copied the code so i don´t know about this.
Why those masks? are you sure it´s wrong?
ARBG = 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000

so it should be:

BGRA = 0x000000ff, 0xff000000 , 0x0000ff00, 0x00ff0000 ??

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this