Directx11 tutorial (66) d3d11 screen text output (1)

Source: Internet
Author: User

In d3d10, through the id3dx10font interface object, we can conveniently output text information on the screen. A drawtext function can solve all the problems, but in d3d11, this becomes a super troublesome problem, because Microsoft has removed the font interface and needs to output text on the screen, you need to do a lot of things.

Generally, we can use the following methods to output text information:

(1) Use texture maps to store all fonts on one texture, and then create a font query table. The corresponding positions of the textures can be rendered in 2D mode, the text is dyed out, but this method is not very flexible. It is easy to say that there are so many letters and characters in English, but it is troublesome for Chinese characters. In addition, the font size cannot be scaled at will.

(2) interact with GDI/GDI + to obtain fonts, store them in textures, and then render them using 2D rendering. In this way, you can obtain texts of any size, it is applicable to both English and Chinese.

(3) using direct write, but it is said that the efficiency is not high, http://www.braynzarsoft.net/index.php? P = d3d11font is detailed in this tutorial.

(4) use some text output controls, such as cegui.

(5) Use FreeType2 to dynamically generate a font texture and display it in 2D rendering.

In this tutorial, we will first look at the specific implementation of the first method. The program code is modified on the basis of mytutoriald3d11_61.

1. Prepare an image containing all English letters, punctuation marks, and special characters (all characters between ASCII code-). The image will be treated as a texture.

The image background is black and RGB is (0, 0, 0). This image is used to generate a font. Dds texture resource file.

2. Create an index file fontdata.txt, which indexes all characters between the ASCII code 32-. The format of this file is:

[ASCII value of character] [the character] [left texture U coordinate] [right texture U coordinate] [pixel width of character]

The first column is the ASCII code of the character, the second column is the character, the third column is the texture coordinate on the left of the character, the fourth column is the texture coordinate on the right of the character, and the fifth column is the pixel width.

32 0.0 0.0 0
33! 0.0 0.000976563 1
34 "0.00195313 0.00488281 3
35 #0.00585938 0.0136719 8
...

125} 0.573242 0.576172 3
126 ~ 0.577148 0.583984 7

With these column values, we can match a character with the corresponding position in the texture. For example, if a string is "hello", we can dynamically create five quadrants, each quadrilateral consists of two triangles. The vertex position of the triangle is the display position of the character on the screen. The vertex attributes include texture coordinates. The quadrilateral displays the character in texture mode.

The following describes the added code:

Our 2D text rendering in textclass can regard this class as a sprites class. The main function is to render the text.

First, this class uses two structures to describe the text. The first one is the structure type of the text sentence, because the text is displayed in the Quadrilateral, this class contains vertex buffering, index buffering, vertex quantity, vertex color, and other information.

Struct sentencetype
{
Id3d11buffer * vertexbuffer, * indexbuffer;
Int vertexcount, indexcount, maxlength;
Float red, green, blue;
};

Struct vertextype
{
D3dxvector3 position;
D3dxvector2 texture;
};

The initializesentence and updatesentence functions are used to generate text sentence data. rendersentence function rendering performs final rendering. The texture contains the font texture. This function calls the fontshader class to implement final rendering.

Bool textclass: rendersentence (id3d11devicecontext * devicecontext, sentencetype * sentence, d3dxmatrix worldmatrix,
D3dxmatrix orthomatrix, id3d11shaderresourceview * texture)

Class fontclass creates vertices and indexes of text sentences, and the fontshaderclass class is used for rendering.

In addition, there are two shader files: font. vs and font. in the PS and PS files, the Alpha value is set based on the texture color, so that the font texture and background can be well mixed.

// Select characters.
Color = shadertexture. Sample (sampletype, input. Tex );

// If the texture is black, it is transparent, so that you can hollow out unnecessary backgrounds and only display fonts.
If (color. r = 0.0f)
{
Color. A = 0.0f;
}
Else
{
Color. RGB = pixelcolor. RGB;
Color. A = 1.0f;
}

The final rendering code is in the graphicsclass class. When starting text rendering, you must disable zbuffer and enable the Alpha blend function. After rendering is complete, perform the opposite operation.

// Turn off the Z buffer for 2D Rendering
M_d3d-> turnzbufferoff ();

// Open alpha blending
M_d3d-> turnonalphablending ();

// Obtain the orthogonal projection matrix
M_d3d-> getorthomatrix (orthomatrix );
// Render text
Result = m_text-> render (m_d3d-> getdevicecontext (), worldmatrix, orthomatrix, m_texmanager-> createtex (m_d3d-> getdevice (), string ("font. DDS ")));
If (! Result)
{
Return false;
}

// Disable alpha blending
M_d3d-> turnoffalphablending ();

// Enable Z buffer
M_d3d-> turnzbuffon ();

The execution interface of the program is as follows. The white and yellow characters tessellation demo by mikewolf are displayed:

Complete code can be found:

Project File mytutoriald3d11_63

Download Code:

Provided later

 

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.