c#开发环境下用Directx载入3D模型
来源:互联网 发布:python 创建临时文件 编辑:程序博客网 时间:2024/05/27 10:44
Direcx一般都是用VC++开发环境,而c#相关的资料比较少,最近做的一个项目中需要用到一个3D模型,而开发环境是visual c#,没办法,就四处搜集资料,查找相关的资料,然后找到了几篇相关的博文,不过这些博文说的都不太仔细,有些内容又丢失了一些关键代码,这个时候就需要我们自己搜集资料,自己学习来补全这些关键代码了。
废话少说,我们直接说说步骤吧。
一.首先要准备一个3D模型,现在3D模型都是.max或者.obj或者别的格式的,不过因为我用的是Directx9.0开发环境,因此载入3D模型的时候需要把文件转换为.X格式,没有关系,我们可以下载一个3dsMax软件,然后装上微软的panda插件,这个软件就能把.max格式的3d文件转换为.x格式输出。这一步我们最主要的目的就是为了得到一个.x格式的3D模型。
二.接下来就是下载Directx9.0Sdk了,下载完成之后安装这个开发工具。
三.接下来进入项目,新建一个Visual c# winform窗体程序,然后点击确定,我们的编程工作就开始了。
首先,项目需要用到Directx开发包,因此我们需要给项目添加引用,右键单击项目,选择添加引用,然后在引用的对话框中选择浏览,这里引用的位置是:C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0,注意最后的应用的文件夹是1.0.2902.0,我们引用这里面的三个文件:DirectX.dll ,DirectX3D.dll 和DirectX3DX.dll,选择这三项之后点击确定,之后就添加了引用,然后我们还需要在窗体代码中添加如下引用:
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
之后我们的引用工作算是完成了。因为我准备把3D模型显示在一个控件上,我选择了一个Panel控件放到主窗口上,我们的模型待会儿就会显示在这个panel上。首先在Form中设置如下几个全局变量:
- #region
-
- private Mesh mesh = null;
-
-
- private Device device = null;
-
- private Material[] meshMaterials;
- private Texture[] meshTextures;
-
-
- string path = System.Windows.Forms.Application.StartupPath;
-
-
- private float angle = 0.0f;
- #endregion
然后写出我们的初始化图形设备的函数:- <span style="font-size:14px;">
- public void InitializeGraphics()
- {
-
- PresentParameters presentParams = new PresentParameters();
-
- presentParams.Windowed = true;
-
- presentParams.SwapEffect = SwapEffect.Discard;
- presentParams.AutoDepthStencilFormat = DepthFormat.D16;
- presentParams.EnableAutoDepthStencil = true;
-
-
- device = new Device(0, DeviceType.Hardware, panel1,
- CreateFlags.SoftwareVertexProcessing, presentParams);
-
- string temp = path;
- temp = temp + "\\Model\\Model.X";
-
- LoadMesh(temp);
- }</span>
然后是载入3D模型的LoadMesh(string file) 函数
- private void LoadMesh(string file)
- {
- ExtendedMaterial[] mtrl = null;
-
- try
- {
- mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);
-
- if ((mtrl != null) && (mtrl.Length > 0))
- {
-
- meshMaterials = new Material[mtrl.Length];
- meshTextures = new Texture[mtrl.Length];
-
- for (int i = 0; i < mtrl.Length; ++i)
- {
-
-
-
- string temp = path + "\\Model\\";
- meshMaterials[i] = mtrl[i].Material3D;
- if ((mtrl[i].TextureFilename != null)
- && mtrl[i].TextureFilename != string.Empty)
- {
- meshTextures[i] = TextureLoader.FromFile(device, temp + mtrl[i].TextureFilename);
- }
- }
- }
- }
- catch (Direct3DXException ex)
- {
- MessageBox.Show(ex.ToString());
- return;
- }
- }
载入模型之后就需要将模型画出来,因此,还需要一个画模型的函数:
-
- private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)
- {
- angle += 0.01f;
- device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);
- for (int i = 0; i < meshMaterials.Length; ++i)
- {
-
- device.Material = meshMaterials[i];
-
- device.SetTexture(0, meshTextures[i]);
-
- mesh.DrawSubset(i);
- }
- }
最后还差一个摄影灯光的设置了,这个用于灯光的角度:-
- private void SetupCamera()
- {
-
- device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 12, this.Width / this.Height, 0.80f, 10000.0f);
- device.Transform.View = Matrix.LookAtLH(new Vector3(-400, -100, 600.0f), new Vector3(), new Vector3(0, 1, 0));
- device.RenderState.Ambient = Color.Black;
- device.Lights[0].Type = LightType.Directional;
- device.Lights[0].Diffuse = Color.AntiqueWhite;
- device.Lights[0].Direction = new Vector3(0, 1, 0);
- device.Lights[0].Update();
- device.Lights[0].Enabled = true;
- }
至此,基本大功告成,最后的是逐步调用这几个函数,然后显示出来,我采用panel这个控件绘画的时候调用这些函数,如下:-
- private void panelPaint(object sender, PaintEventArgs e)
- {
- InitializeGraphics();
- device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.SkyBlue, 1.0f, 1);
- SetupCamera();
- device.Present();
- device.BeginScene();
-
- DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f);
- device.EndScene();
-
- device.Present();
-
- }
最后贴出来一个效果图:
至此,基本大功告成。不过这里还有几个问题要说明:
1.程序编译过程中或许会遇到一个问题:
混合模式程序集是针对“v1.1.4322”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
这个问题解决办法就是打开文件中的app.config文件,注释掉原来的内容,改成以下内容:
- <span style="font-size:14px;"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0"?>
- <configuration>
- <startup useLegacyV2RuntimeActivationPolicy="true">
- <supportedRuntime version="v4.0"/>
- </startup>
- </configuration></span></span>
程序应该就不会有问题了。2.如果你自己在导入自己的3D模型的时候,采用3dsmax中导入的panda控件转换模型文件为.x文件的时候,有个选项,就是XFileSetting的设置一定要设置为Binary,如果设置为Text的话,Directx在读入文件的时候会不识别。截图如下:
能遇到的问题基本就这些了,这里附上源代码链接:
http://download.csdn.net/detail/t46414704152abc/8709251
恩,基本就这样了!希望能帮助到大家!