Nehe OpenGL tutorial (15th)

Source: Internet
Author: User
Tags wingdings font

Lesson 1

Texture ing of graphic fonts:

In this lesson, we will create a font with a texture based on the previous lesson, which is really simple.
 
After publishing the first two tutorials on Bitmap fonts and contour fonts, I have received many emails. Many readers want to know how to assign texture maps to fonts. You can use the automatic texture coordinate generator. It generates texture coordinates for each polygon in the font.
A small comment. This code is specially written for windows. It uses the WGL function of windows to create fonts. Obviously, the Apple system has AGL, the X system has Glx to support the same operation. Unfortunately, I cannot guarantee that the Code is also easy to use. If any code that can display text on the screen and is independent from the platform, please let me know, I will rewrite a tutorial on font.

We will use the code in Lesson 14th to create a texture font demonstration. If any part of the code in the program changes, I will rewrite all the code to see the changes I made.

We also need to add an integer variable called texture. It is used to save the texture. The next three lines are the code in Lesson 14th, which will not be changed.

Some minor changes have been made in the following sections. I plan to use the wingdings font in this lesson to display a pirate flag (Refer header and cross bone. If you want to display text, you do not need to change the code in Lesson 14th. You can also select another font.
Some people want to know how to use the wingdings font, which is one reason why I don't need the standard font. Wingdings is a symbolic font that requires some changes. It is not easy to tell windows to use the wingdings font. If you change the font name to wingdings, you will notice that the font is not selected. You must tell windows that a font is a symbolic font rather than a standard font. I will continue to explain it later.

Glvoid buildfont (glvoid) // create a bitmap font
{
Glyphmetricsfloat GMF [256]; // records information of 256 characters
Hfont font; // font handle

Base = glgenlists (256); // create 256 display lists
Font = createfont (-12, // font height
0, // font width
0, // The Rotation Angle of the font angle of escapement
0, // orientation angle of the font bottom line
Fw_bold, // font weight
False, // whether italics are used
False, // whether to use underline
False, // whether to use strikethrough

This is the magic line! Without the ansi_charset in Lesson 14th, we will use symbol_charset. This tells windows that the font we created is not a typical font consisting of standard characters. Symbol fonts are usually composed of small images (symbols. If you forget to change this line, wingdings, webdings, and other symbolic fonts you want will not work.

Symbol_charset, // sets the character set

The following rows remain unchanged.

Out_tt_precis, // output precision
Clip_default_precis, // cropping Accuracy
Antialiased_quality, // Output Quality
Ff_dontcare | default_pitch, // family and pitch

Now that we have selected the symbolic character set identifier, we can select the wingdings font.

"Wingdings"); // font name

The remaining lines of code remain unchanged.

SelectObject (HDC, font); // select the font to be created.

Wglusefontoutlines (HDC, // set the handle of the device description table in the current window
0, // create the ASCII value of the first character in the font of the display list
255, // number of characters
Base, // name of the first display list

We allow more errors, which means GL will not strictly abide by the font contour. If you set the error to 0.0f, you will find that there are some problems with the texture strictly on the surface. However, if you allow some errors, many problems can be avoided.

0.1f, // the smoothness of the font. The smaller the font size, the smoother the font size. the smoother the font size, the smoother the font size.

The following three lines of code are still the same

0.2f, // The distance highlighted in the z direction
Wgl_font_polygons, // use polygon to generate characters. Each vertex has an independent normal.
GMF); // the address of an array that receives the glyph metric data. Each array element is filled with the data of its corresponding display list characters.
}

Before the resizeglscene () function, add the following code to read the texture. You may recognize the code from the previous lessons. We create a place to save the bitmap, read the bitmap, tell windows to generate a texture, and save it in texture [0.
We create a refined texture (mipmapped texture), which looks better. The texture name is called lights.bmp.

If (textureimage [0] = loadbmp ("Data/lights.bmp") // load the bitmap

The following four lines of code will automatically generate texture coordinates for any object we draw on the screen. The function gltexgen is very powerful and complex. To fully understand its mathematical principles, you need to write another tutorial. However, you only need to know That gl_s and gl_t are texture coordinates. By default, it is set to the X and Y coordinates of the extract body at the moment on the screen, and converts them to vertex coordinates. You will find that the object has no texture on the zplane and only displays some stripes. Both the front and back are given textures, which are produced by the gltexgen function. (X (gl_s) is used to map textures from left to right, and Y (gl_t) is used to map textures from top to bottom.
Gl_texture_gen_mode allows us to select the texture ing mode we want to use on S and T texture coordinates. You have three options:

Gl_eye_linear-The texture is fixed on the screen. It will never move. The texture that the object will be given to in the area it passes.

Gl_object_linear-this is the mode we use. The texture is fixed on an object moving on the screen.
Gl_sphere_map-everyone loves it. Creates a metal object.

// Set the texture ing mode
Gltexgeni (gl_s, gl_texture_gen_mode, gl_object_linear );
Gltexgeni (gl_t, gl_texture_gen_mode, gl_object_linear );
Glable (gl_texture_gen_s); // use automatic texture generation
Glable (gl_texture_gen_t );

There are several lines of new code at the end of initgl. Buildfont () is placed after the code for reading the texture. The gl_color_material line is deleted. If you want to use glcolor3f (R, G, B) to change the texture color, set the glable (gl_color_material) this line is added to this part of code.

Int initgl (glvoid) // All OpenGL settings are started here
{
If (! Loadgltextures () // load texture
{
Return false; // return if the request fails.
}
Buildfont (); // create a font display list

Glshademodel (gl_smooth); // enables shadow smoothing.
Glclearcolor (0.0f, 0.0f, 0.0f, 0.5f); // black background
Glcleardepth (1.0f); // sets the depth cache.
Glable (gl_depth_test); // enable deep Test
Gldepthfunc (gl_lequal); // type of the deep Test
Glable (gl_light0); // use The 0th light
Glable (gl_lighting); // use illumination
Glhint (gl_perspective_correction_hint, gl_nicest); // notify the system to correct the perspective.

Start 2D Texture ing and select the first texture. In this way, we map the first texture to the 3D object we have drawn on the screen. If you want to add more operations, you can start or disable texture ing as needed.

Glable (gl_texture_2d); // uses a two-dimensional texture
Glbindtexture (gl_texture_2d, texture [0]); // select the texture used
Return true; // initialization successful
}

The code for resetting the size has not changed, but the drawglscene code has changed.

Here is the first change. We intend to use COs and sin to rotate the object around the screen instead of holding it in the middle of the screen. We will move the object to three units on the screen. On the X axis, we can move the range between-1.1 and + 1.1. We use the rot variable to control left and right movement. We limit the range of up and down movement to + 0.8 to-0.8. You can also use the rot variable to control up and down movement (it is best to make full use of your variable ).

// Set the font position
Gltranslatef (1.1f * float (COS (ROT/16366f), 0.8f * float (sin (ROT/20366f),-3.0f );

The following is a general rotation. This will rotate the symbols in the X, Y, and Z axes.

Glrotatef (rot, 1.0f, 0.0f, 0.0f); // rotate along the X axis
Glrotatef (rot * 1.2f, 0.0f, 1.0f, 0.0f); // rotate along the Y axis
Glrotatef (rot * 1.4f, 0.0f, 0.0f, 1.0f); // rotate along the Z axis

We move the object to the left and down to position the symbol in the center of each axis. Otherwise, when we rotate it, it does not seem to be rotating around its own center. -0.35 is just a number that can correctly display symbols. I have also tried some other numbers, because I don't know the font width and can adjust it as appropriate. I don't know why this font has no center.

Gltranslatef (-0.35f,-0.35f, 0.1f); // move it to a position that can be displayed.

Finally, we plot the pirate flag and add the rot variable so that the symbol can be rotated and moved on the screen. If you don't know how I got the pirate flag from the letter 'N', open Microsoft Word or wordpad. Select the wingdings font from the font drop-down menu. Enter 'n' in upper case to display the pirate flag.

Glprint ("N"); // draw the pirate flag
Rot + = 0.1f; // increase the rotation variable

The last thing to do is add the killfont () function at the end of killglwindow (), as shown below. It is important to add this line of code. It will clean up before we exit the program.

Although I did not elaborate on it, I think you should have a good understanding of how to make OpenGL generate texture coordinates for you. There should be no problem in assigning texture ing to your font or similar objects, and you only need to change the two lines of code to enable sphere ing, it's cool!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.