简体   繁体   English

在DirectX 11中将文本绘制为2D纹理?

[英]Drawing text to a 2D texture in DirectX 11?

I'd like to be able to apply a transparent texture to each face of a simple 3D cube. 我希望能够在一个简单的3D立方体的每个面上应用透明纹理。 Each texture is simply the name of the face, eg "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. 每个纹理都只是面部的名称,例如“Front”,“Back”,“Left”等。通常我只是在绘画应用程序中创建一个图像,然后保存它,并将图像作为纹理加载到运行。 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. 所以理想的解决方案是能够在运行时将文本字符串渲染为2D纹理 (使用我选择的任何字体和颜色),然后将每个表面上的结果纹理渲染为透明覆盖。 Can this be done relatively easily? 这可以相对容易地完成吗?

eg this sort of process: 例如这种过程:

文本 - 纹理到立方体

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. 这是将文本(字符串)渲染到纹理(ID3D11Texture2D),这是一个棘手的部分。 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. 这是非常新的,所以尽可能用初学者友好的语言保留任何解释。

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: 在这种情况下,我会使用像Qt这样的工具包来生成纹理 - 类似这样:

  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. 这样做需要先将此文本渲染到纹理(渲染到纹理),然后将其应用到多维数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM