博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
托管DirectX,从MDX到SlimDX的转换(转)
阅读量:4664 次
发布时间:2019-06-09

本文共 12930 字,大约阅读时间需要 43 分钟。

转自:http://gis4all.ru/zh-CN/net/managed-directx/

开始迁移到托管DirectX SlimDX框架的,例如,MDX应用的帕特里克Murrisa地形的浏览器。

在托管DirectX代码所示,到新的代码,与SlimDX评论的形式。

 

MDX迁移项目中SlimDX

图书馆设置SlimDX“

下载并安装“  “。 

添加一个引用到项目中。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);
 

转载于:https://www.cnblogs.com/yuxiuting/archive/2013/03/21/2972963.html

你可能感兴趣的文章
hdu3436 splaytree树模拟队列+离散化缩点
查看>>
2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
查看>>
Windows下用C语言获取进程cpu使用率,内存使用,IO情况
查看>>
nandflash擦除、写操作的状态判断
查看>>
iOS照片缩略图thumbnail模糊问题
查看>>
Lua基础(一)
查看>>
有关使用百度编辑器Ueditor的问题
查看>>
定位决定人生成败
查看>>
ORACLE 创建新表
查看>>
设计模式---(创建型)Builder
查看>>
ceshi
查看>>
[转载] Android 2.3.3 API 读取通讯录中电话号码的实例
查看>>
[BZOJ1121] [POI2008] 激光发射器SZK
查看>>
Educational Codeforces Round 9 E. Thief in a Shop NTT
查看>>
【noip2016提高组day2T3】【愤怒的小鸟】状压dp转移时的集合包含
查看>>
python数据结构(三)
查看>>
WebGL-二
查看>>
My97DatePicker的使用
查看>>
在C#中获取IronPthon2.7异常时的调用方法堆栈,调试使用。
查看>>
Every Developer Should Download Now.
查看>>