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


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/08/25/869417.html,如需转载请自行联系原作者