研究Direct2D已经有一段时间了,也写了一个系列的文章 Direct2D ,是基于Windows API Code Pack 1.1。在前文 Direct2D教程VIII——几何(Geometry)对象的运算,本系列的终结篇 中介绍,由于Windows API Code Pack 1.1有错误问题,加上长时间没有更新(可以看出是2010年推出的),于是终止了该系列的教程。
在网上寻寻觅觅Windows API Code Pack 1.1的替代品。找到了SharpDX。SharpDX官网:http://www.sharpdx.org/。
下图是官网截图,SharpDX自诩是最好的DirectX的开发包(不仅仅包括Direct2D,还包括DirectX 3D等等)
我在学习了一段时间后,感觉上,它的帮助文档和MSDN相比,缺少了示例教程。虽然在论坛上,也有不少人提供了各自的示例代码,但仅仅是代码,而且显得凌乱分散。示例代码之间缺少连贯性,给学习者造成了障碍。
下面的截图是调用Geometry对象的Combine方法的效果图(也就是在前文中,Windows API Code Pack 1.1中出现的错误的地方)。
于是决定用SharpDX来继续Direct2D的系列教程
下载开发包,并在项目中引用
在官网上下载完整的安装包(目前的版本是2.5.0),安装后,可以找到如下的动态库的目录。可以看出支持.net 2.0到.net 4.0,支持WinRT、WP8等。
本系列的教程选择 DirectX11_1-net40 ,双击打开后,看到如下的目录
针对不同的内容,有不同的DLL文件。本系列的文章主要是介绍Direct2D,因此只要在项目中引入SharpDX.DLL、SharpDX.DXGI.DLL、SharpDX.Direct2D1.DLL即可。
下面是基本的示例代码,把Direct2D的绘制的内容绘制到控件上。后面的示例类都是继承于此类。
Imports DX = SharpDX
Imports D2D = SharpDX.Direct2D1
Imports WIC = SharpDX.WIC
Imports DDW = SharpDX.DirectWrite
Imports DXGI = SharpDX.DXGI
Public Class clsSharpDXSampleBase
Protected _D2DFactory As D2D.Factory
Protected _RenderTarget As D2D.WindowRenderTarget
Public Sub CreateDeviceResource(Target As Control)
_D2DFactory = New D2D.Factory
Dim P As New D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Ignore)
Dim H As New D2D.HwndRenderTargetProperties
H.Hwnd = Target.Handle
H.PixelSize = New DX.Size2(Target.Width, Target.Height)
H.PresentOptions = SharpDX.Direct2D1.PresentOptions.None
Dim R As New D2D.RenderTargetProperties(D2D.RenderTargetType.Hardware, _
P, 0, 0, _
D2D.RenderTargetUsage.None, _
D2D.FeatureLevel.Level_DEFAULT)
_RenderTarget = New D2D.WindowRenderTarget(_D2DFactory, R, H)
End Sub
Public Sub Render()
If Not _renderTarget Is Nothing Then
_RenderTarget.BeginDraw()
_RenderTarget.Clear(DX.Color.BlueViolet.ToColor4)
_RenderTarget.EndDraw()
End If
End Sub
End Class
和Windows API Code Pack1.1类似,先定义Factory对象,再定义RenderTarget对象的各个参数。PixelFormat对象定义像素格式和Alpha模式;HwndRenderTargetProperties对象定义了绑定控件的Hwnd和Size及PresentOptions(呈现方式);RenderTargetProperties对象定义了呈现模式(Hardware,硬件级;Software,软件级;WIC不支持硬件级),像素模式(PixelFormat),DpiX和DpiY(0表示采用系统的默认值),RenderTargetUsage和FeatureLevel(这两个参数都用默认值)
不同的是,Windows API Code Pack 1.1中用的是Factory对象的CreateHwndRenderTarget方法创建RenderTarget,而SharpDX用RenderTarget对象的New方法创建RenderTarget对象,习惯就好,无所谓优劣。(类似的,SharpDX用对象的New方法对应很多的Create****方法)
下图是上面示例代码的效果图
SharpDX中的颜色
在SharpDX中,对颜色对象加强了功能。用Color、Color3、Color4对象表示颜色。
其中,Color4对象是基础的颜色对象,在使用颜色的场合中,用的都是Color4对象。Color4对象用Red、Green、Blue、Alpha分量表示颜色。下面是Color4的一些方法与函数的原型定义
Public Sub New(value As Single)
Public Sub New(red As Single, green As Single, blue As Single, alpha As Single)
Public Sub New(value As DX.Vector4)
Public Sub New(value As DX.Vector3, alpha As Single)
Public Sub New(rgba As UInteger)
Public Sub New(rgba As Integer)
Public Sub New(values As Single())
Public Sub New(color As DX.Color3)
Public Sub New(color As DX.Color3, alpha As Single)
Public Function ToBgra() As Integer
Public Sub ToBgra(ByRef r As Byte, ByRef g As Byte, ByRef b As Byte, ByRef a As Byte)
Public Function ToRgba() As Integer
Public Function ToVector3() As DX.Vector3
Public Function ToVector4() As DX.Vector4
Public Function ToArray() As Single()
Public Shared Sub Add(ByRef left As DX.Color4, ByRef right As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Add(left As DX.Color4, right As DX.Color4) As DX.Color4
Public Shared Sub Subtract(ByRef left As DX.Color4, ByRef right As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Subtract(left As DX.Color4, right As DX.Color4) As DX.Color4
Public Shared Sub Modulate(ByRef left As DX.Color4, ByRef right As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Modulate(left As DX.Color4, right As DX.Color4) As DX.Color4
Public Shared Sub Scale(ByRef value As DX.Color4, scale As Single, ByRef result As DX.Color4)
Public Shared Function Scale(value As DX.Color4, scale As Single) As DX.Color4
Public Shared Sub Negate(ByRef value As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Negate(value As DX.Color4) As DX.Color4
Public Shared Sub Clamp(ByRef value As DX.Color4, ByRef min As DX.Color4, ByRef max As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Clamp(value As DX.Color4, min As DX.Color4, max As DX.Color4) As DX.Color4
Public Shared Sub Lerp(ByRef start As DX.Color4, ByRef [end] As DX.Color4, amount As Single, ByRef result As DX.Color4)
Public Shared Function Lerp(start As DX.Color4, [end] As DX.Color4, amount As Single) As DX.Color4
Public Shared Sub SmoothStep(ByRef start As DX.Color4, ByRef [end] As DX.Color4, amount As Single, ByRef result As DX.Color4)
Public Shared Function SmoothStep(start As DX.Color4, [end] As DX.Color4, amount As Single) As DX.Color4
Public Shared Sub Max(ByRef left As DX.Color4, ByRef right As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Max(left As DX.Color4, right As DX.Color4) As DX.Color4
Public Shared Sub Min(ByRef left As DX.Color4, ByRef right As DX.Color4, ByRef result As DX.Color4)
Public Shared Function Min(left As DX.Color4, right As DX.Color4) As DX.Color4
Public Shared Sub AdjustContrast(ByRef value As DX.Color4, contrast As Single, ByRef result As DX.Color4)
Public Shared Function AdjustContrast(value As DX.Color4, contrast As Single) As DX.Color4
Public Shared Sub AdjustSaturation(ByRef value As DX.Color4, saturation As Single, ByRef result As DX.Color4)
Public Shared Function AdjustSaturation(value As DX.Color4, saturation As Single) As DX.Color4
从上面的原型定义可以看出,Color4增加了很多有意思的方法,解释如下:
Add:两个颜色相加,各个分量分别相加,等同于运算符 + 。
Subtract:两个颜色相减,各个分量分别相减,等同于运算符 – 。
Modulate:两个颜色相乘,各个分量分别相乘,等同于运算符 * 。
Sacle:缩放颜色,颜色的每个分量分别和参数scale相乘
Negate:取颜色的反色,用1减去每个颜色的分量
Clamp:限定颜色分量的范围,如果分量小于Min的分量,则取Min的分量;如果分量大于Max分量,则取Max分量
Lerp:按比例取两个颜色之间的插值部分,参数amount表示比例
SmoothStep:按比例取两个颜色之间的插值部分,参数amount表示比例。和Lerp相比,Lerp是线性插值,SmoothStep是二次插值
Max:各取两个颜色分量的最大值的颜色
Min:各取两个颜色分量的最小值的颜色
AdjustContrast:调节对比度,参数contrast是对比度比例参数
AdjustSaturation:调节饱和度,参数Saturation是饱和度比例参数
看了代码后,发现Color4并没有对Red、Green、Blue、Alpha分量进行限制(正常值是0-1),这样在用上面的方法时,很容易出现分量超出范围的情况(小于0,大于1)。但是,似乎系统内部进行了处理,在实际使用时,超出的部分会自动处理成0或1(小于0处理成0,大于1处理成1)。
Color对象和Color4对象类似,分量是用Byte表示(0-255);Color3对象用Red、Green、Blue分量表示颜色。
Color对象和Color3对象都有ToColor4方法转换为Color4对象
Color对象还包含了众多内置静态的颜色,如BlueViolet等,这样就可以很方便的用英文来表示颜色,而不需要记住这些颜色对应的Integer的值。
从上面的来看,SharpDX的确要比Windows API Code Pack 1.1方便了很多。
接下来,会陆续把前文中的例子转换为SharpDX后,再出新的SharpDX教程