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
  • 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
  • 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
  • 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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds