[WorldWind学习]2.WorldWindow控件
首先查看WorldWindow 的构造函数,接着查看InitializeGraphics()函数。
1 public WorldWindow() 2 { 3 this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); 4 5 // The m_Device3d can't be created unless the control is at least 1 x 1 pixels in size 6 this.Size = new Size(1,1); 7 8 try 9 { 10 // Now perform the rendering m_Device3d initialization 11 // Skip DirectX initialization in design mode 12 if(!IsInDesignMode()) 13 this.InitializeGraphics();//初始化设备Device 14 15 //Post m_Device3d creation initialization 16 this.drawArgs = new DrawArgs(m_Device3d, this ); 17 this.m_RootWidget = new WorldWind.Widgets.RootWidget(this); 18 this.m_NewRootWidget = new WorldWind.NewWidgets.RootWidget(this); 19 20 //this.m_RootWidget.ChildWidgets.Add(layerManager); 21 DrawArgs.RootWidget = this.m_RootWidget; 22 DrawArgs.NewRootWidget = this.m_NewRootWidget; 23 24 m_FpsTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_FpsTimer_Elapsed); 25 m_FpsTimer.Start(); 26 27 TimeKeeper.Start(); 28 // WorldWind.Widgets.LayerManager layerManager = new WorldWind.Widgets.LayerManager(); 29 // m_RootWidget.ChildWidgets.Add(layerManager); 30 31 } 32 catch (InvalidCallException caught) 33 { 34 throw new InvalidCallException( 35 "Unable to locate a compatible graphics adapter. Make sure you are running the latest version of DirectX.", caught ); 36 } 37 catch (NotAvailableException caught) 38 { 39 throw new NotAvailableException( 40 "Unable to locate a compatible graphics adapter. Make sure you are running the latest version of DirectX.", caught ); 41 } 42 }
View Code
1 private void InitializeGraphics() 2 { 3 // Set up our presentation parameters 4 m_presentParams = new PresentParameters(); 5 6 m_presentParams.Windowed = true; 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 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 }
控件重要的方法:
OnApplicationIdle函数定义: public void OnApplicationIdle(object sender, EventArgs e)//The world render loop.实现世界的渲染循环 OnPaint函数定义: protected override void OnPaint(PaintEventArgs e) // Occurs when the control is redrawn and m_isRenderDisabled=true. protected override void OnMouseWheel(MouseEventArgs e) //处理鼠标滚轮事件 protected override void OnKeyDown(KeyEventArgs e) //重载处理键盘按下的事件 protected override void OnKeyUp(KeyEventArgs e) //重载处理键盘弹起事件 protected override void OnMouseDoubleClick(MouseEventArgs e) protected override void OnMouseUp(MouseEventArgs e)
控件几个重要的属性和字段:
private Device m_Device3d; private DrawArgs drawArgs; private World m_World; private Cache m_Cache; private Thread m_WorkerThread;
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。