World wind 三維地球的初始化

Direct3D的初始化代碼

private void InitializeGraphics()

{

    // Set up our presentation parameters

    // 將 Presentation Parameters 作爲字段變量保存下來:m_presentParams

     m_presentParams = new PresentParameters(); 

     m_presentParams.Windowed = true;

     m_presentParams.SwapEffect = SwapEffect.Discard;

     m_presentParams.AutoDepthStencilFormat = DepthFormat.D16;

     m_presentParams.EnableAutoDepthStencil = true;

    

    if(!World.Settings.VSync)

         // Disable wait for vertical retrace (higher frame rate at the expense of tearing)

         m_presentParams.PresentationInterval = PresentInterval.Immediate;

 

    int adapterOrdinal = 0;

    try

     {

         // Store the default adapter

         adapterOrdinal = Manager.Adapters.Default.Adapter;

     }

    catch

     {

         // User probably needs to upgrade DirectX or install a 3D capable graphics adapter

         throw new NotAvailableException();

     }

 

    DeviceType dType = DeviceType.Hardware; 

    foreach(AdapterInformation ai in Manager.Adapters)

     {

         // 這裏沒有看懂

         if(ai.Information.Description.IndexOf("NVPerfHUD") >= 0)

         {

              adapterOrdinal = ai.Adapter;

              dType = DeviceType.Reference;

         }

     }

 

    CreateFlags flags = CreateFlags.SoftwareVertexProcessing; 

    // Check to see if we can use a pure hardware m_Device3d

    Caps caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware); 

    // Do we support hardware vertex processing?

    if(caps.DeviceCaps.SupportsHardwareTransformAndLight)

         //   // Replace the software vertex processing

         flags = CreateFlags.HardwareVertexProcessing;

 

    // Use multi-threading for now -

     // TODO: See if the code can be changed such that this isn't necessary (Texture Loading for example)

     flags |= CreateFlags.MultiThreaded | CreateFlags.FpuPreserve;

 

    try

     {

         // Create our m_Device3d

         m_Device3d = new Device(adapterOrdinal, dType, this, flags, m_presentParams);

     }

    catch( Microsoft.DirectX.DirectXException )

     {

         throw new NotSupportedException("Unable to create the Direct3D m_Device3d.");

     }

 

    // Hook the m_Device3d reset event

     m_Device3d.DeviceReset += new EventHandler(OnDeviceReset);

     m_Device3d.DeviceResizing += new CancelEventHandler(m_Device3d_DeviceResizing);

     OnDeviceReset(m_Device3d, null); // 必須首先調用一次

}

 

private void OnDeviceReset(object sender, EventArgs e)

{

    // Can we use anisotropic texture minify filter?

    if( m_Device3d.DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic)

     {

         m_Device3d.SamplerState[0].MinFilter = TextureFilter.Anisotropic;

     }

    else if( m_Device3d.DeviceCaps.TextureFilterCaps.SupportsMinifyLinear)

     {

         m_Device3d.SamplerState[0].MinFilter = TextureFilter.Linear;

     }

 

    // What about magnify filter?

    if( m_Device3d.DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic )

     {

         m_Device3d.SamplerState[0].MagFilter = TextureFilter.Anisotropic;

     }

    else if( m_Device3d.DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear )

     {

         m_Device3d.SamplerState[0].MagFilter = TextureFilter.Linear;

     }

 

     m_Device3d.SamplerState[0].AddressU = TextureAddress.Clamp;

     m_Device3d.SamplerState[0].AddressV = TextureAddress.Clamp;

 

     m_Device3d.RenderState.Clipping = true;

     m_Device3d.RenderState.CullMode = Cull.Clockwise;

     m_Device3d.RenderState.Lighting = false;

     m_Device3d.RenderState.Ambient = World.Settings.StandardAmbientColor;

 

     m_Device3d.RenderState.ZBufferEnable = true;

     m_Device3d.RenderState.AlphaBlendEnable = true;

     m_Device3d.RenderState.SourceBlend = Blend.SourceAlpha;

     m_Device3d.RenderState.DestinationBlend = Blend.InvSourceAlpha;

}

 

private void m_Device3d_DeviceResizing(object sender, CancelEventArgs e)

{

    if(this.Size.Width == 0 || this.Size.Height == 0)

     {

         e.Cancel = true;

         return;

     }

 

    this.drawArgs.screenHeight = this.Height;

    this.drawArgs.screenWidth = this.Width;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章