OpenGL中的汉字显示

本文介绍了一个使用OpenGL在屏幕上绘制中文字符串的方法。通过创建字体并利用wglUseFontOutlines将字符转换为轮廓,再调用glCallList进行绘制。这种方法适用于需要在OpenGL环境中显示中文的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifvoidCOpenGLDemoView::glDrawString(unsignedchar*str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifGLYPHMETRICSFLOATpgmf[
1];
InBlock.gifHDChDC
=wglGetCurrentDC();
InBlock.gifHFONThFont;
InBlock.gifLOGFONTlf;
InBlock.gifmemset(
&lf,0,sizeof(LOGFONT));
InBlock.giflf.lfHeight
=1;
InBlock.giflf.lfWidth
=0;
InBlock.giflf.lfEscapement
=0;
InBlock.giflf.lfOrientation
=0;
InBlock.giflf.lfWeight
=FW_NORMAL;
InBlock.giflf.lfItalic
=FALSE;
InBlock.giflf.lfUnderline
=FALSE;
InBlock.giflf.lfStrikeOut
=FALSE;
InBlock.giflf.lfCharSet
=GB2312_CHARSET;
InBlock.giflf.lfOutPrecision
=OUT_TT_PRECIS;
InBlock.giflf.lfClipPrecision
=CLIP_DEFAULT_PRECIS;
InBlock.giflf.lfQuality
=PROOF_QUALITY;
InBlock.giflf.lfPitchAndFamily
=VARIABLE_PITCH|TMPF_TRUETYPE|FF_MODERN;
InBlock.giflstrcpy(lf.lfFaceName,
"宋体");
InBlock.gifhFont
=CreateFontIndirect(&lf);
InBlock.gif
//设置当前字体
InBlock.gif
SelectObject(wglGetCurrentDC(),hFont);
InBlock.gifDWORDdwChar;
InBlock.gif
intListNum;
InBlock.gif
for(size_ti=0;i<strlen((char*)str);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(IsDBCSLeadByte(str[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdwChar
=(DWORD)((str[i]<<8)|str[i+1]);
InBlock.gifi
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifdwChar
=str[i];
InBlock.gifListNum
=glGenLists(1);
InBlock.gifwglUseFontOutlines(hDC,dwChar,
1,ListNum,0.0,0.1f,WGL_FONT_POLYGONS,pgmf);
InBlock.gifglCallList(ListNum);
InBlock.gifglDeleteLists(ListNum,
1);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

None.gifintCOpenGLDemoView::DrawGLScene()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//Here'sWhereWeDoAllTheDrawing
InBlock.gif
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
InBlock.gif
glLoadIdentity();//ResetTheCurrentModelviewMatrix
InBlock.gif
glTranslatef(-2.5f,0.0f,-10.0f);//MoveOneUnitIntoTheScreen
InBlock.gif
unsignedcharstr[]="你好,中国";
InBlock.gif
this->glDrawString(str);
InBlock.gif
returnTRUE;//EverythingWentOK
InBlock.gif
//EverythingWentOK
ExpandedBlockEnd.gif
}

效果如图所示:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


200782501.jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值