C# DirectX技术同时加载多个.x文件 模型可 旋转 移动

DirectX加载.X文件,使用C++语言的实例很多,使用C#的资源稍微少点儿,因工程需要,使用了C#语言。分享下自己使用C#加载多个.X文件的方法,加载后的模型实现了旋转、移动等。

1. 工具:操作系统Windows10企业版,visual studio 2013.

2. 打开VS2013新建项目

3. 添加引用,右击项目下的引用->添加引用->浏览->C盘->Windows->Microsoft.NET->DirectX for Managed Code->1.0.2902.0->选择Microsoft.DirectX.dll

Microsoft.DirectX.Direct3D.dll

Microsoft.DirectX.Direct3DX.dll

->点击添加。

4. 在Form1.cs中添加

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

5. 运行,恭喜您已经可以使用C#, DirectX开发自己的3D游戏了。

6. 接下来是加载.X文件

7. 初始化图形设备

public bool InitializeGraphics(out Parameter3D parameter3D, Parameter3D param)
        {
            parameter3D = param;
            
            try
            {
                PresentParameters presentparams = new PresentParameters();
                presentparams.Windowed = true;//这个是在form1
                presentparams.DeviceWindow = parameter3D.panel;//这个是把图画弄到pannal里面
                presentparams.SwapEffect = SwapEffect.Discard;
                presentparams.EnableAutoDepthStencil = true;
                presentparams.AutoDepthStencilFormat = DepthFormat.D16;
                parameter3D.device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentparams);
                // fonts = new Microsoft.DirectX.Direct3D.Font(device, new System.Drawing.Font
                //("Arial", 14.0f, FontStyle.Bold | FontStyle.Italic));
                parameter3D.device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnCreateDevice(out parameter3D, parameter3D, null);
                this.OnResetDevice(parameter3D.device, null);
            }
            catch (DirectXException)
            {
                return false;
            }
            return true;
        }

8.  在创建图形设备时,进行.X文件的加载

public void OnCreateDevice(out Parameter3D parameter3D,Parameter3D param, EventArgs e)
        {
            parameter3D = param;
            parameter3D.meshmaterials = new Material();
            //meshmaterials.Ambient = System.Drawing.Color.White;
            //meshmaterials.Diffuse = System.Drawing.Color.White;
            //ColorValue cv=new ColorValue(255,1,1);
            //meshmaterials.AmbientColor = cv;
            //meshmaterials.DiffuseColor = cv;
            ExtendedMaterial[] Materials = null;
            parameter3D.mesh = Mesh.FromFile(@"..\..\" + parameter3D.xStr+ @".X", MeshFlags.RtPatches, parameter3D.device, out Materials);//要载入的.x文件

            if (parameter3D.meshtexture == null)
            {
                parameter3D.meshtexture = new Texture[Materials.Length];
                parameter3D.meshmaterials1 = new Microsoft.DirectX.Direct3D.Material[Materials.Length];
                for (int i = 0; i < Materials.Length; i++)
                {
                    parameter3D.meshmaterials1[i] = Materials[i].Material3D;
                    parameter3D.meshmaterials1[i].Ambient = parameter3D.meshmaterials1[i].Diffuse;
                    parameter3D.meshtexture[i] = TextureLoader.FromFile(parameter3D.device, @"..\..\gray.jpg");//DDS files or jpg is OK.  这里的meshtexture 文件必须有,不然模型全黑乎乎的

                }
            }

        }

9. 

public void OnResetDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;
            dev.RenderState.Ambient = Color.Gray;// System.Drawing.Color.Brown;
            dev.RenderState.CullMode = Cull.None;
            dev.RenderState.Lighting = true;
            dev.RenderState.BlendFactor = Color.White;
            dev.RenderState.FogColor = Color.Yellow;


            dev.Lights[0].Type = LightType.Directional;
            dev.Lights[0].Diffuse = Color.Silver;
            dev.Lights[0].Direction = new Vector3(-100, 100, 100);//设置投光位置
            dev.Lights[0].Update();
            dev.Lights[0].Enabled = true;

            dev.Lights[1].Type = LightType.Directional;
            dev.Lights[1].Diffuse = Color.Silver;
            dev.Lights[1].Direction = new Vector3(100, -100, -100);//设置投光位置
            dev.Lights[1].Update();
            dev.Lights[1].Enabled = true;

            //dev.Lights[2].Type = LightType.Directional;
            //dev.Lights[2].Diffuse = Color.Silver;
            //dev.Lights[2].Direction = new Vector3(-100, 100, -100);//设置投光位置
            //dev.Lights[2].Update();
            //dev.Lights[2].Enabled = true;

            //dev.Lights[3].Type = LightType.Directional;
            //dev.Lights[3].Diffuse = Color.Silver;
            //dev.Lights[3].Direction = new Vector3(-100, 100, 100);//设置投光位置
            //dev.Lights[3].Update();
            //dev.Lights[3].Enabled = true;

            Material meshmaterials = new Material();
            //meshmaterials.Ambient = System.Drawing.Color.White;
            //meshmaterials.Diffuse = System.Drawing.Color.White;
            //ColorValue cv=new ColorValue(255,1,1);
            //meshmaterials.AmbientColor = cv;
            //meshmaterials.DiffuseColor = cv;
            dev.Material = meshmaterials;

        }

10. 最后渲染

public void Render(Parameter3D parameter3D)
        {
            if (parameter3D.device == null) return;
            parameter3D.device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0,255,255,255), 10.0f, 0);
            SetupMatrices(parameter3D);
            parameter3D.device.BeginScene();
            for (int i = 0; i < parameter3D.meshmaterials1.Length; i++)
            {
                parameter3D.device.Material = parameter3D.meshmaterials1[i];
                parameter3D.device.SetTexture(0, parameter3D.meshtexture[i]);
                parameter3D.mesh.DrawSubset(i);
            }

            parameter3D.device.EndScene();
            parameter3D.device.Present();
        }

11. 点击初始化时,循环添加即可加载多个.X模型文件,PS:Panel的位置要设置。

private void button1_Click(object sender, EventArgs e)
        {
            Panel panel1 = new Panel();
            panel1.Location = new Point(100, 10);
            panel1.Size = new System.Drawing.Size(200, 200);
            panel1.BackColor = Color.FromArgb(0, 0, 0, 0);
            this.Controls.Add(panel1);

            Parameter3D inParam3D = new Parameter3D(null, panel1, null, null, 0, 50.0f, 0, 0, 0, -5, 0, 0, 0, "K0");
            Parameter3D outParame3D;
            InitializeGraphics(out outParame3D, inParam3D);
            Render(outParame3D);
            data.Add(outParame3D);

            Panel panel2 = new Panel();
            panel2.Location = new Point(310, 10);
            panel2.Size = new System.Drawing.Size(200, 200);
            panel2.BackColor = Color.FromArgb(0, 0, 0, 0);
            this.Controls.Add(panel2);
            Parameter3D inParam3D1 = new Parameter3D(null, panel2, null, null, 0, 50.0f, 0, 0, 0, -5, 0, 0, 0, "K1");
            Parameter3D outParame3D1;
            InitializeGraphics(out outParame3D1, inParam3D1);
            Render(outParame3D1);
            data.Add(outParame3D1);
        }

12. 源码链接   https://download.csdn.net/download/xinghun11111/10732085

 C# DirectX 加载多个.X文件 旋转 移动

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值