3D font in OpenGL

Sample Image

In this article, I give a wrap class which encapsulate the OpenGL 3D Font functions. With this class, you can design any 3D letter as a 3D geometry object, translating, scaling, rotating, texture mapping, etc.

The key steps to display a 3D letter is:

1) Create a display list

2) Create and set a font to current windows context device, wglUseFontOutlines function can retrieve the 3d Font geometry data from the context device.

3) call
glListBase and glCallLists to display 3D font letters.

The
wglUseFontOutlines function calculate the 3D font geometry data from the mathmatic module of Windows system TrueType fonts library.

My 3D font class contain position, material color data and content string. It can be used as a separate geometry object in a multiple 3D objects scene.

Here is a small example:



class C3DFontView : public CView
{
  ...
  CGLFont  m_FontX; // Declare a font object in your view class
  ...
};

int CMy3DFontView::OnCreate(LPCREATESTRUCT lpCreateStruct) // Create view
{
  if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;
/*
** Setup font attributes
*/
  m_FontX.SetXOffset(-0.4f);
  m_FontX.SetYOffset(-0.4f);
  m_FontX.SetXScale(1.4f);
  m_FontX.SetYScale(1.3f);
  m_FontX.SetZScale(0.8f);
  m_FontX.SetXRotate(-20.0f);
  m_FontX.SetYRotate(-6.0f);
  m_FontX.SetZRotate(90.0f);

  wglMakeCurrent(m_pDC->m_hDC,m_hRC);
  m_FontX.SetFontType(GL_FONT_SOLID); // Select font type
  m_FontX.CreateFont(m_pDC, "Arial Black"); // Create the font
  m_FontX.SetText("Xing"); // Select text to render

  wglMakeCurrent(NULL,NULL);

}

void CMy3DFontView::OnDraw(CDC* pDC) // Draw method
{
  CMy3DFontDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);

  ...
	
  wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
  DrawGLFont(); // Render the text
  SwapBuffers(m_pDC->GetSafeHdc());
  wglMakeCurrent(m_pDC->GetSafeHdc(), NULL);

}


void CMy3DFontView::DrawGLFont(void) // The render method
{
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);
	
  //clear color buffer
  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  //set light model
  glLightfv(GL_LIGHT0, GL_AMBIENT, m_Lightambient); //set light ambient
  glLightfv(GL_LIGHT0, GL_DIFFUSE, m_Lightdiffuse); //set light specular
  glLightfv(GL_LIGHT0, GL_SPECULAR, m_Lightspecular); //set light specular
  glLightfv(GL_LIGHT0, GL_POSITION, m_Lightposition); //set light position

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(-0.5f, 0.0f, -5.0f);
	
  m_FontX.GLDrawText(); // Render the text

  glFlush();

}

Downloads

Download demo project - 175 Kb

Download source - 3 Kb

History



Comments

  • Who do I create simple text with black line around the letter?

    Posted by Legacy on 06/19/2003 12:00am

    Originally posted by: ron

    Who do I create simple text with black line around the letter?
    
    Thank you!!!
    ron.

    Reply
  • Who do I create simple text with black line around the letter?

    Posted by Legacy on 06/19/2003 12:00am

    Originally posted by: ron

    Who do I create simple text with black line around the letter?
    Thank you!!!
    ron.

    Reply
  • Who do I create simple text with black line around the letter?

    Posted by Legacy on 06/19/2003 12:00am

    Originally posted by: ron

    Who do I create simple text with black line around the letter?
    
    Thank you!!!
    ron.

    Reply
  • Who do I create simple text with black line around the letter?

    Posted by Legacy on 06/19/2003 12:00am

    Originally posted by: ron

    Who do I create simple text with black line around the letter?
    
    Thank you!!!
    ron.

    Reply
  • Chinese text?

    Posted by Legacy on 12/22/2002 12:00am

    Originally posted by: zhourong

    I want to know how to display chinese TrueType. I tried to do that, but it display another chinese text like "问" or "屯",i want to know what's the problem with it. So if you can help me , please do it!
    
    Thank you !

    Reply
  • Where is the author????

    Posted by Legacy on 10/06/2002 12:00am

    Originally posted by: Jianjun Zhao

    How to display Chinese font?Any help?

    Reply
  • Chinese Font?

    Posted by Legacy on 10/06/2002 12:00am

    Originally posted by: Yu-Sheng Lai

    Any body knows how to display Chinese font?
    Thank you very much beforehand.
    - Lance

    Reply
  • Where is the author????

    Posted by Legacy on 10/06/2002 12:00am

    Originally posted by: Jianjun Zhao

    How to display Chinese font?Any help?

    Reply
  • Where is the author????

    Posted by Legacy on 10/06/2002 12:00am

    Originally posted by: Jianjun Zhao

    How to display Chinese font?Any help?

    Reply
  • Keep Attribute -- little affected by other codes

    Posted by Legacy on 12/04/2000 12:00am

    Originally posted by: lxcao


    void CGLFont::GLDrawText()
    {
    if (m_uiListID == 0 || m_strText.IsEmpty() ||
    m_dXScale == 0.0 || m_dYScale == 0.0 ||
    m_dZScale == 0.0)
    {
    return;
    }

    GLsizei size = m_strText.GetLength();

    //**/////Add this/////**//
    glPushAttrib(GL_ALL_ATTRIB_BITS); //may other flags
    //**/////End Add/////**//

    glPushMatrix();

    ...............
    ...............

    glPopMatrix();

    //**/////Add this/////**//
    glPopAttrib();
    //**/////End Add/////**//
    }

    Reply
  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds