Directx 3D编程实例:多个3D球的综合Directx实例

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档。于是我也觉的有这个必要。写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客。


第一步:判断显卡是否支持

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 综合举例
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 frm = new Form1();
            if (frm.InitializeGraphics() == false)
            {
                MessageBox.Show("显卡不支持3D或者未安装配套的显卡驱动程序!");
                return;
            }
            Application.Run(frm);
        }
    }
}


第二步:程序源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace 综合举例
{
    public partial class Form1 : Form
    {
        private Device device = null;
        private Microsoft.DirectX.Direct3D.Font d3dfont;
        private string helpString;
        private bool showHelpString = true;
        private float angle = 0;
        private bool enableRotator = true;
        private int rotatorXYZ = 0;
        private float rotateSpeed = 0.01f;        //旋转速度
        private bool enableSolidMode = true;
        private bool enableCullMode = true;
        private Mesh[] sphereMeshs;
        private float sphereRadius = 1.5f;        //小球半径
        private int sphereNumber = 18;
        private Matrix[] spherePositions;   //原始位置变换矩阵
        private float xRadius = 15.0f;        //x方向圆的半径
        private int ySpacing = 10;        //y方向相对空间大小
        private VertexBuffer vertexBuffer = null; //顶点缓冲
        private Material[] sphereMaterial;        //小球材质
        private Material[] lineMaterial;          //线的材质
        private bool enableEmissive = false;    //是否允许物体本身发光
        private Material commonSphereMaterial = new Material();
        private Material commonLineMaterial = new Material();
        private bool multiMaterial = true;
        private bool isPointLight = false;
        private bool enableLight = true;

        public Form1()
        {
            InitializeComponent();
        }
        public bool InitializeGraphics()
        {
            try
            {
                PresentParameters presentParams = new PresentParameters();
                presentParams.Windowed = true;
                presentParams.SwapEffect = SwapEffect.Discard;
                presentParams.AutoDepthStencilFormat = DepthFormat.D16;
                presentParams.EnableAutoDepthStencil = true;
                device = new Device(0, DeviceType.Hardware, this,
                    CreateFlags.SoftwareVertexProcessing, presentParams);
                device.RenderState.ZBufferEnable = true;
                return true;
            }
            catch (DirectXException)
            {
                return false;
            }
        }
        public void BuildScene()
        {
            this.buildMeshs();               //创建Mesh对象
            this.BuildspherePositions();        //构造小球位置
            this.BuildLineVertexBuffer();      //创建小球间的连线顶点缓冲
            BuildMaterials();                //创建材质
        }
        private void buildMeshs()
        {
            //一定要及时释放资源,不能靠垃圾回收自动回收,
            //否则退出后会很长时间无反应,像死机一样
            if (sphereMeshs != null)
            {
                for (int i = 0; i < sphereMeshs.Length; i++)
                {
                    sphereMeshs[i].Dispose();
                }
            }
            sphereMeshs = new Mesh[sphereNumber];
            for (int i = 0; i < sphereNumber; i++)
            {
                sphereMeshs[i] = Mesh.Sphere(device, sphereRadius, 30, 15);
            }
        }
        private void BuildspherePositions()
        {
            spherePositions = new Matrix[sphereNumber];
            float alfa = 360.0f / (sphereNumber - 2);
            for (int i = 0; i < sphereNumber; i++)
            {
                if (i == 0)
                {
                    spherePositions[i] = Matrix.Translation(0, ySpacing + 5, 0);
                }
                else if (i == sphereNumber - 1)
                {
                    spherePositions[i] = Matrix.Translation(0, -ySpacing, 0);
                }
                else
                {
                    //将小球按圆周在水平面平均分布
                    float xx = (float)(xRadius * Math.Sin(i * alfa * Math.PI / 180.0f));
                    float zz = (float)(xRadius * Math.Cos(i * alfa * Math.PI / 180.0f));
                    spherePositions[i] = Matrix.Translation(xx, 0, zz);
                }
            }
        }
        private void BuildLineVertexBuffer()
        {
            vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly),
                (sphereNumber - 2) * 4 + 2,
                device, 0, CustomVertex.PositionOnly.Format, Pool.Default);
            vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);
            this.OnCreateVertexBuffer(vertexBuffer, null);
        }
        public void OnCreateVertexBuffer(object sender, EventArgs e)
        {
            VertexBuffer buffer = (VertexBuffer)sender;
            CustomVertex.PositionOnly[] verts =
                (CustomVertex.PositionOnly[])buffer.Lock(0, 0);
            Random random = new Random();
            for (int i = 0; i < sphereNumber - 2; i++)
            {
                Vector3 vector = new Vector3(spherePositions[i + 1].M41,
                    spherePositions[i + 1].M42, spherePositions[i + 1].M43);
                verts[i * 2].Position = vector;
                verts[i * 2 + 1].Position = new Vector3(0, ySpacing, 0);
                verts[2 * (sphereNumber - 2) + (i * 2)].Position = vector;
                verts[2 * (sphereNumber - 2) + (i * 2 + 1)].Position =
                    new Vector3(0, -ySpacing, 0);
            }
            verts[sphereNumber - 1].Position = new Vector3(spherePositions[0].M41,
                spherePositions[0].M42, spherePositions[0].M43);
            verts[sphereNumber - 2].Position = new Vector3(0, ySpacing, 0);
            vertexBuffer.Unlock();
        }
        private void SetupCamera()
        {
            float fieldOfView = (float)Math.PI / 4;
            float aspectRatio = (float)this.Width / (float)this.Height;
            float nearPlane = 1.0f;
            float farPlane = 200.0f;
            device.Transform.Projection =
                Matrix.PerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane);
            Vector3 cameraPosition = new Vector3(0, 0, -50.0f);
            Vector3 cameraTarget = new Vector3(0, 0, 0);
            Vector3 upDirection = new Vector3(0, 1, 0);
            device.Transform.View = Matrix.LookAtLH(cameraPosition, cameraTarget, upDirection);
            device.RenderState.FillMode = (enableSolidMode ? FillMode.Solid : FillMode.WireFrame);
            device.RenderState.CullMode = (enableCullMode ? Cull.CounterClockwise : Cull.None);
        }
        private void BuildMaterials()
        {
            Random random = new Random();
            Color color;
            sphereMaterial = new Material[sphereNumber];
            for (int i = 0; i < sphereNumber; i++)
            {
                color = Color.FromArgb(random.Next(byte.MaxValue),
                    random.Next(byte.MaxValue), random.Next(byte.MaxValue));
                sphereMaterial[i].Diffuse = color;
                //注意:设置Emissive的目的是为了让物体本身发光,以方便演示光照效果
                //如果不设置此属性,物体看起来会更逼真
                if (enableEmissive == true)
                {
                    sphereMaterial[i].Emissive = color;
                }
            }
            int lines = (sphereNumber - 2) * 2 + 1;
            lineMaterial = new Material[lines];
            for (int i = 0; i < lines; i++)
            {
                color = Color.FromArgb(random.Next(byte.MaxValue),
                    random.Next(byte.MaxValue), random.Next(byte.MaxValue));
                lineMaterial[i].Ambient = color;
            }
        }
        private void SetupLights()
        {
            device.RenderState.Ambient = Color.White;
            if (isPointLight == false)
            {
                device.Lights[0].Type = LightType.Directional;
                device.Lights[0].Direction = new Vector3(0, 0, 1); //指向z轴正方向
            }
            else
            {
                device.Lights[0].Type = LightType.Point;
                device.Lights[0].Position = new Vector3(6, 0, 0); //旋转轴中心位置
            }
            device.Lights[0].Range = 500.0f;
            device.Lights[0].Enabled = enableLight;
        }

        public void RendScene()
        {
            if (enableRotator)
            {
                angle += rotateSpeed;
            }
            ////画小球
            for (int i = 0; i < sphereNumber; i++)
            {
                //设置材质
                if (multiMaterial)
                {
                    device.Material = sphereMaterial[i];
                }
                else
                {
                    device.Material = commonSphereMaterial;
                }
                SetWorldTransform(spherePositions[i].M41, spherePositions[i].M42, spherePositions[i].M43);
                sphereMeshs[i].DrawSubset(0);
            }
            //重新进行矩阵变换
            SetWorldTransform(0, 0, 0);
            //画线
            int lines = (sphereNumber - 2) * 2 + 1;
            device.SetStreamSource(0, vertexBuffer, 0);
            device.VertexFormat = CustomVertex.PositionOnly.Format;
            for (int i = 0; i < lines; i++)
            {
                if (multiMaterial)
                {
                    device.Material = lineMaterial[i];
                }
                else
                {
                    device.Material = commonLineMaterial;
                }
                device.DrawPrimitives(PrimitiveType.LineList, i * 2, 1);
            }
            if (showHelpString == true)
            {
                d3dfont.DrawText(null, helpString, 25, this.Height - 290, Color.White);
            }
        }
        private void SetWorldTransform(float x, float y, float z)
        {
            Vector3 world;
            if (rotatorXYZ == 0)
            {
                world = new Vector3(angle, 0, 0);
            }
            else if (rotatorXYZ == 1)
            {
                world = new Vector3(0, angle, 0);
            }
            else
            {
                world = new Vector3(0, 0, angle);
            }
            device.Transform.World =
                Matrix.Translation(x, y, z) *
                Matrix.RotationAxis(world, angle) * Matrix.Translation(6, 0, 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 700;
            this.Height = 520;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
            this.KeyPreview = true;//↑↓←→
            helpString = "<Esc>:退出

" +
                "<F1>:显示/隐藏提示信息
" +
                "<F2>:实心/线框
" +
                "<F3>:背面剔除/不剔除
" +
                "<F4>:旋转/不旋转
" +
                "<F5>:旋转轴(x轴、y轴、z轴)

" +
                "<0>:物体本身发光/物体本身不发光
" +
                "<1>:关闭/打开灯光
" +
                "<2>:直射光
" +
                "<3>:点光源
" +
                "<4>:单一材质/多种材质
" +
                "<5>:变换小球和连线材质

" +
                "上下左右箭头键:增加、减少小球个数
" +
                "<Alt>+箭头键  :提高、降低旋转速度
" +
                "<Ctrl>+箭头键 :增加、减少大圆半径
" +
                "<Shift>+箭头键:增加、减少小球半径";
            System.Drawing.Font winFont = new System.Drawing.Font("宋体", 9, FontStyle.Regular);
            d3dfont = new Microsoft.DirectX.Direct3D.Font(device, winFont);
            d3dfont.PreloadText(helpString);
            BuildScene();      //创建小球及连线
            device.RenderState.Lighting = true;
            //设置环境光颜色
            device.RenderState.Ambient = Color.White;
            //单一材质时,所有小球使用漫射光,受有方向的光照影响
            commonSphereMaterial.Diffuse = Color.Red;
            commonSphereMaterial.Ambient = Color.Black;
            //单一材质时,所有连线均使用连线材质的环境反射色
            //当环境光为白色时,连线的颜色就是指定的环境反射色(此句中连线也使用白色)
            commonLineMaterial.Ambient = Color.White;
            SetupLights();     //设置灯光

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSeaGreen, 1.0f, 0);
            SetupCamera();
            device.BeginScene();
            this.RendScene();
            device.EndScene();
            device.Present();
            if (WindowState != FormWindowState.Minimized)
            {
                this.Invalidate();
            }

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Close(); break;
                case Keys.F1:
                    showHelpString = !showHelpString; break;
                case Keys.F2:
                    enableSolidMode = !enableSolidMode; break;
                case Keys.F3:
                    enableCullMode = !enableCullMode; break;
                case Keys.F4:
                    enableRotator = !enableRotator; break;
                case Keys.F5:
                    rotatorXYZ = (rotatorXYZ + 1) % 3; break;
                case Keys.D0:
                    enableEmissive = !enableEmissive;
                    BuildMaterials();
                    break;
                case Keys.D1:
                    enableLight = !enableLight; SetupLights(); break;
                case Keys.D2:
                    isPointLight = false; SetupLights(); break;
                case Keys.D3:
                    isPointLight = true; SetupLights(); break;
                case Keys.D4:
                    multiMaterial = !multiMaterial; break;
                case Keys.D5:
                    BuildMaterials(); break;
                case Keys.Up:
                case Keys.Right:
                    if (e.Alt == true) rotateSpeed += 0.005f;
                    else if (e.Shift == true) sphereRadius += 0.05f;
                    else if (e.Control == true) xRadius += 0.5f;
                    else sphereNumber += 2;
                    if (e.Alt == false)
                    {
                        BuildScene();
                    }
                    break;
                case Keys.Down:
                case Keys.Left:
                    if (e.Alt == true)
                        rotateSpeed =
                            (rotateSpeed > 0.01 ? rotateSpeed - 0.005f : rotateSpeed);
                    else if (e.Shift == true)
                        sphereRadius =
                            (sphereRadius > 0.1f ? sphereRadius - 0.05f : sphereRadius);
                    else if (e.Control == true)
                        xRadius = (xRadius > 0.5f ? xRadius - 0.5f : xRadius);
                    else
                        sphereNumber = (sphereNumber > 6 ? sphereNumber - 2 : sphereNumber);
                    if (e.Alt == false)
                    {
                        BuildScene();
                    }
                    break;
            }

        }

    }
}




原文地址:https://www.cnblogs.com/pangblog/p/3358110.html