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

  • This paper examines the difficulties and pain points that organizations encounter as they seek to straddle the conflicting pressures of ever increasing email volumes on one hand, and the need for faster, more flexible accessibility on the other. It also explores what practices and policies are currently in use when it comes to archiving emails, and the ways in which businesses can improve practices in this crucial area. There are, for example, options available which can bring email archiving up-to-date, with …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds

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