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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds