Text display under OpenGL

zhaozj2021-02-16  57

The text display in the OpenGL state has been a problem. This article uses the nested display list to display the display of the string in the OpenGL state.

There is a need to pay attention to:

1: This program displays the text of a font according to the given height and text, where some fonts are defined within the OpenGLText, which can be sailed at will. If the code is used in practical applications, the text spacing of the recommended text can be set by ourselves, and the inclination angle, rotation angle, positioning mode, and the like can also add parameters.

2: The fifth parameter of the WGLUSEFONTOUTLINES function indicates the accuracy of the text (chord deviation), which is generally set to zero, but this default approach will cause the text to be too rough, you can set it into a relatively small number to improve text display The accuracy, but this will increase the amount of memory.

3: If the height of the text is relatively small, the text's line may break the phenomenon of break, affecting the appearance, solving such problems: (1) Use OpenGL to reverse the technology; (2) Put the text outline Outline; (3) Calling the GlcallList function multiple times in the DRAW member function of the text, each time the call shifts a pixel to the surroundings, so that the line breaks of the text will be greatly changed, the speed is also the fastest in three ways. .

Detailed code in this article here, welcome to reference: Download source

The application is as follows:

CopENGLTEXT TEXT;

CopENGLTEXT TEXT2;

TEXT2.M_DX = 0;

TEXT2.M_DY = 200;

GLLoadIndentity ();

GLCLEAR (GL_COLOR_BUFFER_BIT);

Glcolor4f (0.0F, 0.0F, 1.0F, 1.0F);

Text.draw ("Song Body");

Text2.draw ("体 _GB2312");

GLFlush ();

The statement and implementation of the class is as follows:

#include

Using namespace std;

Class CopenguText

{

PUBLIC:

// Construct the text

CopENGLTEXT ();

Virtual ~ CopenglText ();

/ / Draw the text of the font, the font is only set when drawing, and then it can be introduced to the null value.

Void Draw (Char * strfontname);

/ / Release the space occupied by the text

Void free ();

// Text string

String m_str;

// String height

Double m_dheight;

// String position

Double M_DX;

Double m_dy;

protected:

Bool genlist ();

Bool gencharslists (char * strfontname);

Int m_idisplayList;

}

//

// CopenglText Class

//

//

// construction / destruction

//

CopENGLTEXT :: CopenglText ()

{

m_dx = 0;

m_dy = 0;

m_str = "ABC China";

m_dheight = 100;

m_idisplayList = 0;

}

CopENGLTEXT :: ~ CopenglText ()

{

FREE ();

}

Bool CopenGlText :: GenCharslists (Char * strfontname)

{

HDC HDC;

Const char * str = m_str.c_str ();

HDC = CREATEDC ("Display", "", ", NULL); int inum = _mbslen (const unsigned char *) STR);

m_idisplayList = GlgenLists (Inum 1);

Hfont hnewcfont;

Logfont clogfont; // Store the current font parameters

// Initialization font parameters

MEMSET (& Clogfont, 0, Sizeof (logfont));

Clogfont.lfescapement = clogfont.lforientation = 0;

Clogfont.lfWeight = fw_normal;

Clogfont.lfcharset = GB2312_CHARSET

Clogfont.lfoutPrecision = OUT_DEFAULT_PRECIS;

Clogfont.lfquality = default_quality;

Clogfont.lfpitchandfamily = default_pitch | ff_swiss;

STRCPY (ClogFont.Lffacename, LPCTSTR (StrfontName);

Clogfont.lfheight = -10;

Clogfont.lfWidth = 0;

Hnewcfont = CREATEFONTINDIRECT; & clogfont;

HFONT HOLDFONT = (HFONT) SELECTOBJECT (HDC, HNewCFont);

INT i = 0, j = 0, itotal = Strlen (STR) ;;

Uint nchar = 0;

While (i

{

J ;

IF (IsdbcsleadByte (STR [I]))

{

Nchar = (unsigned char) STR [i]) * 0x100 (unsigned char) STR [i 1];

i = 2;

Else

{

nchar = str [i];

i ;

}

Glyphmetricsfloat AGMF [1];

Bool Bok = WGlusefontoutLines (HDC, Nchar, 1, M_IDisplayList J, 0.002F, 0, WGL_FONT_POLYGONS, AGMF);

}

SelectObject (HDC, HoldFont);

DeleteObject (Hnewcfont);

Deletedc (HDC);

Return True;

}

Bool coolltext :: genlist ()

{

INT INUM = _MBSLEN ((const unsigned char *) m_str.c_str ());

Glnewlist (m_idisplaylist, gl_compile);

For (INT i = 1; i <= inum; i )

{

GLPUSHMATRIX ();

/ / Modify the text spacing here

GLTRANSLATED (M_DX M_DHEIGHT * (I-1), M_DY, 0);

Glscaled (M_DHEIGHT, M_DHEIGHT, 1);

GlcallList (m_idisplayList i);

GLPOPMAMATRIX ();

}

glendlist ();

Return True;

}

Void CopenglText :: free ()

{

IF (Glislist (m_idisplaylist))

GLDELETELISTS (m_idisplaylist, _mbslen ((const unsigned char *) m_str.c_str ()) 1); m_idisplayList = 0;

}

Void CopenglText :: Draw (Char * StrfontName)

{

IF (! GLISLIST (M_IDisplayList))

{

Gencharslists (strfontname);

Genlist ();

}

GlcallList (m_idisplaylist);

}

转载请注明原文地址:https://www.9cbs.com/read-22824.html

New Post(0)