创建一些基于3D图像的字体(nehe lesson 14)
基本流程如下:
- 申请创建显示列表,这里是创建256个;
- 定义自己的字体并选择字体对象;
- 调用函数wglUseFontOutlines .(
The wglUseFontOutlines function creates a set of display lists, one for each glyph of the currently selected outline font of a device context, for use with the current rendering context. The display lists are used to draw 3-D characters of TrueType fonts. Each display list describes a glyph outline in floating-point coordinates.
The run of glyphs begins with thefirstglyph of the font of the specified device context. The em square size of the font, the notional grid size of the original font outline from which the font is fitted, is mapped to 1.0 in the x- and y-coordinates in the display lists. The extrusion parameter sets how much depth the font has in the z direction.
hdc
Specifies the device context with the desired outline font. The outline font of hdc is used to create the display lists in the current rendering context.
first
Specifies the first of the set of glyphs that form the font outline display lists.
count
Specifies the number of glyphs in the set of glyphs used to form the font outline display lists. The wglUseFontOutlines function creates count display lists, one display list for each glyph in a set of glyphs.
listBase
Specifies a starting display list.
deviation
Specifies the maximum chordal deviation from the original outlines. When deviation is zero, the chordal deviation is equivalent to one design unit of the original font. The value of deviation must be equal to or greater than 0.
extrusion
Specifies how much a font is extruded in the negative z direction. The value must be equal to or greater than 0. When extrusion is 0, the display lists are not extruded.
format
Specifies the format, either WGL_FONT_LINES or WGL_FONT_POLYGONS, to use in the display lists. When format is WGL_FONT_LINES, thewglUseFontOutlines function creates fonts with line segments. When format is WGL_FONT_POLYGONS, wglUseFontOutlines creates fonts with polygons.
lpgmf
Points to an array of countGLYPHMETRICSFLOAT structures that is to receive the metrics of the glyphs. When lpgmf is NULL, no glyph metrics are returned.) - 轮廓文字可以跟其他图形一样,做一些位置,角度变化.
- 调用函数显示字体;
- 当显示结束时,释放显示列表资源;
部分代码如下:
建立轮廓文字
void CMyLibOpenGL::BuildOutlineFontList()
{
CFont font;
m_baseOutline = glGenLists(256);// 创建256个显示列表
BOOL suc = font.CreateFontW(-12,0//如果为0则会按照比率自动匹对
,0,0,FW_BOLD,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,FF_DONTCARE|DEFAULT_PITCH,TEXT("Comic Sans MS"));
if(!suc)
{
MessageBox(NULL,TEXT("创建字体失败"),TEXT("关闭错误"),MB_OK | MB_ICONINFORMATION);
}
CFont* oldfont = m_pDC->SelectObject(&font);
wglUseFontOutlines(m_pDC->GetSafeHdc()//设置当前窗口设备描述表的句柄
,0 //第一个ASCII码值
,255 //字符数
,m_baseOutline //第一个显示列表的名称
,0.0f // 字体的光滑度,越小越光滑,0.0为最光滑的状态
,0.2f // 在z方向突出的距离
,WGL_FONT_POLYGONS // 使用多边形来生成字符,每个顶点具有独立的法线
,gmf); //gmf是GLYPHMETRICSFLOAT结构,记录256个列表的位置和方向的信息
m_pDC->SelectObject(oldfont);
font.DeleteObject();//删除字体
}
显示
void CMyLibOpenGL::glPrintOutlineFont(const char* fmt, ...)
{
float length = 0.0;//字符串的长度
char text[256];//保存字符串
memset(text,0,sizeof(char)*256);
va_list ap;//变量列表指针
if( NULL == fmt)
{
return;
}
va_start(ap, fmt);//使ap指向可变变量的首位置
vsprintf(text, fmt, ap);//把参数写进text字符数组
va_end(ap);
TRACE(text);
for( int loop = 0; loop<(strlen(text)); loop++) //计算整个字符串的长度
{
length += gmf[text[loop]].gmfCellIncX;
}
TRACE("%f",length);
glTranslatef(-length/2, 0.0f, 0.0f);//为的是字符居中显示
glPushAttrib(GL_LIST_BIT); //把显示列表属性压入属性堆栈
glListBase(m_baseOutline); //把m_baseOutline设为基本偏移量,当调用glCallLists将会调用m_baseOutline + text[0...n]的显示列表;若不调用这句,text地址并不是显示列表地址,会出错
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
}
释放显示列表
void CMyLibOpenGL::KillOutlineFontList()
{
glDeleteLists(m_baseOutline,256);
}