用MFC在OpenGL中使用TrueType字体   本文给出了用于OpenGL中使用TrueType字体输出文字的类CGlGlyphList的定 义,该类的使用非常简单,只有一个ConvertString()接口。该接口可以把字符串 转换为OpenGL list,然后可以通过glCallLists()输出到窗口。所使用的字体是 通过ConvertString()的hdc参数输入的。 文章前面的部分是定义CGlGlyphList的头文件和实现它的CPP文件,后面给出 了一个应用该类输出字符串的简单例子。 GlGlyphList.h // GlGlyphList.h: interface for the CGlGlyphList class. // OpenGL字形表类接口 ////////////////////////////////////////////////////////////////////// #if !defined(AFX_GLGLYPHLIST_H_INCLUDED_) #define AFX_GLGLYPHLIST_H_INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CGlGlyphList //OpenGL字形表类 { //Constructor/Destructor: public: CGlGlyphList(); virtual ~CGlGlyphList(); //Attribute: public: int m_nNum; //表中的字形数 USHORT* m_pList; //字形表 //Operation: public: //将字符串串换为OpenGL字形表,存放到m_pList中 BOOL ConvertString(LPCTSTR szText, HDC hdc, float fExtru = 0.1f, float fDev = 0.0f, BOOL bUsePolygon=TRUE); }; #endif // !defined(AFX_GLGLYPHLIST_H_INCLUDED_) //GlGlyphList.h文件结束 ////////////////////////////////////////////////////////////////////// GlGlyphList.cpp // GlGlyphList.cpp: implementation of the CGlGlyphList class. // OpenGL字形表类实现 ////////////////////////////////////////////////////////////////////// #include "GlGlyphList.h" #include "gl/gl.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // 构造函数/析构函数 ////////////////////////////////////////////////////////////////////// CGlGlyphList::CGlGlyphList() { m_nNum = 0; m_pList= NULL; } CGlGlyphList::~CGlGlyphList() { if(m_pList) delete m_pList; } ///////////////////////////////////////////////////////////////////// // 函数名 : CGlGlyphList::ConvertString // 说 明 : 将字符串转换为OpenGL字形表,存放到m_pList中 // 返回值 : BOOL ,如果成功返回TRUE,否则返回FALSE // 参数表 : // LPCTSTR szText : 要串换的字符串 // HDC hdc : 转换字形时所使用的设备环境句柄,含有想使用的字体 // float fExtru : 字形的深度(z方向),缺省为0.1 // float fDev : 字形的精度(>=0),缺省为0.0 // BOOL bUsePolygon : 填充方式,TRUE=实心字形(缺省),FALSE=空心字形 BOOL CGlGlyphList::ConvertString( LPCTSTR szText, HDC hdc, float fExtru/*=0.1*/, float fDev/*=0.0*/, BOOL bUsePolygon/*=TRUE*/) { CString strText = szText; int nStrLen = strText.GetLength(); if(nStrLen == 0) return TRUE; USHORT* pList = new USHORT[nStrLen]; WORD nCharCode; int nFormat = bUsePolygon ? WGL_FONT_POLYGONS : WGL_FONT_LINES; for(int i=0; i<nStrLen; i++) { TCHAR c = strText[i]; #ifndef UNICODE //支持DBCS if(::IsDBCSLeadByte(c)) { //判断是否双字节字符的其实标志 nCharCode = MAKEWORD(strText[++i], c); } else #endif nCharCode = (WORD)c; int nListNo = glGenLists(1); //生成一个字符的字形: if(!wglUseFontOutlines(hdc, nCharCode, 1, nListNo, fDev, fExtru, nFormat, NULL) && m_nNum>0) { delete pList; m_nNum = 0; return FALSE; } pList[m_nNum] = nListNo; //将字形加入列表中 m_nNum ++; } m_pList = new USHORT[m_nNum]; memcpy(m_pList, pList, m_nNum*sizeof(USHORT)); delete pList; return TRUE; }//CGlGlyphList::ConvertString()定义结束 // GlGlyphList.cpp文件结束 ////////////////////////////////////////////////////////////////////// >应用举例: #include "gl/gl.h" #include "GlGlyphList.h" ... //初始化OpenGL及设备环境 ... CDC* pDC = GetDC(); CFont font; font.CreateFont(...,GB2312_CHARSET,..., "宋体"); CFont* pOldFont = pDC->SelectObject( &font ); ... //构造字形表对象: CString strText = _T("OpenGL真麻烦!"); CGlGlyphList glyphList; glyphList.ConvertString( strText, pDC->GetSafeHdc() ); pDC->SelectObject( pOldFont ); //OpenGL变换 ... //在OpenGL窗口中输出字形 glCallLists(glyphList.m_nNum, GL_UNSIGNED_SHORT, glyphList.m_pList);