游戏开发 XNAGame-011 绘制点、线和三角形

25 篇文章 1 订阅
25 篇文章 0 订阅

form1.cs

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 XNAGame_011
{
    public partial class Form1 : Form
    {
        /*
         * 1.编写Direct3D程序,首先需要安装direct3d sdk程序。
         * 2.项目引用中,需要添加Microsoft.DirectX,Microsoft.DirectX.Direct3D,Microsoft.Direct3DX,
         * 3.上面的引用,我的电脑的路径是C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0
         * 4.vs项目属性中,引用路径要把C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include\,
         * 和C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\,都包含进来。
         * 5.在form1.cs中,需要引用 using Microsoft.DirectX,using Microsoft.DirectX.Direct3D;
         * 6.非常重要的一点,此点不解决,窗体不显示,在App.config配置文件中,加入<startup useLegacyV2RuntimeActivationPolicy="true"> 
         * 
         * 
         */
        //设备类
        private Device device1 = null;

        //暂停
        bool pause = false;

        //客户矩阵,已翻译颜色,的数组,
        //增加两个数组变量 
        CustomVertex.TransformedColored[] verts = null;
        CustomVertex.TransformedColored[] verts2 = null;


        public Form1()
        {
            InitializeComponent();
        }

        public bool InitializeGraphics()
        {
            try
            {
                PresentParameters param1 = new PresentParameters();
                param1.Windowed = true;  // 非全屏模式,即窗口模式 
                param1.SwapEffect = SwapEffect.Discard;  // 后备缓存模式,新帧显示,旧帧丢弃
                param1.EnableAutoDepthStencil = true;  //自动深度测试
                param1.AutoDepthStencilFormat = DepthFormat.D16; //深度缓存区单元为16位二进制
              
                device1 = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, param1);
                device1.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnCreateDevice(device1, null);
                this.OnResetDevice(device1, null);

                return true;
            }
            catch (DirectXException ex)
            {
                return false;
            }

        }

        public void Render()
        {
            if (device1 == null) return;   // 设备类没有初始化,为空,不渲染
            if (pause) return; // 暂停,窗体最小化,不可见,不渲染
            device1.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
            device1.BeginScene();

            //增加这一段
            device1.VertexFormat = CustomVertex.TransformedColored.Format;
            Modify(0.0f, 0.0f);
            device1.DrawUserPrimitives(PrimitiveType.TriangleFan, 5, verts2);  //5个三角形

            Modify(250.0f, 0.0f);  //坐标整体向右移动250
            device1.DrawUserPrimitives(PrimitiveType.TriangleStrip, 4, verts2); //4个三角形

            Modify(500.0f,0.0f);  //坐标整体向右移动500
            device1.DrawUserPrimitives(PrimitiveType.TriangleList, 2, verts2); //2个三角形

            Modify(0.0f, 250.0f);  //坐标整体下移250
            device1.DrawUserPrimitives(PrimitiveType.LineList, 3, verts2); //3条线段

            Modify(250.0f, 250.0f); //下移250 ,右移250
            device1.DrawUserPrimitives(PrimitiveType.LineStrip, 5, verts2); // 5条线段

            Modify(500.0f, 250.0f);  // 下移250,右移500
            device1.DrawUserPrimitives(PrimitiveType.PointList, 6, verts2); //6个点

            verts2 = null;


            device1.EndScene();
            device1.Present();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.Render();
        }

        //在此方法中,增加顶点的位置,6个
        public void OnCreateDevice(object sender, EventArgs e)
        {
            verts = new CustomVertex.TransformedColored[6];
            verts[0].Position = new Vector4(10.0f,10.0f,0.5f,1.0f);   //点1,左上
            verts[0].Color = Color.Aqua.ToArgb();
            verts[1].Position = new Vector4(210.0f, 10.0f, 0.5f, 1.0f);  //点2,右上
            verts[1].Color = Color.Brown.ToArgb();
            verts[2].Position = new Vector4(110.0f, 60.0f, 0.5f, 1.0f); //点3,中上 
            verts[2].Color = Color.Brown.ToArgb();
            verts[3].Position = new Vector4(210.0f, 210.0f, 0.5f, 1.0f);  //点4,右下
            verts[3].Color = Color.Aqua.ToArgb();
            verts[4].Position = new Vector4(110.0f, 160.0f, 0.5f, 1.0f);  //点5,中下
            verts[4].Color = Color.LightPink.ToArgb();
            verts[5].Position = new Vector4(10.0f, 210.0f, 0.5f, 1.0f);  //点6,左下 
            verts[5].Color = Color.LightPink.ToArgb();




        }

        public void OnResetDevice(object sender, EventArgs e)
        {

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
        }

        //增加此方法,对第一个数组中的值,增加一个固定值
        private void Modify(float x1, float y1)
        { 
            verts2 = (CustomVertex.TransformedColored[])verts.Clone();
            for (int i = 0; i < 6; i++)
            {
                verts2[i].X += x1;
                verts2[i].Y += y1;
            }
        }

    }
}

program.cs

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

namespace XNAGame_011
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            using (Form1 frm = new Form1())
            {
                if (frm.InitializeGraphics() == false)
                {
                    MessageBox.Show("无法初始化Direct 3D,退出");
                    return;
                }
                frm.Show();
                while (frm.Created == true)
                {
                    frm.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值