[轉]託管DirectX,從MDX到SlimDX的轉換

來源:互聯網
上載者:User

標籤:

開始遷移到託管DirectX SlimDX架構的,例如,MDX應用的帕特裡克Murrisa地形的瀏覽器。

在託管DirectX代碼所示,到新的代碼,與SlimDX評論的形式。

 

MDX遷移項目中SlimDX圖書館設定SlimDX“

下載並安裝“ 2010年2月SlimDX SDK(微星)。 “。 
添加一個引用到項目中。NET庫“C:\ Program Files檔案\ SlimDX SDK(2010年2月)\ BIN \ X86 \ SlimDX.dll。 如果你添加了一個連結的添加引用對話方塊中,不通過標籤“。NET”和一個“瀏覽”,然後選擇“連結屬性複製到本機 - >真實而具體的版本 - >真實的。

     / /使用Microsoft.DirectX.Direct3D;     / /使用Microsoft.DirectX的;     / /使用Microsoft.DirectX.DirectInput;        使用SlimDX;    使用SlimDX.Direct3D9;    使用鍵= SlimDX.DirectInput.Key;    使用KeyboardState = SlimDX.DirectInput.KeyboardState;    使用鍵盤時,SlimDX.DirectInput.Keyboard;    採用的DirectInput = SlimDX.DirectInput.DirectInput;

為什麼不直接使用使用SlimDX.DirectInput,? 可以,但隨後會發生衝突,有一流的裝置,這是在兩個命名空間。

使用類SlimDX.Direct3D9
     / /私人Microsoft.DirectX.Direct3D.Device _device;     / /私人Microsoft.DirectX.DirectInput.Device _Keyboard,;     / /的私人Microsoft.DirectX.Direct3D.Font _font;    專用裝置_device;    私人的Direct3D _direct3d;    私人FilterCaps _textureFilterCaps;    私人DirectInput的_directInput;    私人鍵盤_keyb;    私人SlimDX.Direct3D9.Font _font;
建立SlimDX.Direct3D9.Device

只顯示需要轉換的代碼。

     / /建立裝置     PresentParameters presentParams =的新PresentParameters();     / / PresentParams.AutoDepthStencilFormat的= DepthFormat.D16;     / /裝置新裝置(0,DeviceType.Hardware,的,CreateFlags.HardwareVertexProcessing,presentParams);     presentParams.AutoDepthStencilFormat = Format.D16;     _direct3d新的Direct3D();     _device新的裝置(_direct3d,0,DeviceType.Hardware,this.Handle,CreateFlags.HardwareVertexProcessing,presentParams);
裝置事件
     / / Device.DeviceReset的+ =的EventHandler(OnDeviceReset)的;     / / Device.DeviceResizing的+ =:新CancelEventHandler(OnDeviceResizing);

類似物目前尚未發現 

參數設定渲染裝置

簡單類型:

     / /渲染設定     / / Device.RenderState.ZBufferEnable的= TRUE;     / / Device.RenderState.Ambient的ambientColor;     / / Device.RenderState.FogStart的mapWidth == 0?  60:(浮動)mapWidth“/ 8;     device.SetRenderState(RenderState.ZEnable,TRUE); / / BOOL     device.SetRenderState(RenderState.Ambient,ambientColor.ToArgb()); / / int(COLOR4類型資料)     device.SetRenderState(RenderState.FogStart,mapWidth == 0×60:(浮動)mapWidth / 8); / / INT

等等

類型的參數:

     / / Device.RenderState.FillMode的FillMode.Solid;     / / Device.RenderState.CullMode的Cull.CounterClockwise;     / / Device.RenderState.SourceBlend的Blend.SourceAlpha;     / / Device.RenderState.FogTableMode的FogMode.Linear;     device.SetRenderState   (RenderState.FillMode,FillMode.Solid);     device.SetRenderState  (RenderState.CullMode,Cull.Counterclockwise); device.SetRenderState   (RenderState.SourceBlend,Blend.SourceAlpha); device.SetRenderState   (RenderState.FogTableMode,FogMode.Linear); 
其他參數的行動裝置
     / /紋理過濾     / / Device.SamplerState [0]。MinFilter = TextureFilter.Anisotropic;     / / Device.SamplerState [0]。(MagFilter = TextureFilter.Linear);     / / Device.SamplerState [0]。AddressU = TextureAddress.Clamp;     / / Device.SamplerState [0]。AddressV = TextureAddress.Clamp;     device.SetSamplerState(0,SamplerState.MinFilter,TextureFilter.Anisotropic);     device.SetSamplerState(0,SamplerState.MagFilter,TextureFilter.Linear);     device.SetSamplerState(0,SamplerState.AddressU,TextureAddress.Clamp);     device.SetSamplerState(0,SamplerState.AddressV,TextureAddress.Clamp);     / /紋理階段狀態。     / / Device.TextureState的[0]。ColorArgument2 = TextureArgument.Diffuse;     / / Device.TextureState的[0]。AlphaArgument1 = TextureArgument.TextureColor;     / / Device.TextureState的[0]。ColorOperation = TextureOperation.Modulate;     / / Device.TextureState的[0]。AlphaOperation = TextureOperation.SelectArg1;     ·device.SetTextureStageState(0,TextureStage.ColorArg2的,TextureArgument.Diffuse);     device.SetTextureStageState(0,TextureStage.AlphaArg1,TextureArgument.Texture);     device.SetTextureStageState(0,TextureStage.ColorOperation,TextureOperation.Modulate);     device.SetTextureStageState(0,TextureStage.AlphaOperation型,TextureOperation.SelectArg1);
device.DeviceCaps

所有類型的檢查:

     / /檢查硬體頂點處理和純裝置。     / /如果(device.DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic的)...     / /如果(device.DeviceCaps.TextureFilterCaps.SupportsMinifyLinear的)...     / /如果(device.DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic的)...     / /如果(device.DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear的)...

切換到這樣的設計:

    的能力deviceCaps = _direct3d.GetDeviceCaps(0,DeviceType.Hardware);     FilterCaps textureFilterCaps deviceCaps.TextureFilterCaps;    如果((textureFilterCaps FilterCaps.MinAnisotropic)= 0)...    如果((textureFilterCaps FilterCaps.MinLinear)= 0)...    如果((textureFilterCaps FilterCaps.MagAnisotropic)= 0)...    如果((textureFilterCaps FilterCaps.MagLinear)= 0)...
簡單類型
    顏色 - > SlimDX.Color4     / / Material.SpecularSharpness = 30.0f; / /薄亮點(小=大)     material.Power = 30.0f; / /薄亮點(小=大)
向量
     / /三維向量射線Vector3.Empty;    三維向量射線Vector3.Zero;
向量。 Vector3.Unproject(...)
    三維向量p1的=新的三維向量(的x,y,的MinZ);     ...     / / P1.Unproject(device.Viewport,device.Transform.Projection,device.Transform.View,device.Transform.World);

這條線的設計:

    漂浮的MinZ = device.Viewport.MinZ;    浮動maxZ = device.Viewport.MaxZ;    寬度= device.Viewport.Width;    高度= device.Viewport.Height;     VX = device.Viewport.X;    詮釋VY = device.Viewport.Y;    矩陣worldViewProjection:= device.GetTransform(TransformState.Projection)* device.GetTransform(TransformState.View)* device.GetTransform(TransformState.World);     P1 = Vector3.Unproject(P1,VX,VY,寬度,高度的MinZ,maxZ,worldViewProjection);
向量。 Vector3.Scale(浮動)
     / / Ray.Scale的((浮動)U);    射線Vector3.Multiply(射線,(浮動)U);
向量。 矩陣轉換
    三維向量POS =新的三維向量();    和矩陣lightTrans = Matrix.Identity;     ...     / / Pos.TransformCoordinate的(lightTrans);     POS = Vector3.TransformCoordinate(POS,lightTrans);
燈光設定
     / /燈光設定    私人無效LightSetup()     {         / /使用白色,定向光線          / / Device.Lights [0]。漫= Color.White;         / / Device.Lights [0]。鏡面= Color.FromArgb(0X80,0X80,0X80)/ /軟亮點         / / Device.Lights [0]。類型= LightType.Directional;            燈光燈();         light.Diffuse新Color4(Color.White);         light.Specular =的新Color4(0.5F,0.5F,0.5F); / /軟亮點         light.Type = LightType.Directional;             / / Device.SetLight的(0,光); / /(*** 1)             / /計算光線的方向(三維向量名次)         ...             / / Device.Lights的[0]。方向=-軸位置;         / / Device.Lights [0]。更新(); / /不是,更新,使用EnableLight()         / / Device.Lights的[0]。啟用= TRUE;             light.Direction =-POS;         device.SetLight(0亮); / /搬到了這裡(*** 1)         device.EnableLight(0,TRUE);             ...     }
紋理紋理載入
     / /紋理質地TextureLoader.FromFile(裝置,檔案名稱);    質感紋理Texture.FromFile(裝置,檔案名稱);    質感紋理Texture.FromStream(裝置,流);    但這種方法相對應的,直到我找到(建立紋理位元影像):     / /紋理T =的紋理(裝置,B,0,Pool.Managed);
使用鍵盤初始化
     / /鍵盤KEYB新Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);     / / Keyb.SetCooperativeLevel(這一點,CooperativeLevelFlags.Background CooperativeLevelFlags.NonExclusive);     / / Keyb.Acquire的();     _directInput =新SlimDX.DirectInput.DirectInput();     _keyb =新鍵盤(_directInput)的;     _keyb.SetCooperativeLevel(這一點,SlimDX.DirectInput.CooperativeLevel.Background | SlimDX.DirectInput.CooperativeLevel.Nonexclusive);     _keyb.Acquire();
閱讀
     / / KeyboardState的的鍵= keyb.GetCurrentKeyboardState();     / / BOOL轉變的=鍵[Key.LeftShift] | |鍵[Key.RightShift];     / / BOOL CTRL =鍵[Key.LeftControl] | |鍵[Key.RightControl];
     / /如果Ctrl鍵(鍵[Key.L] &&!移&&!)                  (keys.IsPressed(Key.L)&&!轉變&&!CTRL)
安裝攝像機
     / / _device.Transform.Projection的Matrix.PerspectiveFovLH(FOV的aspectRatio,mapWidth == 0?15F:(浮動)(mapWidth / 30F),mapWidth == 0?5000F:(浮動)(mapWidth * 3));     / / _device.Transform.View的= Matrix.LookAtLH(新的三維向量(0,0區),新的三維向量(0,0,0),新的三維向量(1,0,0));        矩陣perspectiveFovLH Matrix.PerspectiveFovLH(FOV的aspectRatio,mapWidth == 0?15樓:(浮動)(mapWidth / 30F),mapWidth == 0?5000F:(浮動)(mapWidth * 3));    和矩陣lookAtLH = Matrix.LookAtLH(新三維向量(0,0區),新的三維向量(0,0,0),新的三維向量(1,0,0));         _device.SetTransform(TransformState.Projection,perspectiveFovLH);     _device.SetTransform(TransformState.View,lookAtLH);
網格
     / /網目網(numFaces,numVertices,MeshFlags.Managed,CustomVertex.PositionNormalTextured.Format,裝置);    網目=新的網(的裝置,numFaces,numVertices,MeshFlags.Managed,CustomVertex.PositionNormalTextured.Format);
mesh.VertexBuffer

非常重要的區別。 在SlimDX,我們使用線程,和MDX的System.Array。

     / / INT []行列=新的int [1]; / /行列[0] = mesh.NumberVertices / /行列[0] = mesh.VertexCount / / System.Array中ARR = mesh.VertexBuffer.Lock(0, ,LockFlags.None typeof運算(CustomVertex.PositionNormalTextured),職級); / /(Y = startY,Y <=恩迪,Y + +)/ / {/ /(詮釋x = STARTX; X <= endX; X + +)/ / {/ / CustomVertex.PositionNormalTextured PNT新CustomVertex.PositionNormalTextured(); / / ...  / / Arr.SetValue的(PNT,vertIndex + +)/ /} / /} / / mesh.VertexBuffer.Unlock(); / /尺寸= sizeof(CustomVertex.PositionNormalTextured)的mesh.VertexCount使用(資料流流= mesh.VertexBuffer。鎖(0,mesh.VertexBuffer.Description.SizeInBytes,LockFlags.None)){CustomVertex.PositionNormalTextured [] ARR =新CustomVertex.PositionNormalTextured [mesh.VertexCount](詮釋y = startY; <= ENDY; Y + +){ (X = startx時,X <= endX,X + +){CustomVertex.PositionNormalTextured PNT =的新CustomVertex.PositionNormalTextured();“...  arr.SetValue(PNT,vertIndex + +);}} stream.WriteRange   (ARR)的mesh.VertexBuffer.Unlock();} 
CUSTOMVERTEX

類是在CUSTOMVERTEX MDX,但不,在SlimDX,由於在DirectX缺乏直接對應的。 這裡不存在一個獨特的解決方案,所以我共用的最終版本。 你只需要建立一個定義良好的結構形式,但是建立一個類CUSTOMVERTEX的 - 你的情況,我建立的代碼相容MDX。 內部的內容也可以實現的結構,以不同的方式,載體,或簡單的資料類型。

    使用系統;    使用System.Runtime.InteropServices;    使用SlimDX;    使用SlimDX.Direct3D9;    命名空間TerrainViewer     {        公用結構CUSTOMVERTEX         {             / /這句話強制的位元組的順序被儲存在GPU的期望資料             [StructLayout(LayoutKind.Sequential)]            的公用結構PositionNormalTextured / / VertexTypePNT             {                公眾持股量X; / /位置                公眾持股量Y; / /位置                公眾持股量Z; / /位置                公眾持股量Xn的; / /普通                公眾持股量YN / /正常                公眾持股量的鋅,/ /普通                公眾持股量塗; / /質地位置(德克薩斯州)                公眾持股量電視,/ /紋理的位置(TY)                    公用常量VertexFormat格式= VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture1;                     / /選項                 / /公用的三維向量的位置;                 / /公用的三維向量正常;                 / /公用的Vector2 TexturePosition的;                 / /公用的靜態詮釋SizeBytes的{{返回Marshal.SizeOf(typeof運算(PositionNormalTextured));}}             }                 / /這句話強制的位元組的順序被儲存在GPU的期望資料             [StructLayout(LayoutKind.Sequential)]            公用,結構PositionNormalColored / / VertexTypePNT             {                公眾持股量X; / /位置                公眾持股量Y; / /位置                公眾持股量Z; / /位置                公眾持股量Xn的; / /普通                公眾持股量YN / /正常                公眾持股量的鋅,/ /普通                公眾詮釋的顏色; / /顏色                    公用常量VertexFormat格式= VertexFormat.Position | VertexFormat.Normal | VertexFormat.Diffuse;                     / /選項                 / /公用的三維向量的位置;                 / /公用的三維向量正常;                 / /公用COLOR4顏色/ / MDX                 / /公用的靜態詮釋SizeBytes的{{返回Marshal.SizeOf(typeof運算(PositionNormalColored));}}             }         }     }
字形
     / / /      / / /建立一個字型。     / / /      / / HTTP :/ / www.gamedev.net/community/forums/topic.asp?topic_id=557531     / /的公用Microsoft.DirectX.Direct3D.Font的CreateFont(字串原諒我,浮動emSize,System.Drawing.FontStyle風格)    公用SlimDX.Direct3D9.Font的CreateFont(字串原諒我,浮動emSize,System.Drawing.FontStyle風格)     {        嘗試         {             FontDescription的描述=新的FontDescription的();             description.FaceName familyName;             / / Description.Height的=(int)的(1.9 * emSize的);             / / Description.Quality的FontQuality.ClearTypeNatural;            如果(style! = System.Drawing.FontStyle.Regular)             {                 / /如果((風格與System.Drawing.FontStyle.Italic)!= 0)description.IsItalic = TRUE;                 ((風格與System.Drawing.FontStyle.Italic)!= 0)description.Italic = TRUE;                    如果((風格與System.Drawing.FontStyle.Bold)= 0)description.Weight,FontWeight.Heavy;                     / / Description.Quality的FontQuality.AntiAlias??ed / / MDX                 description.Quality FontQuality.Antialiased;             }                 / / FONT =新SlimDX.Direct3D9.Font(裝置,15,0,FontWeight.Bold,0,假的,             / / CharacterSet.Default的,Precision.TrueType,FontQuality.ClearTypeNatural,             / / PitchAndFamily.Default的PitchAndFamily.DontCare,“宋體”);                返回新SlimDX.Direct3D9.Font(INT)(裝置,(1.9 * emSize),0,FontWeight.Bold,0,假的,             CharacterSet.Default,Precision.TrueType,FontQuality.Antialiased,             PitchAndFamily.Default PitchAndFamily.DontCare,原諒我);                 / /返回新Microsoft.DirectX.Direct3D.Font(裝置描述); / / MDX             / / System.Drawing.Font的F =新System.Drawing.Font(新的FontFamily的(原諒我),emSize);             / /返回新SlimDX.Direct3D9.Font的(裝置,F);         }        抓         {            返回null;         }     }
繪製文本
     / / Sprite.Begin(SpriteFlags.None);     / / Font.DrawText的(雪碧,資訊文本,矩形,DrawTextFormat.Left,Color.Black);     sprite.Begin(SpriteFlags.AlphaBlend);     font.DrawString(雪碧,資訊文本,矩形,DrawTextFormat.Left,Color.Black);
原文連結:http://gis4all.ru/zh-CN/net/managed-directx/

[轉]託管DirectX,從MDX到SlimDX的轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.