[Go] Managed DirectX, conversion from MDX to SLIMDX

Source: Internet
Author: User

Start migrating to a browser that hosts the DirectX SLIMDX Framework, for example, the MDX app's Patrick Murrisa terrain.

As shown in the managed DirectX code, go to the new code, in the form of a slimdx comment.

SLIMDX Library Settings Slimdx "in MDX Migration project

Download and install the February 2010 slimdx SDK (MSI). “。
Add a reference to the project. NET Library "c: \ Program Files \ slimdx SDK (February 2010) \ BIN \ X86 \ SlimDX.dll. If you add a link to the Add Reference dialog box, do not pass the label ". NET "and a" browse "and then select" Link properties copied to this machine-> real and specific versions-> real. "

     //use Microsoft.DirectX.Direct3D;     //use of microsoft.directx;     //use Microsoft.DirectX.DirectInput;        Use SLIMDX;    Use Slimdx.direct3d9;    Use key = SlimDX.DirectInput.Key;    Use keyboardstate = SlimDX.DirectInput.KeyboardState;    When using the keyboard, SlimDX.DirectInput.Keyboard;    The DirectInput used = SlimDX.DirectInput.DirectInput;

Why not use Slimdx.directinput directly,? Yes, but then there will be a conflict, there is a first-class device, which is in two namespaces.

Using Class Slimdx.direct3d9
     //Private Microsoft.DirectX.Direct3D.Device _device;     //Private Microsoft.DirectX.DirectInput.Device _keyboard,;     //Private Microsoft.DirectX.Direct3D.Font _font;    Special Equipment _device;    Private Direct3D _direct3d;    Private Filtercaps _texturefiltercaps;    The _directinput of private DirectInput;    Private keyboard _keyb;    Private SlimDX.Direct3D9.Font _font;
Create SlimDX.Direct3D9.Device

Displays only the code that needs to be converted.

     //Create device     presentparameters presentparams = new PresentParameters ();     //Presentparams.autodepthstencilformat = Depthformat.d16;     //Equipment New equipment (0,devicetype.hardware,, createflags.hardwarevertexprocessing,presentparams);     Presentparams.autodepthstencilformat = Format.d16;     _direct3d new Direct3D ();     _device new Equipment (_direct3d,0,devicetype.hardware,this.handle,createflags.hardwarevertexprocessing,presentparams);
Device Events
     //Device.devicereset + = EventHandler (OnDeviceReset);     //Device.deviceresizing + =: New CancelEventHandler (ondeviceresizing);

The analogues have not been found yet.

Parameter setting rendering device

Simple Type:

     //Render settings     //Device.RenderState.ZBufferEnable = TRUE;     //Device.RenderState.Ambient of Ambientcolor;     //Device.RenderState.FogStart Mapwidth = = 0?  60: (floating) Mapwidth "/8;     Device. SetRenderState (renderstate.zenable,true); //BOOL     device. SetRenderState (Renderstate.ambient,ambientcolor.toargb ()); //INT (COLOR4 type data)     device. SetRenderState (Renderstate.fogstart,mapwidth = = 0x60: (floating) MAPWIDTH/8); //INT

Wait a minute

Types of parameters:

     //Device.RenderState.FillMode of Fillmode.solid;     //Device.RenderState.CullMode of cull.counterclockwise;     //Device.RenderState.SourceBlend of Blend.sourcealpha;     //Device.RenderState.FogTableMode of fogmode.linear;     Device. SetRenderState   (renderstate.fillmode,fillmode.solid);     Device. SetRenderState  (renderstate.cullmode,cull.counterclockwise); device. SetRenderState   (renderstate.sourceblend,blend.sourcealpha); device. SetRenderState   
Other parameters for mobile devices
     //Texture Filter//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);     //texture stage status. //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 type, TEXTUREOPERATION.SELECTARG1);
Device. DeviceCaps

All types of checks:

     //Check hardware vertex processing and pure devices.     //if (device. DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic) ...     //if (device. DeviceCaps.TextureFilterCaps.SupportsMinifyLinear) ...     //if (device. DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic) ...     //if (device. DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear) ...

Switch to a design like this:

    The ability to DeviceCaps = _direct3d. GetDeviceCaps (0,devicetype.hardware);     Filtercaps texturefiltercaps devicecaps.texturefiltercaps;    if ((texturefiltercaps filtercaps.minanisotropic) = 0)    ... if ((texturefiltercaps filtercaps.minlinear) = 0)    ... if ((texturefiltercaps filtercaps.maganisotropic) = 0)    ... if ((texturefiltercaps filtercaps.maglinear) = 0) ...
Simple Type
    Color-> Slimdx.color4     //material.specularsharpness = 30.0f;//thin highlight (small = Large)     Material. Power = 30.0f; //Thin Highlights (small = Large)
Vector
     //three-dimensional vector ray Vector3.empty;    Three-dimensional vector Ray Vector3.zero;
Vector. Vector3.unproject (... )
    Three-dimensional vector p1 = new three-dimensional vector (x, y, Minz);     ...     //P1. Unproject (Device.viewport,device.transform.projection,device.transform.view,device.transform.world);

The design of this line:

    Floating Minz = device. Viewport.minz;    Floating MAXZ = device. VIEWPORT.MAXZ;    width = device. Viewport.width;    Height = device. Viewport.height;     VX = device. Viewport.x;    Annotation VY = device. Viewport.y;    Matrix worldviewprojection:= device. GetTransform (transformstate.projection) * device. GetTransform (transformstate.view) * device. GetTransform (Transformstate.world);     P1 = Vector3.unproject (p1,vx,vy, width, height of minz,maxz,worldviewprojection);
Vector. Vector3.scale (floating)
     //Ray.scale ((floating) U);    Ray Vector3.multiply (Ray, (floating) U);
Vector. Matrix Conversion
    Three-dimensional vector pos = new three-dimensional vector ();    and matrix Lighttrans = matrix.identity;     ...     //Pos.transformcoordinate (Lighttrans);     POS = Vector3.transformcoordinate (Pos,lighttrans);
Lighting Settings
     //Light settings    Private Invalid Lightsetup ()     {         //use white, directional light          //device.lights [0]. diffuse = Color.White;         //device.lights [0]. Mirror = Color.FromArgb (0x80,0x80,0x80)//Soft Highlight         //device.lights [0]. Type = lighttype.directional;            Light ();         Light. Diffuse new Color4 (color.white);         Light. specular = new Color4 (0.5f,0.5f,0.5f); //Soft Highlights light         . Type = lighttype.directional;             //device.setlight (0, light); //(* * * 1)             //calculate the direction of light (three-dimensional vector rank) ...             //device.lights [0]. Direction =-axis position;         //device.lights [0]. Update (); //Not, updated, using Enablelight ()         //device.lights [0]. Enabled = TRUE;             Light. Direction =-pos;         Device. SetLight (0 bright); //Moved here (* * * 1)         device. Enablelight (0,true);             ...     }
Texture Texture Loading
     //Texture texture Textureloader.fromfile (device, file name);    Textured texture Texture.fromfile (device, file name);    Textured texture Texture.fromstream (device, stream);    But this method corresponds until I find (create texture bitmap):     //texture T = texture (device, b,0,pool.managed);
Class with the keyboard.
     //Keyboard keyb new Microsoft.DirectX.DirectInput.Device (Systemguid.keyboard);     //Keyb.setcooperativelevel (this point, Cooperativelevelflags.background cooperativelevelflags.nonexclusive);     //Keyb.acquire ();     _directinput = new SlimDX.DirectInput.DirectInput ();     _keyb = new keyboard (_directinput);     _keyb. SetCooperativeLevel (this point, SlimDX.DirectInput.CooperativeLevel.Background | SlimDX.DirectInput.CooperativeLevel.Nonexclusive);     _keyb. Acquire ();
Read
     //The key of Keyboardstate = keyb. Getcurrentkeyboardstate ();     //BOOL shift = key [Key.leftshift] | | | key [Key.rightshift];     //BOOL CTRL = key [Key.leftcontrol] | | | key [Key.rightcontrol];
Check
     //If the CTRL key (key [KEY.L] &&! Move &&! )                  (keys. IsPressed (KEY.L) &&! Transforming &&! CTRL)
Installing the Camera
     //_device. Transform.projection's MATRIX.PERSPECTIVEFOVLH (fov aspectratio,mapwidth = = 0?) 15F: (floating) (mapwidth/30f), mapwidth = = 0? 5000F: (Floating) (Mapwidth * 3));     //_device. Transform.view = MATRIX.LOOKATLH (new three-dimensional vector (0,0 area), new three-dimensional vector (0,0,0), new three-dimensional vector (1,0,0));        Matrix Perspectivefovlh matrix.perspectivefovlh (fov aspectratio,mapwidth = = 0?) 15 Floor: (floating) (mapwidth/30f), mapwidth = = 0? 5000F: (Floating) (Mapwidth * 3));    and Matrix LookAtLH = MATRIX.LOOKATLH (new three-dimensional vector (0,0 area), new three-dimensional vector (0,0,0), new three-dimensional vector (1,0,0));         _device. SetTransform (TRANSFORMSTATE.PROJECTION,PERSPECTIVEFOVLH);     _device. SetTransform (TRANSFORMSTATE.VIEW,LOOKATLH);
Grid
     //Net mesh (numfaces,numvertices,meshflags.managed,customvertex.positionnormaltextured.format, equipment);    Mesh = new Net (equipment, numfaces,numvertices,meshflags.managed,customvertex.positionnormaltextured.format);
Mesh. VertexBuffer

Very important difference. In slimdx, we use threads, and MDX's System.Array.

     //int [] row = new int [1]; //column [0] = mesh. Numbervertices//Column [0] = mesh. arr = mesh in Vertexcount//System.Array. Vertexbuffer.lock (0,, Lockflags.none typeof operation (customvertex.positionnormaltextured), rank); //(y = starty,y <= Endy, y + +)//{//(int x = STARTX; X <= EndX; X + +)//{//customvertex.positionnormaltextured PNT new customvertex.positionnormaltextured ();  //Arr.setvalue (Pnt,vertindex + +)//}//}//Mesh. Vertexbuffer.unlock (); //size = sizeof (customvertex.positionnormaltextured) for mesh.vertexcount use (data flow = mesh. VertexBuffer. Lock (0,mesh. Vertexbuffer.description.sizeinbytes,lockflags.none)) {customvertex.positionnormaltextured [] ARR = New customvertex.positionnormaltextured [Mesh. Vertexcount] (interpretation y = starty; <= ENDY; Y + +) {(x = startx, x <= endx,x + +) {customvertex.positionnormaltextured PNT = new customvertex.positionnormaltextured () ;“ ...  Arr. SetValue (Pnt,vertindex + +);}} Stream. Writerange   
CustomVertex

The

class is in CustomVertex MDX, but not, in Slimdx, due to the lack of direct correspondence in DirectX.   Here does not exist a unique solution, so I share the final version.   You just need to create a well-defined form of the structure, but create a class CustomVertex-your case, the code I created is compatible with MDX.   Internal content can also be implemented in different ways, vectors, or simple data types.

    Use the system;    Use System.Runtime.InteropServices;    Use SLIMDX;    Use Slimdx.direct3d9; namespace Terrainviewer {public struct CustomVertex {///This sentence forces the order of bytes to be stored on the GPU's expected data [StructLayout (layoutkind.sequential)] public structure positionnormaltextured//VERTEXTYPEPNT {public shareholding x;// Position public holding capacity y; //Position public holding capacity Z; //Position public shareholding xn; //general public shareholding yn///normal public shareholding of zinc,//general public holding capacity; //Texture position (TEXAS) public shareholding TV,//Texture position (TY) Common constant VertexFormat format = Vertexformat.position | Vertexformat.normal |                     Vertexformat.texture1;                 //Options//Common three-dimensional vector position;                 //Common three-dimensional vector normal;                 //Public Vector2 textureposition;             //public static annotation Sizebytes {{return marshal.sizeof (typeof operation (positionnormaltextured));}}     }//This sentence forces the order of bytes to be stored on the GPU's expected data [StructLayout (layoutkind.sequential)]       Public, structural positionnormalcolored//VERTEXTYPEPNT {public shareholding x;//position public shareholding Y;//per person Hold public shareholding Z; //Position public shareholding xn; //general public shareholding yn///normal public shareholding of zinc,//The color of the general public interpretation; //color Common constant VertexFormat format = Vertexformat.position | Vertexformat.normal |                     Vertexformat.diffuse;                 //Options//Common three-dimensional vector position;                 //Common three-dimensional vector normal;             //Public COLOR4 color//MDX//public static annotation Sizebytes {{return marshal.sizeof (typeof operation (positionnormalcolored));}} }         }     }
Glyph
     //////Create a font. /////HTTP://www.gamedev.net/community/forums/topic.asp?topic_id=557531//Public MICROSOFT.DIRECTX.DIRECT3D.F    Ont's createfont (string forgive me, floating emsize,system.drawing.fontstyle style) Public SlimDX.Direct3D9.Font CreateFont (string forgive me, floating emsize,system.drawing.fontstyle style) {try {Fontd             Description of the escription = new Fontdescription (); Description.             FaceName familyname;             //description.height = (int) (1.9 * emsize);            //description.quality of fontquality.cleartypenatural; if (style! = System.Drawing.FontStyle.Regular) {//if (style and System.Drawing.FontStyle.Italic)! = 0) Description.                 Isitalic = TRUE; (Style and System.Drawing.FontStyle.Italic)! = 0) Description.                    Italic = TRUE; if ((style and System.Drawing.FontStyle.Bold) = 0) description.                     Weight,fontweight.heavy; //Description.quality's Fontquality.antialias?? ED//MDX description. Quality fontquality.antialiased; }//FONT = new SlimDX.Direct3D9.Font (device, 15,0,fontweight.bold,0, fake,//Characterset.default, Pre Cision.                Truetype,fontquality.cleartypenatural,//Pitchandfamily.default Pitchandfamily.dontcare, "song Body"); return new SlimDX.Direct3D9.Font (INT) (device, (1.9 * emSize), 0,fontweight.bold,0, False, Characterset.default,precision.true                 type,fontquality.antialiased, Pitchandfamily.default Pitchandfamily.dontcare, forgive me); //Return to new Microsoft.DirectX.Direct3D.Font (device description);             //MDX//System.Drawing.Font f = new System.Drawing.Font (forgive me for the new fontfamily), emSize);         //Return to the new SlimDX.Direct3D9.Font (device, F);         } catch {returns null; }     }
Draw text
     //Sprite.begin (Spriteflags.none);     //Font.drawtext (Sprite, information text, rectangle, drawtextformat.left,color.black);     Sprite. Begin (spriteflags.alphablend);     Font. DrawString (Sprite, information text, rectangle, drawtextformat.left,color.black);
Original link: http://gis4all.ru/zh-CN/net/managed-directx/

[Go] Managed DirectX, conversion from MDX to SLIMDX

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.