[WorldWind学习]3.Device对象

  首先介绍一下Device类,Device位于using Microsoft.DirectX.Direct3D;命名空间下。

  Device类用于完成DirectX 里所有绘图操作,我们可以把这个类假想为图形卡,场景里所有其他图形对象都依赖于Device,计算机里可以有多个Device对象。所以在全局变量中定义一个绘图设备,如下代码:
  private Device m_Device3d;//定义绘图设备  

 1 private void InitializeGraphics()
 2         {
 3             // Set up our presentation parameters
 4             m_presentParams = new PresentParameters();
 5 
 6             m_presentParams.Windowed = true;//指定以Windows窗体形式显示
 7             m_presentParams.SwapEffect = SwapEffect.Discard;//当前屏幕绘制后
它将自动从内存中删除
8 m_presentParams.AutoDepthStencilFormat = DepthFormat.D16;//深度检测 9 m_presentParams.EnableAutoDepthStencil = true; 10 11 if(!World.Settings.VSync) 12 // Disable wait for vertical retrace (higher frame rate at the expense of tearing) 13 m_presentParams.PresentationInterval = PresentInterval.Immediate; 14 15 int adapterOrdinal = 0; 16 try 17 { 18 // Store the default adapter 19 adapterOrdinal = Manager.Adapters.Default.Adapter; 20 } 21 catch 22 { 23 // User probably needs to upgrade DirectX or install a 3D capable graphics adapter 24 throw new NotAvailableException(); 25 } 26 27 DeviceType dType = DeviceType.Hardware; 28 29 foreach(AdapterInformation ai in Manager.Adapters) 30 { 31 if(ai.Information.Description.IndexOf("NVPerfHUD") >= 0) 32 { 33 adapterOrdinal = ai.Adapter; 34 dType = DeviceType.Reference; 35 } 36 } 37 CreateFlags flags = CreateFlags.SoftwareVertexProcessing; 38 39 // Check to see if we can use a pure hardware m_Device3d 40 Caps caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware); 41 42 // Do we support hardware vertex processing? 43 if(caps.DeviceCaps.SupportsHardwareTransformAndLight) 44 // // Replace the software vertex processing 45 flags = CreateFlags.HardwareVertexProcessing; 46 47 // Use multi-threading for now - TODO: See if the code can be changed such that this isn't necessary (Texture Loading for example) 48 flags |= CreateFlags.MultiThreaded | CreateFlags.FpuPreserve; 49 50 try 51 { 52 // Create our m_Device3d 实例化device对象 53 m_Device3d = new Device(adapterOrdinal, dType, this, flags, m_presentParams); 54 } 55 catch( Microsoft.DirectX.DirectXException ) 56 { 57 throw new NotSupportedException("Unable to create the Direct3D m_Device3d."); 58 } 59 60 // Hook the m_Device3d reset event 61 m_Device3d.DeviceReset += new EventHandler(OnDeviceReset); 62 m_Device3d.DeviceResizing += new CancelEventHandler(m_Device3d_DeviceResizing); 63 OnDeviceReset(m_Device3d, null); 64 }

 

posted @ 2013-04-01 11:14  太一吾鱼水  阅读(375)  评论(0编辑  收藏  举报