OpenGL中的汉字显示

void COpenGLDemoView::glDrawString(unsigned char *str)
{
    GLYPHMETRICSFLOAT pgmf[
1];
    HDC hDC
=wglGetCurrentDC();
    HFONT hFont;
    LOGFONT lf ;
    memset(
&lf,0,sizeof(LOGFONT));
    lf.lfHeight 
= 1 ;
    lf.lfWidth 
= 0 ;
    lf.lfEscapement 
= 0 ;
    lf.lfOrientation 
= 0 ;
    lf.lfWeight 
= FW_NORMAL ;
    lf.lfItalic 
= FALSE ;
    lf.lfUnderline 
= FALSE ;
    lf.lfStrikeOut 
= FALSE ;
    lf.lfCharSet 
= GB2312_CHARSET ;
    lf.lfOutPrecision 
= OUT_TT_PRECIS ;
    lf.lfClipPrecision
= CLIP_DEFAULT_PRECIS ;
    lf.lfQuality 
= PROOF_QUALITY ;
    lf.lfPitchAndFamily 
= VARIABLE_PITCH | TMPF_TRUETYPE | FF_MODERN ;
    lstrcpy (lf.lfFaceName, 
"宋体") ;
    hFont 
= CreateFontIndirect(&lf);
    
//设置当前字体
    SelectObject(wglGetCurrentDC(),hFont);
    DWORD dwChar;
    
int ListNum;
    
for(size_t i=0;i<strlen((char *)str);i++)
    
{
        
if(IsDBCSLeadByte(str[i]))
        
{
            dwChar
=(DWORD)((str[i]<<8)|str[i+1]);
            i
++;
        }

        
else
            dwChar
=str[i];
        ListNum
=glGenLists(1);
    wglUseFontOutlines(hDC,dwChar,
1,ListNum,0.0,0.1f,WGL_FONT_POLYGONS,pgmf);
        glCallList(ListNum);
        glDeleteLists(ListNum,
1);
    }

}


int COpenGLDemoView::DrawGLScene()                                   
{// Here's Where We Do All The Drawing
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
    glLoadIdentity();                                    // Reset The Current Modelview Matrix
    glTranslatef(-2.5f,0.0f,-10.0f);                        // Move One Unit Into The Screen
    unsigned char str[] = "你好,中国";
    
this->glDrawString(str);
    
return TRUE;                                        // Everything Went OK
                                      
// Everything Went OK
}

效果如图所示:


200782501.jpg

posted on   Phinecos(洞庭散人)  阅读(4586)  评论(1编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2006-08-25 操作系统复习笔记(二)
2006-08-25 操作系统复习笔记(一)

导航

统计

点击右上角即可分享
微信分享提示