OpenGl下可以方便的操作字符,但是對字符串顯示的支持不夠.OpenGL本身不提供文字的顯示和處理能力,如要顯示文字的借助于第三方的擴展程序。初涉OpenGl, 想請教有什么好的方法可以解決這個問題?網上覓得bluebohe三年前的一篇文章, 在vs2005下實現他的代碼, 編譯通過了, 但是結果顯示不出來. 可否幫忙看看? COpenGLText源碼
bluebohe的原文:
一直以來,OpenGL狀態下的文字顯示都是一個問題,本文使用嵌套顯示列表的方式進行OpenGL狀態下的字符串的顯示。
有以下幾點需要注意:
1:本程序顯示按照給定的高度和文字的位置顯示某一種字體的文字,其中一些字體的參數定義在OpenGLText內部給定,可以隨意將它抽出來。如果將代碼用于實際應用中,建議文字的文字間距自己設定,也可以隨意添加文字的傾斜角、旋轉角度、定位方式等等參數。
2:wglUseFontOutlines函數的第五個參數表示文字的精度(弦偏差),一般設置成零,但這樣缺省的做法會導致文字太粗糙,可以將它設置成一個比較小的數,以提高文字顯示的精確度,但這樣會增加內存的占用量。
3:如果文字的高度比較小,文字的線條可能會出現斷裂的現象,影響美觀,解決這類問題有以下幾種方式:(1)使用OpenGL反走樣技術;(2)把文字的輪廓用線條勾勒出來;(3)在文字的Draw成員函數中多次調用glCallList函數,每次調用向周圍平移一個像素,這樣的話會使文字的線條斷裂問題大為改觀,速度也是三種方式中最快的。
應用方式如下:
COpenGLText text;
COpenGLText text2;
text2.m_dX=0;
text2.m_dY=200;
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
text.Draw("宋體");
text2.Draw("楷體_GB2312");
glFlush();
類的聲明和實現如下:
1
#include <string>
2
#include <GL/glut.h>
3
4
using namespace std;
5
6
class COpenGLText
7

{
8
public:
9
10
//構造文字
11
COpenGLText();
12
virtual ~COpenGLText();
13
14
//繪制制定字體的文字,字體只在第一次繪制時進行設置,之后可以傳入空值
15
void Draw(char *strFontName);
16
17
//釋放文字所占空間
18
void Free();
19
//文字字符串
20
string m_str;
21
//字符串高度
22
double m_dHeight;
23
//字符串位置
24
double m_dX;
25
double m_dY;
26
27
protected:
28
BOOL GenList();
29
BOOL GenCharsLists(char *strFontName);
30
int m_iDisplayList;
31
};
32
33
/**/////////////////////////////////////////////////////////////////////// 34
35
// COpenGLText Class
36
37
/**///////////////////////////////////////////////////////////////////////
38
39
////////////////////////////////////////////////////////////////////// 40
41
// Construction/Destruction
42
43
/**/////////////////////////////////////////////////////////////////////// 44
45
COpenGLText::COpenGLText()
46

{
47
m_dX=0;
48
m_dY=0;
49
m_str="abc中國";
50
m_dHeight=100;
51
m_iDisplayList=0;
52
}
53
54
COpenGLText::~COpenGLText()
55

{
56
Free();
57
}
58
59
BOOL COpenGLText::GenCharsLists(char *strFontName)
60

{
61
HDC hdc;
62
const char *str=m_str.c_str()
63
hdc=CreateDC( "DISPLAY", "", "", NULL );
64
int iNum=_mbslen((const unsigned char *)str);
65
66
m_iDisplayList=glGenLists(iNum+1);
67
HFONT hNewCFont;
68
LOGFONT CLogFont; //存儲當前字體參數
69
70
//初始化字體參數
71
72
memset( &CLogFont, 0, sizeof(LOGFONT) );
73
CLogFont.lfEscapement = CLogFont.lfOrientation = 0;
74
CLogFont.lfWeight = FW_NORMAL;
75
CLogFont.lfCharSet = GB2312_CHARSET;
76
CLogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
77
CLogFont.lfQuality=DEFAULT_QUALITY;
78
CLogFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
79
strcpy( CLogFont.lfFaceName, LPCTSTR(strFontName) );
80
CLogFont.lfHeight=-10;
81
CLogFont.lfWidth=0;
82
83
hNewCFont=CreateFontIndirect(&CLogFont);
84
HFONT hOldFont=(HFONT)SelectObject(hdc,hNewCFont);
85
86
int i=0,j=0,iTotal=strlen(str);;
87
UINT nChar=0;
88
89
while(i<iTotal)
90
{
91
j++;
92
93
if(IsDBCSLeadByte(str[i]))
94
{
95
nChar=((unsigned char)str[i])*0x100+(unsigned char)str[i+1];
96
i+=2;
97
}
98
else
99
{
100
nChar=str[i];
101
i++;
102
103
}
104
GLYPHMETRICSFLOAT agmf[1];
105
BOOL bOK=wglUseFontOutlines(hdc,nChar,1,m_iDisplayList+j,0.002f,0,WGL_FONT_POLYGONS,agmf);
106
107
}
108
109
SelectObject(hdc,hOldFont);
110
DeleteObject(hNewCFont);
111
DeleteDC(hdc);
112
113
return TRUE;
114
115
}
116
117
BOOL COpenGLText::GenList()
118

{
119
int iNum=_mbslen((const unsigned char *)m_str.c_str());
120
glNewList( m_iDisplayList, GL_COMPILE);
121
122
for(int i=1;i<=iNum;i++)
123
{
124
125
glPushMatrix();
126
127
//此處修改文字間距
128
129
glTranslated(m_dX+m_dHeight*(i-1),m_dY,0);
130
131
glScaled(m_dHeight,m_dHeight,1);
132
133
glCallList(m_iDisplayList+i);
134
135
glPopMatrix();
136
137
}
138
glEndList();
139
140
return TRUE;
141
}
142
143
void COpenGLText::Free()
144

{
145
if(glIsList(m_iDisplayList))
146
glDeleteLists(m_iDisplayList,_mbslen((const unsigned char *)m_str.c_str())+1);
147
m_iDisplayList=0;
148
}
149
150
void COpenGLText::Draw(char *strFontName)
151

{
152
if(!glIsList(m_iDisplayList))
153
{
154
GenCharsLists(strFontName);
155
GenList();
156
}
157
glCallList(m_iDisplayList);
158
}
159
posted on 2007-04-12 15:26
哈哈 閱讀(3755)
評論(2) 編輯 收藏 引用