DirectX 10 Draw Text to texture

2019-07-27 02:11发布

I am atempting to draw text to a textured mesh object. The mesh is basically a cube which has been pasted up to the front of the screen. (overall I'm aiming for a menu system). After a bit of googling I've found surprisingly little information on how to draw a text to a texture. I've read that it should be as simple as making a texture, setting it as a render source and then drawing to it, yet I cannot figure this one out.

Any ideas, suggestions or skeleton code with the necessary function calls to give me a jump start?

2条回答
ら.Afraid
2楼-- · 2019-07-27 02:40

See the section "Rendering to a texture" in Creating Texture Resources for some very helpful documentation. That provides several snippets of example code that should help you get on your way.

查看更多
爷、活的狠高调
3楼-- · 2019-07-27 02:50

When you need to render text into texture, you should setup rendering to texture as Adam suggested, to actually render some text you can do this

Create font and sprite

D3DX10CreateFont(device, 16, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Verdana", &font);
D3DX10CreateSprite(device, 256, &sprite);

and render

hr = sprite->Begin(0);
left = L"Some text to render";
RECT r;
r.top = 0;
r.bottom = window->height;
r.left = 0;
r.right = window->width;
hr = font->DrawTextW(sprite, left.c_str(), -1, &r, DT_LEFT | DT_TOP | DT_CALCRECT, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f));
hr = font->DrawTextW(sprite, left.c_str(), -1, &r, DT_LEFT | DT_TOP, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f));
//hr = sprite->Flush();
hr = sprite->End();
device->OMSetBlendState(NULL, 0, 0xffffffff);

Beware, sprite rendering changes blend state, so be sure to set it to default. If you need only "text on plain surface" (no texture mapping on mesh) you could render sprite into main scene instead of texture.

查看更多
登录 后发表回答