研究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
因而決定用SharpDX來繼續Direct2D的系列教程3d
下載開發包,並在項目中引用orm
在官網上下載完整的安裝包(目前的版本是2.5.0),安裝後,能夠找到以下的動態庫的目錄。能夠看出支持.net 2.0到.net 4.0,支持WinRT、WP8等。htm
本系列的教程選擇 DirectX11_1-net40 ,雙擊打開後,看到以下的目錄對象
針對不一樣的內容,有不一樣的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****方法)
下圖是上面示例代碼的效果圖
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教程