您的位置:首页 > 其它

Managed DirectX® 9学习笔记 ycl

2005-02-17 09:55 399 查看
ManagedDirectX®9学习笔记

第一章介绍

1、命名空间

Table1.ManagedDirectXNamespaces
Microsoft.DirectXParentnamespace,holdsallcommoncode.
Microsoft.DirectX.Direct3DDirect3DgraphicsAPI,aswellastheD3DXhelperlibrary.
Microsoft.DirectX.DirectDrawDirectDrawgraphicsAPI.
Microsoft.DirectX.DirectPlayDirectPlaynetworkingAPI.
Microsoft.DirectX.DirectSoundDirectSoundaudioAPI.
Microsoft.DirectX.DirectInputDirectInputuserinputAPI.
Microsoft.DirectX.AudioVideoPlaybackSimpleaudioandvideoplaybackAPI.
Microsoft.DirectX.DiagnosticsSimplediagnosticsAPI.
Microsoft.DirectX.SecurityUnderlyingstructureforDirectXcodeaccesssecurity.
Microsoft.DirectX.Security.PermissionsPermissionclassesforDirectXcodeaccesssecurity.

2、引入

usingMicrosoft.DirectX;
usingMicrosoft.DirectX.Direct3D;

3、Direct3DDevice

deviceclass是所有Direct3D的根,我们可以把它想象为在机器上得真实图形设备,我们的PC上可能有0到多个devices。
一共有三个构造函数可以创建一个设备,我们先介绍第一个。
publicDevice(System.Int32adapter,Microsoft.DirectX.Direct3D.DeviceTypedeviceType,
System.Windows.Forms.ControlrenderWindow,Microsoft.DirectX.Direct3D.CreateFlags
behaviorFlags,Microsoft.DirectX.Direct3D.PresentParameterspresentationParameters)
我们介绍一下这些参数,adapter是指向我们可以操纵的物理设备,0通常是默认的设备。
DeviceType是说我们要创建什么类型的device,通常使用DeviceType.Hardware,就是用硬件设备,
有时候我们也用DeviceType.Reference,会很慢,一般用来测试和检测显卡是否支持DirectX版本。
renderWindow参数将设备绑定在一个窗口上,我们可以用form,panel或者其他control-derivedclass作为这个参数。
behaviorFlags是用来指定在创建设备以后用什么来控制这个设备的外貌,这个枚举有很多的选择,但他们有的是互斥的,不能选。
目前我们选择SoftwareVertexProcessing,这个选项指定所有的vertexprocessing用CPU来计算,比起用GPU这样自然会慢一点,
但安全起见,还是用这个选项,因为我们并不知道你的GPU是否可以完成这项任务,也许你的PC根本就没有GPU.
presentationParameters指定数据是如何被写到屏幕的,其中有一些属性“Windowed”他是一个Boolean,false表示用全屏
幕模式,true表示用窗口模式。SwapEffect用来控制缓冲区。
我们设计一个函数,以后的程序都可以用
privateDevicedevice=null;
publicvoidInitializeGraphics()
{
PresentParameterspresentParams=newPresentParameters();
presentParams.Windowed=true;
presentParams.SwapEffect=SwapEffect.Discard
//创建一个新的设备
device=newDevice(0,DeviceType.Hardware,this,
CreateFlags.SoftwareVertexProcessing,presentParams);
}
上面写的代码并没有被调用,我们必须修改一下主函数,修改如下:
staticvoidMain()
{
using(Form1frm=newForm1())
{
//Showourformandinitializeourgraphicsengine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
我们修改以后,并没有发现和向导中创建的空windowsform有什么变化,因为我们并没有做什么改动,
为了使其有变化,我们必须重写OnPaint函数,达到重绘窗口的目的
protectedoverridevoidOnPaint(System.Windows.Forms.PaintEventArgse)
{
device.Clear(ClearFlags.Target,System.Drawing.Color.CornflowerBlue,1.0f,0);
device.Present();
}
我们用Clear方法填充窗口,第一个参数表示要填充的目标,用的是ClearFlags这个枚举值中的Target;
第二个参数是颜色值;第三个参数代表Z轴的buffer;第四个参数为stencil-buffer,可以设定为0。
(具体参数可查看DSDK).Present方法可以立即更新屏幕,这个方法有许多重载,以后介绍或可自己查帮助文件。
我们下面将绘制一个三角形,利用CustomVertex,D3D中有很多结构通用的"vertexformat",
每个vertex可以表示一个点。现在我们用TransformedColored来绘制这个三角形,这个结构告诉D3D运行
时我们这个三角形不需要变换(旋转或移动)。我们把下面的代码插入到OnPaintoverride的方法中。
(注:不同版本的SDK提供的方法和属性有变动,请根据自己的sdk修改代码)
CustomVertex.TransformedColored[]verts=newCustomVertex.TransformedColored[3];
verts[0].SetPosition(newVector4(this.Width/2.0f,50.0f,0.5f,1.0f));
verts[0].Color=System.Drawing.Color.Aqua.ToArgb();
verts[1].SetPosition(newVector4(this.Width-(this.Width/5.0f),this.Height–
(this.Height/5.0f),0.5f,1.0f));
verts[1].Color=System.Drawing.Color.Black.ToArgb();
verts[2].SetPosition(newVector4(this.Width/5.0f,this.Height-(this.Height/5.0f)
,0.5f,1.0f));
verts[2].Color=System.Drawing.Color.Purple.ToArgb();
我们将创建三个Member,每个Member代表三角形的一个点,每个member用一个结构Vector4初始化
他的位置,另两个参数表示z位置和rhw。
结构定义好了,我们可以花三角形了
device.BeginScene();
device.VertexFormat=CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList,1,verts);
device.EndScene();
现在可以绘制了,为了在改变窗口大小的时候重绘窗口,我们用this.Invalidate();来使窗口重绘时
立即更新。而此函数必须搭配设置窗口类型的方法一起使用
this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.Opaque,true);
整个源代码如下:

usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingMicrosoft.DirectX;
usingMicrosoft.DirectX.Direct3D;

namespaceChapter1Code
{
///<summary>
///SummarydescriptionforForm1.
///</summary>
publicclassForm1:System.Windows.Forms.Form
{
privateDevicedevice=null;
///<summary>
///Requireddesignervariable.
///</summary>
privateSystem.ComponentModel.Containercomponents=null;

publicForm1()
{
//
//RequiredforWindowsFormDesignersupport
//
InitializeComponent();

this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.Opaque,true);
}

///<summary>
///Wewillinitializeourgraphicsdevicehere
///</summary>
publicvoidInitializeGraphics()
{
//Setourpresentationparameters
PresentParameterspresentParams=newPresentParameters();

presentParams.Windowed=true;
presentParams.SwapEffect=SwapEffect.Discard;

//Createourdevice
device=newDevice(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,presentParams);
}

protectedoverridevoidOnPaint(System.Windows.Forms.PaintEventArgse)
{
device.Clear(ClearFlags.Target,System.Drawing.Color.CornflowerBlue,1.0f,0);

CustomVertex.TransformedColored[]verts=newCustomVertex.TransformedColored[3];
verts[0].Position=(newVector4(this.Width/2.0f,50.0f,0.5f,1.0f));
verts[0].Color=System.Drawing.Color.Aqua.ToArgb();
verts[1].Position=(newVector4(this.Width-(this.Width/5.0f),this.Height-(this.Height/5.0f),0.5f,1.0f));
verts[1].Color=System.Drawing.Color.Black.ToArgb();
verts[2].Position=(newVector4(this.Width/5.0f,this.Height-(this.Height/5.0f),0.5f,1.0f));
verts[2].Color=System.Drawing.Color.Purple.ToArgb();

device.BeginScene();
device.VertexFormat=CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList,1,verts);
device.EndScene();

device.Present();

this.Invalidate();
}

///<summary>
///Cleanupanyresourcesbeingused.
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#regionWindowsFormDesignergeneratedcode
///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.components=newSystem.ComponentModel.Container();
this.Size=newSystem.Drawing.Size(300,300);
this.Text="Form1";
}
#endregion

///<summary>
///Themainentrypointfortheapplication.
///</summary>
staticvoidMain()
{ using(Form1frm=newForm1()) { //Showourformandinitializeourgraphicsengine frm.Show(); frm.InitializeGraphics(); Application.Run(frm); }
}
}
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: