天天看點

SharpDX之Direct2D教程I——簡單示例和Color(顔色)

下圖是官網截圖,SharpDX自诩是最好的DirectX的開發包(不僅僅包括Direct2D,還包括DirectX 3D等等)

SharpDX之Direct2D教程I——簡單示例和Color(顔色)
SharpDX之Direct2D教程I——簡單示例和Color(顔色)
SharpDX之Direct2D教程I——簡單示例和Color(顔色)

我在學習了一段時間後,感覺上,它的幫助文檔和MSDN相比,缺少了示例教程。雖然在論壇上,也有不少人提供了各自的示例代碼,但僅僅是代碼,而且顯得淩亂分散。示例代碼之間缺少連貫性,給學習者造成了障礙。

下面的截圖是調用Geometry對象的Combine方法的效果圖(也就是在前文中,Windows API Code Pack 1.1中出現的錯誤的地方)。

SharpDX之Direct2D教程I——簡單示例和Color(顔色)

于是決定用SharpDX來繼續Direct2D的系列教程

下載下傳開發包,并在項目中引用

在官網上下載下傳完整的安裝包(目前的版本是2.5.0),安裝後,可以找到如下的動态庫的目錄。可以看出支援.net 2.0到.net 4.0,支援WinRT、WP8等。

SharpDX之Direct2D教程I——簡單示例和Color(顔色)

本系列的教程選擇 DirectX11_1-net40 ,輕按兩下打開後,看到如下的目錄

SharpDX之Direct2D教程I——簡單示例和Color(顔色)

針對不同的内容,有不同的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 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之Direct2D教程I——簡單示例和Color(顔色)

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教程

    本文轉自萬倉一黍部落格園部落格,原文連結:http://www.cnblogs.com/grenet/p/3329567.html,如需轉載請自行聯系原作者

繼續閱讀