⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 这是一个简单的3D游戏源码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MyVertexBuffer
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Device device = null;
		private VertexBuffer vb = null;
		private float angle = 0.0f;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque,true);
			
			this.Load +=new EventHandler(InitializeGraphics);
	
			
		}

		public void InitializeGraphics(object sender, EventArgs e)
		{	
			PresentParameters presentParams = new PresentParameters();
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;

			device = new Device(0,DeviceType.Hardware,this,CreateFlags.HardwareVertexProcessing,presentParams);
			//创建顶点缓冲
			vb = new VertexBuffer(typeof(CustomVertex.PositionColored),3,device,Usage.Dynamic | Usage.WriteOnly,
				CustomVertex.PositionColored.Format,Pool.Default);
			vb.Created += new EventHandler(OnVertexBufferCreate);
			
			OnVertexBufferCreate(vb, null);
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			device.Clear(ClearFlags.Target,System.Drawing.Color.Aqua,1.0f,0);
			SetupCamera();
			DrawSence();
			device.Present();
			this.Invalidate();
		}
			

		private void SetupCamera()
		{
			device.RenderState.CullMode = Cull.None;
			device.Transform.World = Matrix.RotationAxis(new Vector3(angle/((float)Math.PI*2.0f),angle/((float)Math.PI*4.0f),
				angle/((float)Math.PI*6.0f)),angle/(float)Math.PI);
			angle += 0.1f;
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, this.Width/this.Height, 1.0f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,5.0f),new Vector3(),new Vector3(0,1,0));
			device.RenderState.Lighting = false;
		}

		private void DrawSence()
		{
			device.BeginScene();
			device.VertexFormat = CustomVertex.PositionColored.Format;
			device.SetStreamSource(0,vb,0);
			device.DrawPrimitives(PrimitiveType.TriangleList,0,1);
			device.EndScene();
		}

		private void OnVertexBufferCreate(object sender, EventArgs e)
		{
			VertexBuffer buffer = (VertexBuffer)sender;

			CustomVertex.PositionColored[] verts = new Microsoft.DirectX.Direct3D.CustomVertex.PositionColored[3];
            verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
			verts[0].Color = System.Drawing.Color.Olive.ToArgb();
            verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
			verts[1].Color = System.Drawing.Color.GreenYellow.ToArgb();
            verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
			verts[2].Color = System.Drawing.Color.Purple.ToArgb();
			buffer.SetData(verts,0,LockFlags.None);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -