扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
private Microsoft.DirectX.Direct3D.Device device = null; |
public void InitializeGraphics() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Flip; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams); } |
public void Render() { // 清空设备,并准备显示下一帧。 device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black , 1.0f, 0); // 设置照相机的位置 SetupCamera(); //开始场景 device.BeginScene(); if(meshLoaded) { mesh.Render(meshLoc); } device.EndScene(); //显示设备内容。 device.Present(); } |
private void SetupCamera() { device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.00f); device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f ,0.0f, 20.0f), new Vector3(0.0f,0.0f, 0.0f), new Vector3(0,1,0)); } |
[STAThread] static void Main() { using (Form1 EarthForm = new Form1()) { EarthForm.InitializeGraphics(); EarthForm.Show(); while(EarthForm.Created) { EarthForm.Render(); Application.DoEvents(); } EarthForm.Dispose(); } |
public class Earth : BaseEarth { private Material[] mMaterials; //保存材质 private Texture[] mTextures; //保存纹理 private Matrix locationOffset; //用来保存网格对象的相对位置 private Mesh mMesh = null; //三角形网格对象 private Device meshDevice; //需要显示在哪个设备上。 } |
public Earth(ref Device device, Matrix location): base(ref device) { meshDevice = device; locationOffset = location; } |
public bool LoadMesh(string meshfile) { ExtendedMaterial[] mtrl; try { // 装载文件 mMesh = Mesh.FromFile(meshfile, MeshFlags.Managed, meshDevice, out mtrl); // 如果有材质的话,装入它们 if ((mtrl != null) && (mtrl.Length > 0)) { mMaterials = new Material[mtrl.Length]; mTextures = new Texture[mtrl.Length]; // 得到材质和纹理 for (int i = 0; i < mtrl.Length; i++) { mMaterials[i] = mtrl[i].Material3D; if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty)) { //前面得到的纹理的路径是相对路径,需要保存的是绝对路径,通过应用程序路径可以获得 mTextures[i] = TextureLoader.FromFile(meshDevice, @"..\..\" + mtrl[i].TextureFilename); } } } return true; } catch { return false; } } |
public void Render(Matrix worldTransform) { /把位置变为世界坐标 meshDevice.Transform.World = Matrix.Multiply(locationOffset, worldTransform); //绘制网格 for (int i = 0; i < mMaterials.Length; i++) { meshDevice.Material = mMaterials[i]; meshDevice.SetTexture(0, mTextures[i]); mMesh.DrawSubset(i); } } |
private Earth mesh = null; private Matrix meshLoc; private bool meshLoaded = false; |
meshLoc = Matrix.Identity; meshLoc.M41 = 2.0f; mesh = new Earth(ref device, meshLoc); if (mesh.LoadMesh(@"..\..\earth.x")) { meshLoaded = true; } |
device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White; device.Lights[0].Direction = new Vector3(0, -1, -1); device.Lights[0].Update(); device.Lights[0].Enabled = true; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。