資源簡介
本程序可以實現三維的顯示,放大,縮小,翻轉等操作,并且還可自行添加貼圖,只需將其中的圖片路徑修改一下。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?Microsoft.DirectX;
using?Microsoft.DirectX.Direct3D;
namespace?WindowsFormsApplication14
{
????public?partial?class?Form1?:?Form
????{
????????public?Vector3?CamPostion?=?new?Vector3(0?100?100);//定義攝像機位置?
????????public?Vector3?CamTarget?=?new?Vector3(125?30?125);//定義攝像機目標位置?
????????private?Device?device?=?null;//定義繪圖設備?
????????private?float?angleY?=?0.01f;//定義繞Y軸旋轉變量?
????????private?int?mouseLastX?mouseLastY;//記錄鼠標按下時的坐標位置?
????????private?bool?isRotateByMouse?=?false;//記錄是否由鼠標控制旋轉?
????????private?bool?isMoveByMouse?=?false;//記錄是否由鼠標控制移動?
?
????????private?CustomVertex.PositionTextured[]?vertices;//定義頂點變量?
????????private?Texture?texture;//定義貼圖變量?
????????private?Material?material;//定義材質變量?
?
????????private?VertexBuffer?vertexBuffer;//定義頂點緩沖變量?
????????private?IndexBuffer?indexBuffer;//定義索引緩沖變量?
????????private?int[]?indices;//定義索引號變量?
?
????????private?int?xCount?=?5?yCount?=?4;//定義橫向和縱向網格數目?
????????private?float?cellHeight?=?1f?cellWidth?=?1f;//定義單元的寬度和長度?
????????public?string?texturePath?=?@“D:\1.bmp“;//定義貼圖路徑?
????????public?string?heightMapPath?=?@“D:\1.bmp“;//定義高度圖路徑?
????????public?Form1()
????????{
????????????//InitializeComponent();
????????????this.ClientSize?=?new?Size(800?600);//指定窗體尺寸?
????????????this.Text?=?“三維顯示“;//指定窗體標題
????????}
????????public?bool?InitializeDirect3D()?
????????{?
????????????try?
????????????{?
????????????????PresentParameters?presentParams?=?new?PresentParameters();?
????????????????presentParams.Windowed?=?true;?//指定以Windows窗體形式顯示?
????????????????presentParams.SwapEffect?=?SwapEffect.Discard;?//當前屏幕繪制后它將自動從內存中刪除?
????????????????presentParams.AutoDepthStencilFormat?=?DepthFormat.D16;?
????????????????presentParams.EnableAutoDepthStencil?=?true;?
????????????????presentParams.PresentationInterval?=?PresentInterval.Immediate;?
????????????????device?=?new?Device(0?DeviceType.Hardware?this?CreateFlags.SoftwareVertexProcessing?presentParams);?//實例化device對象?
????????????????Matrix?viewMatrix?=?Matrix.LookAtLH(CamPostion?CamTarget?new?Vector3(0?1?0));?
????????????????device.Transform.Projection?=?Matrix.PerspectiveFovLH((float)Math.PI?/?4?this.Width?/?this.Height?0.1f?1000f);?
????????????????device.Transform.View?=?viewMatrix;?
????????????????//lastCamPosAndTarg.Add(device.Transform.View);//記錄這次攝像機的位置?
????????????????VertexDeclaration();//定義頂點?
????????????????IndicesDeclaration();//定義索引緩沖?
????????????????LoadTexturesAndMaterials();//導入貼圖和材質?
????????????????return?true;?
????????????}?
????????????catch?(DirectXException?e)?
????????????{?
????????????????MessageBox.Show(e.ToString()?“Error“);?//處理異常?
????????????????return?false;?
????????????}?
????????}?
????????private?void?VertexDeclaration()//定義頂點?
????????{?
???
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????917??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14.sln
????..A..H.?????22016??2013-03-18?10:10??WindowsFormsApplication14\WindowsFormsApplication14.suo
?????文件??????14068??2013-03-02?20:00??WindowsFormsApplication14\WindowsFormsApplication14\Form1.cs
?????文件???????1175??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14\Form1.Designer.cs
?????文件???????1178??2013-03-01?17:20??WindowsFormsApplication14\WindowsFormsApplication14\Program.cs
?????文件???????3645??2013-03-01?17:18??WindowsFormsApplication14\WindowsFormsApplication14\WindowsFormsApplication14.csproj
?????文件???????6508??2013-03-02?20:00??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????180??2013-03-02?16:08??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\WindowsFormsApplication14.Properties.Resources.resources
?????文件???????8388??2013-03-02?16:08??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\GenerateResource-ResGen.read.1.tlog
?????文件???????1022??2013-03-02?16:08??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\GenerateResource-ResGen.write.1.tlog
?????文件??????36352??2013-03-02?20:00??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\WindowsFormsApplication14.pdb
?????文件??????14336??2013-03-02?20:00??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\WindowsFormsApplication14.exe
?????文件???????2058??2013-03-18?10:07??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\WindowsFormsApplication14.csproj.FileListAbsolute.txt
?????文件???????3413??2013-03-01?17:55??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\ResolveAssemblyReference.cache
?????文件????????490??2010-03-17?22:39??WindowsFormsApplication14\WindowsFormsApplication14\bin\Debug\WindowsFormsApplication14.vshost.exe.manifest
?????文件??????11608??2013-03-18?10:07??WindowsFormsApplication14\WindowsFormsApplication14\bin\Debug\WindowsFormsApplication14.vshost.exe
?????文件??????14336??2013-03-02?20:00??WindowsFormsApplication14\WindowsFormsApplication14\bin\Debug\WindowsFormsApplication14.exe
?????文件??????36352??2013-03-02?20:00??WindowsFormsApplication14\WindowsFormsApplication14\bin\Debug\WindowsFormsApplication14.pdb
?????文件???????1406??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14\Properties\AssemblyInfo.cs
?????文件???????5612??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14\Properties\Resources.resx
?????文件???????2900??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14\Properties\Resources.Designer.cs
?????文件????????249??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14\Properties\Settings.settings
?????文件???????1110??2013-03-01?17:08??WindowsFormsApplication14\WindowsFormsApplication14\Properties\Settings.Designer.cs
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug\TempPE
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86\Debug
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\obj\x86
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\bin\Debug
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\obj
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\bin
?????目錄??????????0??2013-03-01?17:24??WindowsFormsApplication14\WindowsFormsApplication14\Properties
............此處省略5個文件信息
- 上一篇:c#拼圖游戲可換圖,選難度,計時,統計步數
- 下一篇:ASP圖書管理系統
評論
共有 條評論