title: OpenGL, SDL_ttf
body: <p>I have trouble with display text in OpenGL through SDL_ttf. I have written LoadFont() function which loads font and makes OpenGL texture.</p>
  
  <pre><code>int LoadFont( char *name, GLubyte r, GLubyte g, GLubyte b, char *text, int ptsize)
  {
  SDL_Color color = {r, g, b, 0};
  TTF_Font *font = TTF_OpenFont(name, ptsize);
  
  Uint32 rmask, gmask, bmask, amask;
  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
      rmask = 0xff000000;
      gmask = 0x00ff0000;
      bmask = 0x0000ff00;
      amask = 0x000000ff;
  #else
      rmask = 0x000000ff;
      gmask = 0x0000ff00;
      bmask = 0x00ff0000;
      amask = 0xff000000;
  #endif
  
  SDL_Surface *msg = TTF_RenderText_Blended(font, text, color);
  
  SDL_SetAlpha(msg, 0, 0);
  
  if(!msg) {
      printf("Unable to Render text\n");
      return -1;
  }
  
  SDL_Surface *tmp = SDL_CreateRGBSurface(0, msg-&gt;w, msg-&gt;h, 32, rmask, gmask, bmask, amask);
  
  SDL_BlitSurface(msg, NULL, tmp, NULL);
  
  
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  
  glGenTextures(1, &amp;texture[0]);
  glBindTexture(GL_TEXTURE_2D, texture[0]);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, msg-&gt;w, msg-&gt;h, 0, GL_RGBA,
               GL_UNSIGNED_BYTE, msg-&gt;pixels);
  
  
  return 1;
  }
  </code></pre>
  
  <p>And i use this function in initGL():</p>
  
  <pre><code>int initGL( GLvoid )
  {
  glViewport(0, 0, SCREEN_WIDHT, SCREEN_HEIGHT);
  LoadFont("THWACK.TTF", 255, 255, 255, "Hello!", 14);
  glEnable(GL_TEXTURE_2D);
  glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
  glClearDepth(1.0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  
  gluPerspective(45.0f, (GLfloat)SCREEN_WIDHT/(GLfloat)SCREEN_HEIGHT, 0.1f, 100.0f);
  
  glMatrixMode(GL_MODELVIEW);
  
  return TRUE;
  }
  </code></pre>
  
  <p>Further i try display this text on polygon:</p>
  
  <pre><code>int drawGLScene( GLvoid )
  {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  
  glTranslatef(0.0f, 0.0f, -5.0f);
  
  glBindTexture(GL_TEXTURE_2D, texture[0]);
  
  glEnable(GL_TEXTURE_2D);
  glBegin(GL_POLYGON);
      glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
      glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
      glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
      glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
  glEnd();
  glDisable(GL_TEXTURE_2D);
  
  SDL_GL_SwapBuffers();
  
  return TRUE;
  }
  </code></pre>
  
  <p>But text doesnt display, only white polygon.</p>