SharpDX之Direct2D教程I——简单示例和Color(颜色)

研究Direct2D已经有一段时间了,也写了一个系列的文章 Direct2D ,是基于Windows API Code Pack 1.1。在前文 Direct2D教程VIII——几何(Geometry)对象的运算,本系列的终结篇 中介绍,因为Windows API Code Pack 1.1有错误问题,加上长时间没有更新(能够看出是2010年推出的),因而终止了该系列的教程。html

 

在网上寻寻觅觅Windows API Code Pack 1.1的替代品。找到了SharpDX。SharpDX官网:http://www.sharpdx.org/函数

下图是官网截图,SharpDX自夸是最好的DirectX的开发包(不单单包括Direct2D,还包括DirectX 3D等等)学习

 

我在学习了一段时间后,感受上,它的帮助文档和MSDN相比,缺乏了示例教程。虽然在论坛上,也有很多人提供了各自的示例代码,但仅仅是代码,并且显得凌乱分散。示例代码之间缺乏连贯性,给学习者形成了障碍。spa

 

下面的截图是调用Geometry对象的Combine方法的效果图(也就是在前文中,Windows API Code Pack 1.1中出现的错误的地方)。.net

image

 

因而决定用SharpDX来继续Direct2D的系列教程3d

 

下载开发包,并在项目中引用orm

在官网上下载完整的安装包(目前的版本是2.5.0),安装后,能够找到以下的动态库的目录。能够看出支持.net 2.0到.net 4.0,支持WinRT、WP8等。htm

image

本系列的教程选择 DirectX11_1-net40 ,双击打开后,看到以下的目录对象

image

针对不一样的内容,有不一样的DLL文件。本系列的文章主要是介绍Direct2D,所以只要在项目中引入SharpDX.DLL、SharpDX.DXGI.DLL、SharpDX.Direct2D1.DLL便可。blog

 

下面是基本的示例代码,把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****方法)

下图是上面示例代码的效果图

image

 

 

SharpDX中的颜色

在SharpDX中,对颜色对象增强了功能。用Color、Color三、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教程

相关文章
相关标签/搜索