opengl创建位图文字

创建一些基于2D图像的字体(nehe lesson 13)

基本流程如下:

  1. 申请创建显示列表,这里是创建96个,因为ASCII码又96个可以显示,以空格(32)为开始,每一个ASCII码对应一个显示列表;
  2. 定义自己的字体并选择字体对象;
  3. 调用函数wglUseFontBitmaps.此函数意思是根据选择的字体创建一组位图并加入到显示列表中(

    The wglUseFontBitmaps function creates a set of bitmap display lists for use in the current OpenGL rendering context. The set of bitmap display lists is based on the glyphs in the currently selected font in the device context. You can then use bitmaps to draw characters in an OpenGL image.

    The wglUseFontBitmaps function creates count display lists, one for each of a run of count glyphs that begins with the first glyph in the hdc parameter's selected fonts.

    hdc

    Specifies the device context whose currently selected font will be used to form the glyph bitmap display lists in the current OpenGL rendering context.

    first

    Specifies the first glyph in the run of glyphs that will be used to form glyph bitmap display lists.

    count

    Specifies the number of glyphs in the run of glyphs that will be used to form glyph bitmap display lists. The function creates count display lists, one for each glyph in the run.

    listBase

    Specifies a starting display list.

    );
  4. 确定字体显示的位置.调用opengl函数glRasterPos2f. glRasterPos( specify the raster(光栅) position for pixel operations). 函数glRasterPos的参数坐标是设置当前的像素位置,它以窗口坐标为单位,OpenGL窗口坐标是以屏幕客户去的左下角为原点的坐标系。三维的glRasterPos虽然也可用,但是最终变换为屏幕坐标即二维的

    当前光栅位置:当前光栅位置是下一个位图(或图象)绘制的原点。

    光栅位置的坐标变换为屏幕坐标的过程与要获得当前光栅位置,你可以用GL_CURRENT_RASTER_POSITION作为参数调用查询命令glGetFloatv()。第二个参数必须是一个指向内存阵列的指针,这个阵列要足以容纳(x,y,z,w)四个浮点值。以GL_CURRENT_RASTER_POSITION_VALID作为第一个参数调用glGetBooleanv()决定当前光栅位置是否有效。

    yzw说明了光栅位置的坐标。。如果glRasterPos2*()被使用,z被简单的设为0并且w总是被设为1;类似的,对glRasterPos3*()w被设为1。glVertex*()命令所提供的坐标的变换过程完全一样(即,经模型视见和透视变换矩阵)。变换之后,它们或者定义一个屏幕上窗口中一个有效的位置,或者因为变换后位于视区之外而被裁剪掉。如变换后的点被裁剪掉则当前光栅位置无效。(摘录)http://www.opengl.org/sdk/docs/man/xhtml/glRasterPos.xml
  5. 调用函数显示字体;
  6. 当显示结束时,释放显示列表资源;
部分代码如下:
void CMyLibOpenGL::BuildFontList(void)
{
	CFont font;
	base = glGenLists(96);// 创建96个显示列表
	BOOL suc = font.CreateFontW(-24,0//如果为0则会按照比率自动匹对
		,0,0,FW_BOLD,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,
		ANTIALIASED_QUALITY,FF_DONTCARE|DEFAULT_PITCH,TEXT("Courier New"));
	if(!suc)
	{
		MessageBox(NULL,TEXT("创建字体失败"),TEXT("关闭错误"),MB_OK | MB_ICONINFORMATION);
	}
	CFont* oldfont = m_pDC->SelectObject(&font);
	wglUseFontBitmaps(m_pDC->GetSafeHdc(),32,96,base);// 创建96个显示列表,绘制从ASCII码为32-128的字符
	m_pDC->SelectObject(oldfont);
	font.DeleteObject();//删除字体
}
glRasterPos2f(-0.2f,0.2f);
glPrint("active opengl text with me");
//glPrint是nehe自己定义的字符串输出函数<stdarg.h>


void CMyLibOpenGL::KillFontList()
{
	glDeleteLists(base,96);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值