TEXT 66
Untitled By Guest on 22nd May 2017 02:22:09 AM
  1. void Engine::GL3Render::DrawTextf(Font *Font, float x, float y, const char *fmt, ...)  //thanks to http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/ for an amazing method.
  2. {
  3.          
  4.     // We Want A Coordinate System Where Distance Is Measured In Window Pixels.
  5.     pushScreenCoordinateMatrix();                                  
  6.          
  7.        
  8.  
  9.     GLuint font=Font->GlyphList;
  10.     // We Make The Height A Little Bigger.  There Will Be Some Space Between Lines.
  11.         float h=Font->FontSize/.63f;                                                
  12.     char    text[256];                                  // Holds Our String
  13.     va_list ap;                                     // Pointer To List Of Arguments
  14.  
  15.     if (fmt == NULL)                                    // If There's No Text
  16.         *text=0;                                    // Do Nothing
  17.     else
  18.         {
  19.         va_start(ap, fmt);                              // Parses The String For Variables
  20.         vsprintf(text, fmt, ap);                            // And Converts Symbols To Actual Numbers
  21.         va_end(ap);                                 // Results Are Stored In Text
  22.     }
  23.  
  24.     // Here Is Some Code To Split The Text That We Have Been
  25.     // Given Into A Set Of Lines.
  26.     // This Could Be Made Much Neater By Using
  27.     // A Regular Expression Library Such As The One Available From
  28.     // boost.org (I've Only Done It Out By Hand To Avoid Complicating
  29.     // This Tutorial With Unnecessary Library Dependencies).
  30.     const char *start_line=text;
  31.     std::vector<std::string> lines;
  32.         const char *c;
  33.     for(c=text;*c;c++)
  34.         {
  35.         if(*c=='\n')
  36.                 {
  37.             std::string line;
  38.             for(const char *n=start_line;n<c;n++)
  39.                                 line.append(1,*n);
  40.             lines.push_back(line);
  41.             start_line=c+1;
  42.         }
  43.     }
  44.     if(start_line)
  45.         {
  46.         std::string line;
  47.         for(const char *n=start_line;n<c;n++)
  48.                         line.append(1,*n);
  49.         lines.push_back(line);
  50.     }
  51.  
  52.     glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT  | GL_ENABLE_BIT | GL_TRANSFORM_BIT);
  53.     glMatrixMode(GL_MODELVIEW);
  54.     glDisable(GL_LIGHTING);
  55.     glEnable(GL_TEXTURE_2D);
  56.     glDisable(GL_DEPTH_TEST);
  57.     glEnable(GL_BLEND);
  58.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    
  59.  
  60.     glListBase(font);
  61.  
  62.  
  63.         float modelview_matrix[16];    
  64.     glGetFloatv(GL_MODELVIEW_MATRIX, modelview_matrix);
  65.  
  66.     // This Is Where The Text Display Actually Happens.
  67.     // For Each Line Of Text We Reset The Modelview Matrix
  68.     // So That The Line's Text Will Start In The Correct Position.
  69.     // Notice That We Need To Reset The Matrix, Rather Than Just Translating
  70.     // Down By h. This Is Because When Each Character Is
  71.     // Drawn It Modifies The Current Matrix So That The Next Character
  72.     // Will Be Drawn Immediately After It.
  73.  
  74.         glColor3ub(rand() % 255,rand() % 255,rand() % 255);
  75.  
  76.     for(unsigned int i=0;i<lines.size();i++)
  77.         {
  78.         glPushMatrix();
  79.         glLoadIdentity();
  80.         glTranslatef(x,y-h*i,0);
  81.         glMultMatrixf(modelview_matrix);
  82.  
  83.     // The Commented Out Raster Position Stuff Can Be Useful If You Need To
  84.     // Know The Length Of The Text That You Are Creating.
  85.     // If You Decide To Use It Make Sure To Also Uncomment The glBitmap Command
  86.     // In make_dlist().
  87.         glRasterPos2f(0,0);
  88.         glCallLists(lines[i].length(), GL_UNSIGNED_BYTE, lines[i].c_str());
  89.         float rpos[4];
  90.         glGetFloatv(GL_CURRENT_RASTER_POSITION ,rpos);
  91.         float len=x-rpos[0];
  92.         glPopMatrix();
  93.     }
  94.  
  95.     glPopAttrib();        
  96.  
  97.     pop_projection_matrix();
  98. }

Paste is for source code and general debugging text.

Login or Register to delete or keep track of your pastes.