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

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date