您的位置:首页 > 运维架构

OpenGL显示中文-Unicode版本

2014-02-24 15:04 411 查看
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);   

 } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: