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

 

C# Direct3D SDK 翻译<?xml:namespace prefix = o />

教程1:创建设备

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

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


None.gif           [C#]
None.gif
using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Windows.Forms;
None.gif
using  Microsoft.DirectX;
None.gif
using  Microsoft.DirectX.Direct3D;
None.gif
None.gif
namespace  DeviceTutorial
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class CreateDevice : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Our global variables for this project
InBlock.gif
        Device device = null// Our rendering device
InBlock.gif

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

InBlock.gif        .
InBlock.gif        .
InBlock.gif        .
ExpandedSubBlockEnd.gif    }

InBlock.gif    .
InBlock.gif    .
InBlock.gif    .
ExpandedBlockEnd.gif}

None.gif

初始化D3D对象

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


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

InBlock.gif    
catch (DirectXException)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif         
return false
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

前面的代码通过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.0f 0 );
    
// Begin the scene
    device.BeginScene();
    
    
//  Rendering of scene objects can happen here

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

转载于:https://www.cnblogs.com/idiot8/archive/2005/09/21/241217.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值