• 大小: 50KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-18
  • 语言: C#
  • 标签: c#  三维  

资源简介

本程序可以实现三维的显示,放大,缩小,翻转等操作,并且还可自行添加贴图,只需将其中的图片路径修改一下。

资源截图

代码片段和文件信息

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个文件信息

评论

共有 条评论