`
phinecos
  • 浏览: 339893 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

《基于MFC的OpenGL编程》Part 13 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,在CCY457OpenGLView类中加入下述变量:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
//ForText
GLuintm_3DTextList;
GLuintm_2DTextList;
BOOLm_b3DText,m_b2DText;

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->CCY457OpenGLView::CCY457OpenGLView()
{
m_xRot
=0.0f;
m_yRot
=0.0f;
m_b3DText
=FALSE;
m_b2DText
=FALSE;
}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnText2dtext()
{
m_b3DText
=FALSE;
m_b2DText
=TRUE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnText3dtext()
{
m_b3DText
=TRUE;
m_b2DText
=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateText2dtext(CCmdUI*pCmdUI)
{
pCmdUI
->SetRadio(m_b2DText);
}
voidCCY457OpenGLView::OnUpdateText3dtext(CCmdUI*pCmdUI)
{
pCmdUI
->SetRadio(m_b3DText);
}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::Create3DTextLists()
{
CFontm_font;
GLYPHMETRICSFLOATagmf[
256];
m_font.CreateFont(
-12,//HeightOfFont
0,//WidthOfFont
0,//AngleOfEscapement
0,//OrientationAngle
FW_BOLD,//FontWeight
FALSE,//Italic
FALSE,//Underline
FALSE,//Strikeout
ANSI_CHARSET,//CharacterSetIdentifier
OUT_TT_PRECIS,//OutputPrecision
CLIP_DEFAULT_PRECIS,//ClippingPrecision
ANTIALIASED_QUALITY,//OutputQuality
FF_DONTCARE|DEFAULT_PITCH,//FamilyAndPitch
"Algerian");
CFont
*m_pOldFont=m_pDC->SelectObject(&m_font);
//createdisplaylistsforglyphs0through255with0.1extrusion
//anddefaultdeviation.Thedisplaylistnumberingstartsat1000
//(itcouldbeanynumber)
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);
}
voidCCY457OpenGLView::Create2DTextLists()
{
CFontm_font;
m_font.CreateFont(
-24,//HeightOfFont
0,//WidthOfFont
0,//AngleOfEscapement
0,//OrientationAngle
FW_NORMAL,//FontWeight
FALSE,//Italic
FALSE,//Underline
FALSE,//Strikeout
ANSI_CHARSET,//CharacterSetIdentifier
OUT_TT_PRECIS,//OutputPrecision
CLIP_DEFAULT_PRECIS,//ClippingPrecision
DEFAULT_QUALITY,//OutputQuality
FF_DONTCARE|DEFAULT_PITCH,//FamilyAndPitch
"Algerian");
CFont
*m_pOldFont=m_pDC->SelectObject(&m_font);
//createdisplaylistsforglyphs0through255with0.1extrusion
//anddefaultdeviation.Thedisplaylistnumberingstartsat1000
//(itcouldbeanynumber)
m_2DTextList=glGenLists(256);
wglUseFontBitmaps(m_pDC
->GetSafeHdc(),0,255,m_2DTextList);
m_pDC
->SelectObject(m_pOldFont);
}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->BOOLCCY457OpenGLView::InitializeOpenGL()
{
//GetaDCfortheClientArea
m_pDC=newCClientDC(this);
//FailuretoGetDC
if(m_pDC==NULL)
{
MessageBox(
"ErrorObtainingDC");
returnFALSE;
}
//Failuretosetthepixelformat
if(!SetupPixelFormat())
{
returnFALSE;
}
//CreateRenderingContext
m_hRC=::wglCreateContext(m_pDC->GetSafeHdc());
//FailuretoCreateRenderingContext
if(m_hRC==0)
{
MessageBox(
"ErrorCreatingRC");
returnFALSE;
}
//MaketheRCCurrent
if(::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC)==FALSE)
{
MessageBox(
"ErrormakingRCCurrent");
returnFALSE;
}

//SpecifyBlackastheclearcolor
::glClearColor(0.0f,0.0f,0.0f,0.0f);
//Specifythebackofthebufferascleardepth
::glClearDepth(1.0f);
//EnableDepthTesting
::glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
SetupLighting();
//CreateFontDisplayLists
Create2DTextLists();
Create3DTextLists();
returnTRUE;
}

5,绘制函数修改如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::RenderScene()
{
//绘制函数
glTranslatef(-1.0f,0.0f,-5.0f);
glRotatef(
-10.0,1.0f,0.0f,0.0f);
glRotatef(
-10.0,0.0f,1.0f,0.0f);
if(m_b2DText)
{
//2D文本
//PositionTheTextOnTheScreen
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)
{
//3D文本
glListBase(m_3DTextList);
glCallLists(
6,GL_UNSIGNED_BYTE,"OpenGL");
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics