2014-01-14 37 views
1

的最頂部邊框,用於繪製文本我正在使用freetype。我已經成功地渲染了文本,但是我仍然遇到了這樣的問題:字符不能指向正確的y座標(這些字符不是平坦的)。OpenGL freetype文字,y偏移到OpenGL中字符

看下面的圖片,我已經做了'正在加載...'文本。

enter image description here

,你可以看到y座標的位置是不正確的。它讓我感到困惑。 這個代碼我做了什麼:

class FontBitmap{ 
    private: 
    struct character_info { 
     float bw; // bitmap.width; 
     float bh; // bitmap.rows; 
    } c[256]; 
    int next_p2 (int a) 
    { 
     int rval=1; 
     while(rval<a) rval<<=1; 
     return rval; 
    } 
     GLuint texture[256]; //character textures 
    public: 
     FT_Library library; /* handle to library  */ 
     FT_Face  face;  /* handle to face object */ 
     FontBitmap(){} 

     void Print(char * str,GLfloat x,GLfloat y){ //the function where to render the text 
      glEnable(GL_TEXTURE_2D); 
      glEnable(GL_BLEND); 
      glPushMatrix(); 
      glTranslatef(x,y,0.0f); 
      for(int i = 0;i < strlen(str);i++) 
      {  
       glPushMatrix(); 
       glBindTexture(GL_TEXTURE_2D,texture[str[i]]); 
       glTranslatef(x,0,0); //the placement of per character 
       glBegin(GL_QUADS); 
       glTexCoord2f(0,0); glVertex3f(0,0,0.0f); 
       glTexCoord2f(1,0);glVertex3f(c[str[i]].bw,0,0.0f); 
       glTexCoord2f(1,1); glVertex3f(c[str[i]].bw, c[str[i]].bh ,0.0f); 
       glTexCoord2f(0,1); glVertex3f(0, c[str[i]].bh,0.0f);    
       glEnd(); 
       x +=c[str[i]].bw; 
       glPopMatrix(); 
      } 
      glPopMatrix(); 
      glDisable(GL_TEXTURE_2D); 
      glDisable(GL_BLEND); 
     } 

     FontBitmap(char * filePath, int size) //the constructor 
     { 
      glEnable(GL_TEXTURE_2D);  

      if (FT_Init_FreeType (&library)) 
      { 
       //on error 
       return; 
      } 
     if (FT_New_Face(library,filePath,0,&face)) 
     { 
      //on error 
      return; 
     } 
     FT_Set_Pixel_Sizes (face, 0, size); 
     for (unsigned long i = 33; i < 256; i++) 
     { 
      if(FT_Load_Char(face, i, FT_LOAD_RENDER)) 
      { 
       //on error; 
       return; 
      } 
      int width = next_p2(face->glyph->bitmap.width); 
      int height = next_p2(face->glyph->bitmap.rows); 

      c[i].bw = width; 
      c[i].bh = height; 

      GLubyte* expanded_data = new GLubyte[ 2 * width * height]; 

      for(int j=0; j <height;j++) { 
        for(int i=0; i < width; i++){ 
          expanded_data[2*(i+j*width)]= expanded_data[2*(i+j*width)+1] = 
            (i>=face->glyph->bitmap.width || j>=face->glyph->bitmap.rows) ? 
            0 : face->glyph->bitmap.buffer[i + face->glyph->bitmap.width*j]; 
        } 
      } 
      glGenTextures(1,&texture[i]); 
      glBindTexture(GL_TEXTURE_2D, texture[i]); 
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
     glPixelStorei(GL_UNPACK_ALIGNMENT,1); 
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 
         0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data); 
      delete [] expanded_data; 
     } 

     } 

    }; 
+2

你可能想看看FreeType的[字形指標(http://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html )如何正確呈現角色。 – yngccc

+0

我環顧了這個鏈接,並得到了設置從原點到角色距離的處理程序。它承載着Y.但是當我嘗試時,它總是會給予巨大的價值? – Ricks

+1

你可能要除以64,原因是在同一頁上。 – yngccc

回答

0

上升和下降是在問候你的問題你感興趣的兩個FreeType的指標。人物之間

水平間距控制與推進和Kerning