??? Hello, I am Skyman (SM3D), I like 3D programming, now I am learning OpenGL, DirectX, VR, etc., I often go to the major forums to turn. Among them, the largest place is NEHE's OpenGL website ( http://nehe.gamedev.net), that is the best learning OpenGL website I have ever seen, highly recommended!!! This is my first article about OpenGL, writing is not very good, welcome Everyone finances.
Everyone knows that there are two functions in OpenGL: WGluseFontBitMaps and WGluseFontoutLines, the former is used to output 2D text, the latter is used to output 3D text. To output Chinese characters, you must use the TrueType font. This article teaches you how to use WGluseFontoutLines To create a cool three-dimensional Chinese character effect. From this article, you can learn three points: 1. How to load the position from the file as a texture; 2. How to generate three-dimensional Chinese characters; 3. How to put the texture to 3D Chinese characters This program is shown in the following figure:
I am using Nehe's Simple Framework. For the sake of simplicity, I only list the added code here.
First, then head file definition district plus: #include
Then, in Bool Fullscreen = true; back defining the variables to be used:
Gluint Texture [1]; // Texture ID
INT ROT = 0; // Three-dimensional Chinese word around X-axis rotation angle
HFONT HFONT; / / Font Handle
Logfont LF; // logical font
GLYPHMETRICSFLOAT GMF [128]; // includes the structure of the shape and direction information
Unsigned int ichar = 0; // Character integer value
Char cchar; // To convert characters in the display list
Unsigned int i = 0; // cycle variable
Unsigned int j = 0; // cycle variable
Char text [128]; / /) / / Store arrays of characters to display
Byte TextList [128]; // Display list
Const gluint listbase = 1000; // Display the list of the list
Then LRESULT CALLBACK WNDPROC (HWND, UINT, WPARAM, LPARAM); later defines LoadBMP and LoadGlTextures loaded into documents, this is here in many tutorials in Nehe, here to write:
AUX_RGBIMAGEREC * LOADBMP (Char * filename) ??????????? // Load bitmap
{
?? file * file = NULL; ????????????????????? // file handle
?? if (! filename) ??????????????????????????? // file name has been given?
?? {
?????? return null; ??????????????????????, if it is not returned NULL
??}
?? file = fopen (filename, "r"); ???????????????? // check if the file exists
?? f (file) ????????????????????????????? // file exists?
?? {
?????? fclose (file); ????????????????????? // shut down file
?????? Return AuxDibImageLoad (filename); ???????????? // load bitmap and return the pointer of the bitmap
??}
?? Return null; ???????????????????????? / Returns NULL if loaded
}
?
INT loadGLTEXTURES () ????????????????????????????? // load bitmap and convert to texture
{
?? int status = false; ???????????????????? // status indication
?? aux_rgbimagerec * textureImage [1]; ???????????????? // created a storage space for texture ?? MEMSET (TextureImage, 0, Sizeof (VOID *) * 1); ?? ???????? ?? // clear
?? // Load bitmap, check, exit, if the bitmap does not exist
?? IF (TextureImage [0] = LoadBMP ("Data / Tex.bmp")) // Texture Picture Path
?? {
?????? status = true; ??????????????????????????????????
?????? GlGentextures (1, & texture [0]); ??????????????? // creation texture
?????? Glbindtexture (GL_Texture_2D, Texture [0]);
?????? Glubuild2dmipmaps (GL_Texture_2D, 3, TextureImage [0] -> SIZEX, TextureImage [0] -> Sizey, GL_RGB, GL_UNSIGNED_BYTE, TEXTUREIMAGE [0] -> DATA);
?????? gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_Linear); // Filter
?????? gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); // Filter
?????? Gltexgeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); / / Automatically generate texture coordinates
?????? Gltexgeni (GL_T, GL_TEXTURE_GEN_MODE, GL_Object_Linear); // Automatically generate texture coordinates
?????? glenable (GL_TEXTURE_GEN_S);
?????? glenable (GL_TEXTURE_GEN_T);
??}
?? IF (TextureImage [0]) ???????????????????? / // If the texture exists
?? {
?????? if (TextureImage [0] -> DATA) ??????????????? / If the texture image exists
?????? {
?????????free (TextureImage [0] -> DATA); ?????????????????? // release the texture image occupied by the memory
??????}
?????? free (TextureImage [0]); ??????????????????? // release image structure
??}
?? Return status; ??????????????????????????? // returned
}
Note: Because the three-dimensional font is a more complex model, it is difficult to manually define the texture coordinates. We can let OpenGL automatically generate texture coordinates. Functions GLTEXGEN () is used to automatically generate texture coordinates.
Void Gltexgen {IFD} (Glenum Coord, GleNum PName, Type Param);
Coord parameters must be GL_S, GL_T, GL_R or GL_Q, indicate whether to generate the texture coordinates s, t, r or Q parameter pname is GL_TEXTURE_GEN_MODE, showing the mode of generating texture coordinates. Alternatively parameter param GL_OBJECT_LINEAR, GL_EYE_LINEAR or GL_SPHERE_MAP. GL_OBJECT_LINEAR represents The texture image is stationary relative to a moving object, which is used in this way.
Then in the initgl () function, GLHINT (GL_NISPECTIVE_CORRECTION_HINT, GL_NICEST); after adding the following statement:
Glenable (GL_LIGHT0); ?????????????????????????? // activated the default illumination (although the light effect is not very good but the speed is fast) ?? Glenable GL_LIGHTING); ??????????????????????? // activated light
?? glenable (GL_COLOR_MATERIAL); / / Activate Material Color
?? Glenable (GL_Texture_2D); // Activate 2D texture
??? GlbindTexture (GL_Texture_2D, Texture [0]); // Bind texture
?
??? MEMSET (& lf, 0, sizeof (logfont)); // Clear memory
??? // Set the properties of logical font LF
?? lf.lfheight = -1; // Specify the word high
?? lf.lfwidth = -1; // Specify the word wide
???? lf.LFescapement = 0; // Specify angle (1/10 degrees)
???? lf.lForientation = 0; // Specify the baseline of the character and the angle of the X-axis (1/10 degrees)
???? lf.lfweight = fw_bold; // Specify the weight of the font (fw_bold = 700)
???? lf.lfitalic = false; / / Specify whether it is behind
???? lf.lfunderline = false; / / Specify whether there is an underscore
???? lf.lfstrikeout = false; / / Specify whether it is a strikeout font
???? lf.lfcharset = ANSI_CHARSET; / / Specified character set
???? lf.lfoutPrecision = OUT_TT_PRECIS; / / Specify output accuracy
???? lf.lfclipprecision = clip_default_precis; // Specify cropping precision
???? lf.lfquality = proof_quality; // Specify the output quality
???? lf.lfpitchandfamily = variable_pitch | TMPF_TRUETYPE | FF_MODERN; / / Specify the positioning and appearance of the font
???? lstrcpy (lf.lffacename, "Song"); // Specify the font name
?? hfont = createfontIndirect (& lf); // Create a logical font
??? SelectObject (HDC, HFONT); // Select the logical font to the device environment
Then join in the drawglscene () function in the Drawglscene () function:
STRCPY (TEXT, "Chongqing University Welcome"); / / Specify the character to be displayed
?? i = 0; // 初 初 制
?? j = 0; // 初 制
?? While (i <128)
?? {
?????? if (isdbcsleadbyte (text [i])) // determine if it is double byte
?????? {
??? ichar = text [i];
???? iChar = (iChar << 8) 256; // 256 is the "offset" of the Chinese characters
???? iChar = iChar text [i 1];
???? i ; i ;
??? WGlusefontOutlines (HDC, // device environment handle
???? iChar, // To convert the first character to the display list
???? 1, // To convert the number of characters that display the list
??????? 1000 j, // Display the base of the list
????
0.0F
, // Specify the maximum offset with the actual contour
????
0.2F
// 在 z 负 负 方 向
????? WGL_FONT_POLYGONS, // Specify the display list to create a font with a polygon
???? & gmf [j]); // accept the address of the character ???? TextList [J] = J;
???? j ;
??????}
?????? else // is single byte (English)
?????? {
?? cchar = text [i];
???? i ;
???? WGLUSEFONTOUTLINES (HDC, // device environment handle
???? cchar, // to convert the first character to the display list
???? 1, // To convert the number of characters that display the list
??????? 1000 j, // Display the base of the list
????
0.0F
, // Specify the maximum offset with the actual contour
????
0.5F
// 在 z 负 负 方 向
????? WGL_FONT_POLYGONS, // Specify the display list to create a font with a polygon
???? & gmf [j]); // accept the address of the character
???? textlist [j] = j;
???? j ;
??????}
??}
??? gllistbase (listbase); // Set the initial index value of the display list
?? GLTRANSLATEF (
-4.0F
,
0.0F
,
-10.0F
); // The location of the text to be displayed
?? Glrotatef (Float (ROT),
1.0f
,
0.0F
,
0.0F
); // text around X-axis
?? GlcallLists (Strlen (Text), GL_UNSIGNED_BYTE, & TEXTLIST); // Call Multi-Display List Drawing Text
?? GLFLUSH (); // Mandatory drawing
??? ROT = (Rot 1)% 360; // Rotary variable increase
?? ?? Note: 1. Double-byte internal code DBCS (Dual-word character set) supports many different East Asian language letters, such as Chinese, Japanese and Korean. DBCS uses numbers 0-128 to represent the ASCII character set. Other numbers greater than 128 as the leader based character, it is not true characters, just simple indicating that the next character belongs to the non-Latin character set. In DBCS, the length of the ASCII character is one byte, while Japanese, Korean and other East Asian characters are 2 bytes. The offset of the Chinese characters is 256.
???? 2. Executing a multi-display list can effectively improve rendering speed. The method is to put the index value of the display list in an array and then call the GLCALLSTS () command. For a variety of fonts, you need to create a different initial display list index value for each font. These initial index values can be specified by calling the GLLSTBASE () function before calling the GLCALLSTS () function. This function specifies an offset that is added to the display list index in the GlcallLists () function to obtain the final display list index value. The function GlcallLists (n, type, * lists) performs N displays a list. The listed index value performed is an offset indicated by the currently display list (specified by gllistbase ()), plus the sum of an integer from the array pointed by the LISTS.
???? ok. Everything gets! Compilation, what did you see! Good, cool three-dimensional Chinese character effect! (Note: This program can not only display Chinese characters, but also in English :)). You can join the effect of light, material, and fog, make it more cool! ! !
???? This is my first tutorial about OpenGL. If you find any mistakes or any comments, please write to me. My e-mail is: skyman_2001 @ 163.com, QQ: 175910174, my website is: http://sm3d.126.com, welcome to exchange!
?? ?? Download source code: Visual C ? (Note: The downloaded file suffix is called .txt, change its suffix name to .rar, then use WinRAR to decompression. Source code is stored in free space, unstable Which stable storage space can be stored for me) ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????????? to Nehe! ! !