VC#版DirectX开发入门详解
一 首先需要安裝DirectX SDK,
可在
http://download.microsoft.com/download/F/1/7/F178BCE4-FA19-428F-BB60-F3DEE1130BFA/DXSDK_Feb10.exe
下載
也可在我的網(wǎng)盤下載:
http://pan.baidu.com/s/1pJmMLHh
?
安裝之后在下圖目錄出現(xiàn)下圖內(nèi)容,這是用于C#的DirectX支持DLL;
?
二 首先拷貝過(guò)來(lái)一個(gè)例子代碼調(diào)試一下;
出現(xiàn)上圖錯(cuò)誤,按下圖啟動(dòng)工作站服務(wù),
?
?
再調(diào)試,可以運(yùn)行了,但是出現(xiàn)下圖錯(cuò)誤;
??? 按網(wǎng)上一些說(shuō)法,改了一些地方,包括.net版本改為2.0,還是錯(cuò)誤,提示只有一句:不是有效的Win32應(yīng)用程序;既然不好搞,還是不用拷來(lái)的例子,自己寫吧;
?
新建一個(gè)空項(xiàng)目 - 添加新項(xiàng) 窗體;
此時(shí)窗體只有一個(gè)構(gòu)造函數(shù),添加Main()函數(shù)讓程序能運(yùn)行,如下;
static void Main()
??????? {
??????????? Form1 basicForm = new Form1(); //創(chuàng)建窗體對(duì)象
??????????? basicForm.Show(); //
??????? }
?
然后添加引用,添加命名空間,如下;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
先運(yùn)行一下,結(jié)果出現(xiàn) VS沒(méi)反應(yīng),出現(xiàn)提示對(duì)話框:VS正在等待某個(gè)操作.... 這個(gè)錯(cuò)誤,大概不少人碰到過(guò);
工具-選項(xiàng)-IntelliSense,把 IntelliSense成員選擇 前面的勾去掉;
??? 再運(yùn)行,好了;下面添加DirectX代碼;
添加窗體級(jí)變量:
Device device = null;//定義繪圖設(shè)備
?
添加初始化D3D函數(shù):
public bool InitializeDirect3D()
??????? {
??????????? try
??????????? {
??????????????? PresentParameters presentParams = new PresentParameters();
??????????????? presentParams.Windowed = true; //指定以Windows窗體形式顯示
??????????????? presentParams.SwapEffect = SwapEffect.Discard; //當(dāng)前屏幕繪制后它將自動(dòng)從內(nèi)存中刪除
??????????????? device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //實(shí)例化device對(duì)象
??????????????? return true;
??????????? }
??????????? catch (DirectXException e)
??????????? {
??????????????? MessageBox.Show(e.ToString(), "Error"); //處理異常
??????????????? return false;
??????????? }
??????? }
?
添加渲染函數(shù),此時(shí)并無(wú)渲染內(nèi)容;
public void Render()
??????? {
??????????? if (device == null)?? //如果device為空則不渲染
??????????? {
??????????????? return;
??????????? }
??????????? device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);? //清除windows界面為深藍(lán)色
??????????? device.BeginScene();
??????????? //在此添加渲染圖形代碼
??????????? device.VertexFormat = CustomVertex.TransformedColored.Format;
??????????? device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
??????????? device.EndScene();
??????????? device.Present();
??????? }
?
main()函數(shù)改為如下:
static void Main()
??????? {
??????????? Form1 basicForm = new Form1(); //創(chuàng)建窗體對(duì)象
??????????? if (basicForm.InitializeDirect3D() == false) //檢查Direct3D是否啟動(dòng)
??????????? {
??????????????? MessageBox.Show("無(wú)法啟動(dòng)Direct3D!", "錯(cuò)誤!");
??????????????? return;
??????????? }
??????????? basicForm.Show(); //如果一切都初始化成功,則顯示窗體
??????????? while (basicForm.Created) //設(shè)置一個(gè)循環(huán)用于實(shí)時(shí)更新渲染狀態(tài)
??????????? {
??????????????? basicForm.Render(); //保持device渲染,直到程序結(jié)束
??????????????? Application.DoEvents(); //處理鍵盤鼠標(biāo)等輸入事件
??????????? }
??????? }
然后再運(yùn)行,出現(xiàn)下圖錯(cuò)誤;
?
把.net版本改為2.0;Run,好了,第一個(gè)DirectX窗口出現(xiàn);不過(guò)并無(wú)內(nèi)容;
?
下面來(lái)畫(huà)一個(gè)基本的三角形;
在BeginScene和EndScene之間加入如下代碼;
CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[3];//定義頂點(diǎn)
??????????? vertices[0].Position = new Vector4(400f, 200f, 0f, 1f);
??????????? vertices[0].Color = Color.Red.ToArgb();
??????????? vertices[1].Position = new Vector4(this.Width / 2, 100f, 0f, 1f);
??????????? vertices[1].Color = Color.Green.ToArgb();
??????????? vertices[2].Position = new Vector4(this.Width - 150f, 200f, 0f, 1f);
??????????? vertices[2].Color = Color.Yellow.ToArgb();
??????????? device.VertexFormat = CustomVertex.TransformedColored.Format;
??????????? device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
?
運(yùn)行程序,結(jié)果如下,因?yàn)樵O(shè)置的三角形坐標(biāo)有問(wèn)題,三角形超出了邊界;
?
Vector4結(jié)構(gòu)的前三個(gè)參數(shù)是指浮點(diǎn)型的x、y、z坐標(biāo);
修改坐標(biāo)為如下,
vertices[0].Position = new Vector4(100f, 200f, 0f, 1f);
??????????? vertices[0].Color = Color.Red.ToArgb();
??????????? vertices[1].Position = new Vector4(this.Width / 2, 50f, 0f, 1f);
??????????? vertices[1].Color = Color.Green.ToArgb();
??????????? vertices[2].Position = new Vector4(this.Width - 150f, 100f, 0f, 1f);
??????????? vertices[2].Color = Color.Yellow.ToArgb();
再RUN,這下好了;下面就可以開(kāi)始用DirectX做更多的事。
?
項(xiàng)目源碼下載:
http://pan.baidu.com/s/1c050hCO
?
?
總結(jié)
以上是生活随笔為你收集整理的VC#版DirectX开发入门详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: DirectX9 SDK 下载、安装、V
- 下一篇: 图解VC#版DirectX开发教程二 -