使用DX11渲染文本

4

我正在尝试在SlimDX中绘制屏幕上的文本。

初步研究并不鼓舞人心。我找到的唯一一个具体示例是这个:

http://www.aaronblog.us/?p=36

我正试图将其移植到我的代码中,但这证明非常困难,经过4个小时后,我开始怀疑这是否是正确的方法。

从根本上讲,似乎做一些简单的屏幕文本写入是相当困难的。目前看来,Aaron的方法是唯一实用的方法。没有其他比较点。

还有其他人能提供任何建议吗?

P.S. 我认为现在我可能已经为每个字母创建了单独的图像,并编写了将字符串转换为一系列精灵的程序。不过这似乎有点疯狂。

2个回答

4
渲染文本确实有些复杂。渲染一般文本的唯一方法是使用Direct2D / DirectWrite。这只支持DirectX10。因此,您必须创建一个DirectX 10设备、DirectWrite和Direct2D工厂。然后,您可以创建一个共享纹理,该纹理可被DirectX 10和11设备使用。
此纹理将包含渲染后的文本。Aaron使用此纹理将其与全屏混合。因此,您可以使用DirectWrite绘制完整的字符串。本质上,这就像绘制纹理四边形一样。
另一种方法是,如您所提到的,绘制精灵表并将字符串分成几个精灵。我认为后者速度会更快,但我还没有测试过。我在我的Sprite & Text engine中使用了这种方法。请参见AssertDeviceCreateCharTable方法。

谢谢Nico!看起来你在这方面比我更有优势。一旦我将Aaron的功能移植到我的框架中,我会更新我的自定义精灵类。8D - CdrTomalak

3

有一个很好的DirectWrite包装器叫做http://fw1.codeplex.com/

它是用c++编写的,但是为它制作混合模式包装器非常简单(这就是我为我的c#项目所做的)。

以下是一个简单的例子:

.h文件

#pragma once
#include "Lib/FW1FontWrapper.h"
using namespace System::Runtime::InteropServices;

public ref class DX11FontWrapper
{
public:
    DX11FontWrapper(SlimDX::Direct3D11::Device^ device);
    void Draw(System::String^ str,float size,int x,int y,int color);
private:
    SlimDX::Direct3D11::Device^ device;
    IFW1FontWrapper* pFontWrapper;
};

.cpp文件

#include "StdAfx.h"
#include "DX11FontWrapper.h"

DX11FontWrapper::DX11FontWrapper(SlimDX::Direct3D11::Device^ device)
{
    this->device = device;

    IFW1Factory *pFW1Factory;
    FW1CreateFactory(FW1_VERSION, &pFW1Factory);
    ID3D11Device* dev = (ID3D11Device*)device->ComPointer.ToPointer();

    IFW1FontWrapper* pw;

    pFW1Factory->CreateFontWrapper(dev, L"Arial", &pw); 
    pFW1Factory->Release();

    this->pFontWrapper = pw;
}

void DX11FontWrapper::Draw(System::String^ str,float size,int x,int y, int color)
{
    ID3D11DeviceContext* pImmediateContext = 
        (ID3D11DeviceContext*)this->device->ImmediateContext->ComPointer.ToPointer();

    void* txt = (void*)Marshal::StringToHGlobalUni(str);
    pFontWrapper->DrawString(pImmediateContext, (WCHAR*)txt, size, x, y, color, 0);
    Marshal::FreeHGlobal(System::IntPtr(txt));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接