天天看点

OpenGL显示中文-Unicode版本

void GLFont::Print3DTextW(wchar_t *string, float z)

{

 HDC hDC = wglGetCurrentDC();        

 HFONT hOldFont=(HFONT)::SelectObject(hDC,m_hFont);

 int   nListNum;                  

 DWORD dwChar;                    

 GLYPHMETRICSFLOAT pgmf[1];       

 glPushMatrix();

 glDisable(GL_LIGHTING);

 for(int i = 0; i <wcslen(string); i++)

 {  

  dwChar = string[i];    

  nListNum = glGenLists(1);  

  wglUseFontOutlinesW( hDC,             

   dwChar,         

   1,                

   nListNum,         

   0.0f,

   z,                

   WGL_FONT_POLYGONS,

   pgmf             

   );

  glCallList(nListNum);       

  glDeleteLists(nListNum, 1);   

 } 

 glEnable(GL_LIGHTING);  

 glPopMatrix();

 ::SelectObject(hDC,hOldFont);

}

其实Ansi版本的处理比Unicode的要复杂,因为需要考虑中文的多个字节,如下所示:

 for(int i = 0; i < strlen(string); i++)

 {

  if( IsDBCSLeadByte((BYTE)pChar[i]) )  

  {

   dwChar=(DWORD)((pChar[i]<<8)|pChar[i+1]); 

      i++;

  }

   else 

    dwChar = pChar[i];    

   nListNum = glGenLists(1);  

   wglUseFontOutlines( hDC,             

        dwChar,         

        1,                

        nListNum,         

        0.0f,

        z,                

        WGL_FONT_POLYGONS,

        pgmf             

      );

   glCallList(nListNum);       

   glDeleteLists(nListNum, 1);   

 } 

其实Ansi版本的处理反而更复杂一些,因为需要考虑中文的多字节问题,如下所示:

 for(int i = 0; i < strlen(string); i++)

 {

  if( IsDBCSLeadByte((BYTE)pChar[i]) )  

  {

   dwChar=(DWORD)((pChar[i]<<8)|pChar[i+1]); 

      i++;

  }

   else 

    dwChar = pChar[i];    

   nListNum = glGenLists(1);  

   wglUseFontOutlines( hDC,             

        dwChar,         

        1,                

        nListNum,         

        0.0f,

        z,                

        WGL_FONT_POLYGONS,

        pgmf             

      );

   glCallList(nListNum);       

   glDeleteLists(nListNum, 1);   

 } 

继续阅读