4

I'd like to be able to apply a transparent texture to each face of a simple 3D cube. Each texture is simply the name of the face, e.g. "Front", "Back", "Left", etc. Normally I'd just create an image in a paint app, then save it, and load the image as a texture at runtime. However, the twist is that I'd like this text to be in multiple (and growing) different languages and although creating six new images for each language is one method, it would be preferable to dynamically generate textures from translated strings instead.

So the ideal solution is to be able to render a text string to a 2D texture at runtime (using any font and colour I choose), then render the resultant texture over each surface as a transparent overlay. Can this be done relatively easily?

e.g. this sort of process:

text-to-texture-to-cube

Getting the texture onto the cube is easy enough. It's the getting the text (string) rendered to a texture (ID3D11Texture2D) which is the tricky part. It seems as though it should be very do-able. Very new at this, so as far as possible keep any explanations in beginner-friendly language where possible.

WalderFrey
  • 575
  • 2
  • 17
  • 1
    You need [render to texture](https://www.google.com/search?name=f&hl=en&q=directx+render+to+texture). The principle is simple: you do usual drawing, but set conventional texture instead of back buffer as render target. But I believe that commercial apps (such as games) would rather prefer generating localized textures offline to avoid runtime cost (by using or writing some little texture generator tool). You could simplify font rendering things a bit by using [DirectX support library](https://directxtk.codeplex.com/) or with classic font libraries such as FreeType. – Ivan Aksamentov - Drop Dec 04 '15 at 13:17
  • 1
    Take a look at the [DirectX Tool KIt](https://github.com/Microsoft/DirectXTK/wiki/Getting-Started)'s tutorials, specifically the [Drawing text](https://github.com/Microsoft/DirectXTK/wiki/Drawing-text) lesson on ``SpriteFont`` and the [post processing](https://github.com/Microsoft/DirectXTK/wiki/Writing-custom-shaders) lesson which covers render-to-texture. – Chuck Walbourn Dec 04 '15 at 16:59
  • I think DirectXTK's drawing text is only supported SpriteBatch which is just in a 2d surface. We can't change the font texture rendering to 3D mode. – Aaron Lee May 27 '17 at 09:33

1 Answers1

0

I guess the cube textures do not change too frequently, since the language should not be switched to often by the user. In this case I would use a toolkit like Qt to generate the textures - similar like this:

  QImage image(width,height,QImage::Format_ARGB32);
  image.fill(Qt::transparent);

  QPainter painter;
  painter.begin(&image);
  painter.setRenderHints(QPainter::HighQualityAntialiasing
                 | QPainter::TextAntialiasing);
  painter.setFont(font);
  painter.setPen(Qt::black);

  painter.drawText(0, metric.ascent(), QString(c));
  painter.end();

To access the texture data:

auto* data = image.data();

A different solution would be to use Signed Distance Text Rendering. Doing this you need to render this text to a texture first (render to texture) and then apply it to the cube.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90