《基于MFC的OpenGL编程》Part 12 Creating 2D and 3D Text

《基于MFC的OpenGL编程》Part 12 Creating 2D and 3D Text

原文链接

源码链接

wglUseFontBitmaps函数

The wglUseFontBitmaps() function creates a set of bitmap display lists based on the glyphs in the currently selected font in the current DC for use in the current OpenGL RC. It basically creates a series of sequential display lists which can be executed using the function glCallLists. The function takes care of aligning the raster positions of subsequent bitmaps once we specify the raster position for the first bitmap. We use the glRasterPos function to set the current raster position, where the bitmapped text would start appearing.

The  glRasterPos function works exactly the same way as glVertex function, the only difference being that the position is being transformed and not the object. Thus when we use wglUseFontBitmaps to generate display lists and then call them, the resulting text is displayed, starting at the current raster position, and the bitmaps are copied to the raster buffer, giving the effect of always having the text positioned in the xy plane of the screen. 

Thus we would use wglUseFontBitmaps when we need the text to be visible to the user and that the size of the text relative to its distance from the viewpoint doesn't matter.

wglUseFontOutlines函数

The wglUseFontOutlines function creates a set of 3D polygon or line based primitive display lists, based on the glyphs in the currently selected TrueType font in the current DC for use in the current OpenGL RC. Stroke and Raster fonts are not supported. These objects can then be used to draw 3D characters. This function also has additional arguments that control the extrusion of the 3D characters in the +Z direction, the deviation of the generated primitive vertices from the design outline of the font, whether to generated filled polygons or a wireframe primitives and an array of structures to hold the metrics of each of the generated characters.

1、在CMy2OpenGLView类中加入下述变量:

<span style="font-size:14px;">    GLuint m_3DTextList;
    GLuint m_2DTextList;
    BOOL m_b3DText, m_b2DText;</span>

并在构造函数中进行初始化:

<span style="font-size:14px;">	m_b3DText=FALSE;
	m_b2DText=FALSE;</span>

2、加入两个用来创建文本的菜单项及其事件处理函数:

void CMy2OpenGLView::OnText2Dtext() 
{
	// TODO: Add your command handler code here
	m_bCube        = FALSE;    
    m_bTorus       = FALSE;        
    m_bTeapot      = FALSE;        
    m_bIcosahedron = FALSE;
    m_bSimpleCube  = FALSE; 
	m_bSun		   = FALSE;
	m_bCubeMove	   =FALSE;
	m_bSunMove	   =FALSE;

	m_b2DText=TRUE;
	m_b3DText=FALSE;
	m_bCubeTexture =FALSE;
	InvalidateRect(NULL,FALSE);
}


void CMy2OpenGLView::OnUpdateTEXT2Dtext(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_b2DText);
}

void CMy2OpenGLView::OnText3dtext() 
{
	// TODO: Add your command handler code here
	m_bCube        = FALSE;    
    m_bTorus       = FALSE;        
    m_bTeapot      = FALSE;        
    m_bIcosahedron = FALSE;
    m_bSimpleCube  = FALSE; 
	m_bSun		   = FALSE;
	m_bCubeMove	   =FALSE;
	m_bSunMove	   =FALSE;

	m_b2DText=FALSE;
	m_b3DText=TRUE;
	m_bCubeTexture =FALSE;
	InvalidateRect(NULL,FALSE);
}

void CMy2OpenGLView::OnUpdateText3dtext(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_b3DText);
}

3、实际创建2D3D文本列表的函数:

void CMy2OpenGLView::Create3DTextLists()
{
	 glShadeModel(GL_FLAT);

	CFont m_font;
	GLYPHMETRICSFLOAT agmf[256];
	m_font.CreateFont(-24,
						0,
						0,
						0,
						FW_BOLD,//FW_NORMAL
						FALSE,
						FALSE,
						FALSE,
						ANSI_CHARSET,
						OUT_TT_PRECIS,
						CLIP_DEFAULT_PRECIS,
						ANTIALIASED_QUALITY,
						FF_DONTCARE|DEFAULT_PITCH,
						"Algerian");
	CFont* m_pOldFont = m_pDC->SelectObject(&m_font);
	//Create display lists for glyphs 0 through 255 with 0.1 extrusion
	//and default deviation.The display list numbering starts at 1000
	//(it could be any number)
	m_3DTextList = glGenLists(256);
	wglUseFontOutlines(m_pDC->GetSafeHdc(),0,255,m_3DTextList,0.0f,0.5f,WGL_FONT_POLYGONS,agmf);
	m_pDC->SelectObject(m_pOldFont);

}

void CMy2OpenGLView::Create2DTextLists()
{
	glShadeModel(GL_FLAT);

	CFont m_font;
	m_font.CreateFont(-24,//Height of font
						0,//Width of Font
						0,//angle of Escapement
						0,//Orientation Angle
						FW_NORMAL,//Font Weight
						FALSE,//Italic
						FALSE,//Underline
						FALSE,//Strikeout
						ANSI_CHARSET,//Character Set Identifier
						OUT_TT_PRECIS,//Output Precision
						CLIP_DEFAULT_PRECIS,//Clipping precision
						DEFAULT_QUALITY,//Output Quality
						FF_DONTCARE|DEFAULT_PITCH,//Family and pitch
						"Algerian");
	CFont* m_pOldFont = m_pDC->SelectObject(&m_font);
	//Create display lists for glyphs 0 through 255 with 0.1 extrusion
	//and default deviation.The display list numbering starts at 1000
	//(it could be any number)
	m_2DTextList = glGenLists(256);
	wglUseFontBitmaps(m_pDC->GetSafeHdc(),0,255,m_2DTextList);
	m_pDC->SelectObject(m_pOldFont);
}

4、InitializeOpenGL函数中调用上述两个函数:

int CMy2OpenGLView::InitializeOpenGL()
{
	//Get a DC for the Client Area
    m_pDC = new CClientDC(this);
    //Failure to Get DC
    if(m_pDC == NULL)
    {
        MessageBox("Error Obtaining DC");
        return FALSE;
    }
    //Failure to set the pixel format
    if(!SetupPixelFormat())
    {
        return FALSE;
    }
    //Create Rendering Context
    m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());
    //Failure to Create Rendering Context
    if(m_hRC == 0)
    {
        MessageBox("Error Creating RC");
        return FALSE;
    }
    //Make the RC Current
    if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)
    {
        MessageBox("Error making RC Current");
        return FALSE;
    }
    //Specify Black as the clear color
    ::glClearColor(0.5,0.0,0.5,1.0);/此时为紫色//此处设置背景颜色值1.0,1.0,1.0,1.0(白色)|0.0f,0.0f,0.0f,0.0f(黑色)
    //Specify the back of the buffer as clear depth
    ::glClearDepth(1.0f);
    //Enable Depth Testing
    ::glEnable(GL_DEPTH_TEST);

	::glEnable(GL_SMOOTH);
	//set effect of Fog
	::glFogi(GL_FOG_MODE,GL_EXP);
	GLfloat fog_color[4] = {0.2f,0.2f,0.2f,0.0f};
	::glFogfv(GL_FOG_COLOR,fog_color);
	::glFogf(GL_FOG_DENSITY,0.25);
   

	::glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_DST_ALPHA);
	
	//SetupLighting();

	 CreateSceneList();
	 Create2DTextLists();
	 Create3DTextLists();

    return TRUE;
}

5、绘制函数void CMy2OpenGLView::RenderScene3D()中添加如下代码:

	if (m_b2DText)
	{
		glLoadIdentity();
		glTranslatef(-0.0f,0.0f,-5.0f);
		glRotatef(-10.0,1.0f,0.0f,0.0f);
		glRotatef(-10.0,0.0f,1.0f,0.0f);
		//2D文本
		//Position the text on the screen
		glDisable(GL_LIGHTING);
		glColor3f(1.0f,1.0f,0.0f);
		glRasterPos2f(0,0);
		glListBase(m_2DTextList);
		glCallLists(6,GL_UNSIGNED_BYTE,"OpenGL");
		glEnable(GL_LIGHTING);
	}
	if (m_b3DText)
	{
		glLoadIdentity();

// 		glTranslatef(-0.0f,0.0f,-5.0f);//如果不想手动旋转就用这三句
// 		glRotatef(-10.0,1.0f,0.0f,0.0f);
// 		glRotatef(-10.0,0.0f,1.0f,0.0f);

		glTranslatef(-2+m_xPos,m_yPos,-5.0f);//手动调节旋转
		glRotatef(m_xAngle,1.0f,0.0f,0.0f);
		glRotatef(m_yAngle,0.0f,1.0f,0.0f);
		//3D文本
		glDisable(GL_LIGHTING);
		glColor3f(1.0f,1.0f,0.0f);
	 	glRasterPos2f(0,0);

		glListBase(m_3DTextList);
		glCallLists(6,GL_UNSIGNED_BYTE,"OpenGL");

	 	glEnable(GL_LIGHTING);
	}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值