教程1:创建设备-C# Direct3D SDK 翻译(1)

 

C# Direct3D SDK 翻译

教程1:创建设备

效果图示
/Files/idiot8/Tutorial1.rar
创建一个应用程序窗口

任何windows应用程序运行前都必须创建一个窗口。在下面的代码中,首先定义一个应用程序类CreateDevice,再通过它的构造函数来设置显示窗口的大小,标题和图标。CreateDevice继承自System.Windows.Forms.Form类。System.Windows.Forms.FormMicrosoft .NET Framework提供的代表程序中窗口的类。

          [C#]
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DeviceTutorial
{
    
public class CreateDevice : Form
    
{
        
// Our global variables for this project
        Device device = null// Our rendering device

        
public CreateDevice()
        
{
            
// Set the initial size of our form
            this.ClientSize = new System.Drawing.Size(400,300);
            
// And its caption
            this.Text = "D3D Tutorial 01: CreateDevice";
        }

        .
        .
        .
    }

    .
    .
    .
}

初始化D3D对象

创建应用程序窗口后,你就要初始化D3D对象,以便你使用它来渲染场景。这个过程包括创建对象,设置参数,最终创建好D3D对象。

          [C#]
public bool InitializeGraphics()
{
    
try
    
{
        
// Now  setup our D3D stuff
        PresentParameters presentParams = new PresentParameters();
        presentParams.Windowed
=true;
        presentParams.SwapEffect 
= SwapEffect.Discard;
        device 
= new Device(0, DeviceType.Hardware, this
                CreateFlags.SoftwareVertexProcessing, presentParams);
                
return true;
    }

    
catch (DirectXException)
    

         
return false
    }

}

前面的代码通过PresentParameters对象设置窗口的显示特性。举例来说,通过设置Windowed属性为true,窗口的大小就会以非全屏显示。缺省的窗口没有菜单,也没有子窗口,但是含有最小化,最大化,关闭按钮。在这个例子中,快速的交换显存到系统内存被禁止了,是通过SwapEffect.Discard标志。如果windowed属性设置为false,创建的窗口将会在所有非顶层窗口之上,甚至处于非活动状态时候也是。

初始化的最后步骤就是创建D3D 设备。在本例子中设置Device(Int32,DeviceType,Control,CreateFlags,PresentParameters[])指定硬件设备是首选的,顶点处理将采用软件的形式。注意,如果你在支持硬件顶点处理的显卡上,通过指定CreateFlags.HardwareVertexProcessing程序采用硬件处理顶点,将会获得非常大的性能的提高。

渲染D3D 对象

程序通过调用静态对象ApplicationDoEvents方法,保持循环运行。DoEvents在当前线程启动标准的windows消息循环。

          [C#]
static void Main() 
{

    
using (CreateDevice frm = new CreateDevice())
    {
        
if (!frm.InitializeGraphics()) // Initialize Direct3D
        {
            MessageBox.Show(
                
"Could not initialize Direct3D.  This tutorial will exit.");
            
return;
        }
        frm.Show();

        
// While the form is still valid, render and process messages
        while(frm.Created)
        {
            frm.Render();
            Application.DoEvents();
        }
    }
}

当创建的设备有效,调用预订义的Render方法来渲染D3D 对象。调用Device.Clear方法将视口设置成单一蓝色。场景从调用Device.BeginScene方法开始渲染,接着可以加入自己的代码,渲染结束后要调用EndScenePresent方法。

      [C#]
private void Render()
{
    
if (device == null
        
return;

    
//Clear the backbuffer to a blue color 
    device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f0);
    
//Begin the scene
    device.BeginScene();
    
    
// Rendering of scene objects can happen here

    
//End the scene
    device.EndScene();
    device.Present();
}

posted on 2005-09-21 14:22  idiot8  阅读(3567)  评论(0编辑  收藏  举报