Render OpenGL text

Started by
2 comments, last by Aztral 14 years, 5 months ago
Hi, I am using a bunch of code I found in a different thread on these forums (that I seem to have lost track of) attempting to render text with openGL and SDL. I've been staring at it for who knows how long and I can't figure out what I'm missing. Nothing at all is drawn. Thanks very much for any pointers. The code to load a font.

void openFont()
{
  string fontPath = "Fonts/" + m_sFontName + ".ttf";
  m_font = TTF_OpenFont(fontPath.c_str(), m_nPtSize);

  if(!m_font)
    {
      cerr<<"Could not open font '"<<m_sFontName<<"': "<<TTF_GetError()<<endl;
      // Exit? Log?
    }
}//END openFont


The code to load the texture.

GLuint loadTexture(SDL_Surface* surface)
{
  GLuint texture = NULL;
  if(surface)
    {
      glGenTextures(1, &texture);
      glBindTexture(GL_TEXTURE_2D, texture);
      
      glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0,
		   GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
      
      // Again using linear filtered textures for now
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      
      SDL_FreeSurface(surface);
    }
  else // Handle null surface* as param
    cerr<<"The surface pointer passed into loadTexture is null.\n";
  
  return texture;
  
}//END loadTexture (SDL_Surface*)


The code to render the text.

void render()
{

  if(m_txtImg)
    SDL_FreeSurface(m_txtImg);


  SDL_Surface* initialTxtImg = TTF_RenderText_Blended(m_font, m_sText.c_str(), m_fg);
  
  if(!initialTxtImg)
    {
      cerr<<"Could not render: "<<m_sText<<" because: "<<TTF_GetError()<<endl;
    }
  

  m_nWidth = nextPowerOfTwo(initialTxtImg->w);
  m_nHeight = nextPowerOfTwo(initialTxtImg->h);
  
 
  m_txtImg = SDL_CreateRGBSurface(0, m_nWidth, m_nHeight, 32, 
						    0x00ff0000, 0x0000ff00,
						    0x000000ff, 0xff000000);
  
  if(!m_txtImg)
    {
      cerr<<"Could not render intermediate text image: "<<TTF_GetError()<<endl;

    }

  SDL_BlitSurface(initialTxtImg, 0, m_txtImg, 0);
  
  m_nTexture = loadTexture(m_txtImg);
}//END render


The code to draw the rendered image.

void draw()
{
  glBindTexture(GL_TEXTURE_2D, m_nTexture);

  glBegin(GL_QUADS);

  glNormal3f(0.0f, 0.0f, 1.0f);// Normal facing us

  // Loop through verts.
  for(int i = 0; i < 4; i++)
    {
      glTexCoord2f(m_texCoords.x, m_texCoords.y);
      glVertex3f(m_verts.x, m_verts.y, m_verts.z);
    }
  
  glEnd();
}//END draw


The loop actually calling the draw function.

void execGameLoop()
{
  SDL_Event e;

  Sprite glass(75, 75, 200, 300, "Graphics/Glass.bmp");
  
  SDL_Color fg = {150, 150, 150};

  DrawableString testString("Hello", "AbductionIII", 12, fg, 500, 500);

  while(!m_done)
    {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();

      glass.draw();
      testString.draw();

      SDL_PollEvent(&e);
            
      handleEvents(e);

      SDL_GL_SwapBuffers();
    }
}//END drawScene


Advertisement
Little off topic but I would probably work a little on cleaning up some of the names in your code. For example, you've got a function called draw() and another called render(). What's the difference?

On to your dilemma. I haven't used SDL_ttf too extensively so I can't be of too much aid on why the text isn't drawing, but I can say that from the looks of it, it's probably a setting. Are you rendering to the correct, visible coordinates? Does m_fg contain a 4 floating point values and is the alpha component at least 0.1? Little things like that are usually a good place to start. Another thing I noticed is that you don't have any calls to glEnable(GL_TEXTURE_2D). Without that you won't render whatever texture you have loaded in your loadTexture function.
Thanks for the response - I can see how that would be confusing and I'll clean it up.

I am quite sure I'm rendering to the proper coordinates because I can render a regular texture based off of a bmp or png just fine to the same location.

m_fg is currently just {0xFF, 0xFF, 0xFF} which should be white text on a black background. I don't give it an alpha value.

I make the call to glEnable(GL_TEXTURE_2D) much earlier and like I said I can render normal textures based on bmp's or png's without issue so I don't think thats the problem

Hmm

Thanks again for the response.
Still unable to figure this out - any other ideas?

This topic is closed to new replies.

Advertisement