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

📄 matrixcontrol.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace EnterDirectX {
	/// <summary>
	/// Summary description for MatrixControl.
	/// </summary>
	public class MatrixControl : System.Windows.Forms.Form {
		#region Form Components

		internal System.Windows.Forms.Label lblRotZ;
		internal System.Windows.Forms.Label lblRotY;
		internal System.Windows.Forms.Label LblRotX;
		internal System.Windows.Forms.CheckBox chkAuto;
		internal System.Windows.Forms.GroupBox grpTranslation;
		internal System.Windows.Forms.Label lblTranY;
		internal System.Windows.Forms.Label lblTranZ;
		internal System.Windows.Forms.Label lblTranX;
		internal System.Windows.Forms.Label lblScaX;
		internal System.Windows.Forms.Label lblScaY;
		internal System.Windows.Forms.Label lblScaZ;
		#endregion

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private bool endTest = false;		public bool EndTest {			get { return endTest; }		}

		private Device device = null;
		private static int x = 0;

		private VertexBuffer vertBuffer = null;
		private Texture[] textures = new Texture[10];
		internal System.Windows.Forms.TrackBar RotationX;
		internal System.Windows.Forms.TrackBar RotationY;
		internal System.Windows.Forms.TrackBar RotationZ;
		internal System.Windows.Forms.TrackBar TranslationX;
		internal System.Windows.Forms.TrackBar TranslationY;
		internal System.Windows.Forms.TrackBar TranslationZ;
		internal System.Windows.Forms.TrackBar ScaleX;
		internal System.Windows.Forms.TrackBar ScaleY;
		internal System.Windows.Forms.TrackBar ScaleZ;
		private System.Windows.Forms.GroupBox grpRotation;
		private System.Windows.Forms.GroupBox grpScale;

		// Simple textured vertices constant and structure
		private const VertexFormats customVertex  = VertexFormats.Position | VertexFormats.Texture1;
		private int numVerts = 36;

		public MatrixControl() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if(components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		public void DisposeD3D() {
			for(int i = 0; i < 10; i++) {
				if(textures[i] != null) {
					textures[i].Dispose();
					textures[i] = null;
				}
			}

			if(vertBuffer != null) {
				vertBuffer.Dispose();
				vertBuffer = null;
			}

			if(device != null) {
				device.Dispose();
				device = null;
			}
		}

		public bool InitD3D(IntPtr winHandle) {
			DisplayMode DispMode = 
				Manager.Adapters[Manager.Adapters.Default.Adapter].CurrentDisplayMode;
			PresentParameters presentParams = new PresentParameters();
			// Define the presentation parameters
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;
			presentParams.BackBufferFormat = DispMode.Format;
			presentParams.EnableAutoDepthStencil = true;
			presentParams.AutoDepthStencilFormat = DepthFormat.D16;
			// Try to create the device
			try {
				device = new Device(Manager.Adapters.Default.Adapter, 
					DeviceType.Hardware, winHandle, 
					CreateFlags.SoftwareVertexProcessing, presentParams);
				// Turn off culling => front and back of the triangles are visible
				device.RenderState.CullMode = Cull.None;
				// Turn off D3D lighting
				device.RenderState.Lighting = false;
				// Turn on ZBuffer
				device.RenderState.ZBufferEnable = true;
				device.VertexFormat = customVertex;
				//  Set the Projection Matrix to use a orthogonal view
				device.Transform.Projection = Matrix.OrthoLH(300, 200, -200, +200);
				return true;
			}
			catch {
				return false;
			}
		}

		public bool CreateCube() {
			try {
				string txName;
				for(int i=1; i<=10; i++) {
					txName = Application.StartupPath+"\\Walk"+i.ToString()+".bmp";
					textures[i-1] = 
						TextureLoader.FromFile(device, txName);
				}
				vertBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), numVerts, device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
				vertBuffer.Created += new EventHandler(this.OnVertexBufferCreate);
				OnVertexBufferCreate(vertBuffer, null);
				return true;
			}
			catch {
				return false;
			}
		}


		private void OnVertexBufferCreate(object sender, EventArgs e) {
			VertexBuffer buffer = (VertexBuffer)sender;
			CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[numVerts];
		// 1st facet --------------------------------------------------------- 
			//triangle 1
			verts[0] = new CustomVertex.PositionTextured(0, 0, 0, 0, 0);
			verts[1] = new CustomVertex.PositionTextured(90, 0, 0, 1, 0);
			verts[2] = new CustomVertex.PositionTextured(0, 90, 0, 0, 1);

			//triangle 2
			verts[3] = new CustomVertex.PositionTextured(0, 90, 0, 0, 1);
			verts[4] = new CustomVertex.PositionTextured(90, 0, 0, 1, 0);
			verts[5] = new CustomVertex.PositionTextured(90, 90, 0, 1, 1);

			// 2nd facet --------------------------------------------------------- 
			//triangle 1
			verts[6] = new CustomVertex.PositionTextured(90, 0, 0, 0, 0);
			verts[7] = new CustomVertex.PositionTextured(90, 90, 0, 1, 0);
			verts[8] = new CustomVertex.PositionTextured(90, 0, 90, 0, 1);

			//triangle 2
			verts[9] = new CustomVertex.PositionTextured(90, 0, 90, 0, 1);
			verts[10] = new CustomVertex.PositionTextured(90, 90, 0, 1, 0);
			verts[11] = new CustomVertex.PositionTextured(90, 90, 90, 1, 1);

			// 3rd facet --------------------------------------------------------- 
			//triangle 1
			verts[12] = new CustomVertex.PositionTextured(0, 0, 0, 0, 0);
			verts[13] = new CustomVertex.PositionTextured(90, 0, 0, 1, 0);
			verts[14] = new CustomVertex.PositionTextured(0, 0, 90, 0, 1);

			//triangle 2
			verts[15] = new CustomVertex.PositionTextured(0, 0, 90, 0, 1);
			verts[16] = new CustomVertex.PositionTextured(90, 0, 0, 1, 0);
			verts[17] = new CustomVertex.PositionTextured(90, 0, 90, 1, 1);


			// 4th facet --------------------------------------------------------- 
			//triangle 1
			verts[18] = new CustomVertex.PositionTextured(0, 0, 0, 0, 0);
			verts[19] = new CustomVertex.PositionTextured(0, 90, 0, 1, 0);
			verts[20] = new CustomVertex.PositionTextured(0, 0, 90, 0, 1);

			//triangle 2
			verts[21] = new CustomVertex.PositionTextured(0, 0, 90, 0, 1);
			verts[22] = new CustomVertex.PositionTextured(0, 90, 0, 1, 0);
			verts[23] = new CustomVertex.PositionTextured(0, 90, 90, 1, 1);

			// 5th facet --------------------------------------------------------- 
			//triangle 1
			verts[24] = new CustomVertex.PositionTextured(0, 0, 90, 0, 0);
			verts[25] = new CustomVertex.PositionTextured(90, 0, 90, 1, 0);
			verts[26] = new CustomVertex.PositionTextured(0, 90, 90, 0, 1);

			//triangle 2
			verts[27] = new CustomVertex.PositionTextured(0, 90, 90, 0, 1);
			verts[28] = new CustomVertex.PositionTextured(90, 0, 90, 1, 0);
			verts[29] = new CustomVertex.PositionTextured(90, 90, 90, 1, 1);

			// 6th facet --------------------------------------------------------- 
			//triangle 1
			verts[30] = new CustomVertex.PositionTextured(0, 90, 0, 0, 0);
			verts[31] = new CustomVertex.PositionTextured(90, 90, 0, 1, 0);
			verts[32] = new CustomVertex.PositionTextured(0, 90, 90, 0, 1);

			//triangle 2
			verts[33] = new CustomVertex.PositionTextured(0, 90, 90, 0, 1);
			verts[34] = new CustomVertex.PositionTextured(90, 90, 0, 1, 0);
			verts[35] = new CustomVertex.PositionTextured(90, 90, 90, 1, 1);

			buffer.SetData(verts, 0, LockFlags.None);
		}

		public void Render() {
			int Tick;
			if ((device==null)) return;

			// Move the cube automatically
			if (chkAuto.Checked) {
				Tick = Environment.TickCount;
				device.Transform.World = Matrix.RotationAxis(
					new Vector3((float)Math.Cos((double)Tick/3000.0F), 1, 
					(float)Math.Sin((double)Tick/3000.0F)), Tick/3000.0F);
			}
			device.Clear(ClearFlags.Target|ClearFlags.ZBuffer, Color.FromArgb(255, 0, 0, 255), 1.0F, 0);
			device.BeginScene();

			// Show one texture a time, in order to create the illusion of a walking guy
			device.SetTexture(0, textures[x]);
			x = (x == 9) ? 0 : x+1; //If x is 9, set to 0, otherwise increment x
			device.SetStreamSource(0, vertBuffer, 0);

			device.DrawPrimitives(PrimitiveType.TriangleList, 0, numVerts/3);
			device.EndScene();
			try {
				device.Present();
			}
			catch {
				// This can lead to an error if the window is closed while the scene is being rendered
			}
		}
	
		private void Transformations_ValueChanged(object sender, System.EventArgs e) {
			if(device != null) {
				device.Transform.World = Matrix.Identity;
				RotationMatrices((float)RotationX.Value, (float)RotationY.Value, (float)RotationZ.Value);
				TranslationMatrices((float)TranslationX.Value, (float)TranslationY.Value, (float)TranslationZ.Value);
				ScaleMatrices((float)ScaleX.Value, (float)ScaleY.Value, (float)ScaleZ.Value);
			}
		}

		//The following functions create the transformation matrices for each operation
		public void RotationMatrices(float x, float y, float z) {
			device.Transform.World = Matrix.Multiply(device.Transform.World, Matrix.RotationX((float)(x * Math.PI / 180)));
			device.Transform.World = Matrix.Multiply(device.Transform.World, Matrix.RotationY((float)(y * Math.PI / 180)));
			device.Transform.World = Matrix.Multiply(device.Transform.World, Matrix.RotationZ((float)(z * Math.PI / 180)));
		}

		public void TranslationMatrices(float x, float y, float z) {
			device.Transform.World = Matrix.Multiply(device.Transform.World, Matrix.Translation(x, y, z));
		}

		public void ScaleMatrices(float x, float y, float z) {
			device.Transform.World = Matrix.Multiply(device.Transform.World, Matrix.Scaling(x / 100, y / 100, z / 100));
		}

		private void MatrixControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			if(e.KeyCode == Keys.Escape) {
				endTest = true;
			}
		}

		private void chkAuto_CheckedChanged(object sender, System.EventArgs e) {
			// Forces the execution of the Transformations_ValueChanged event
			Transformations_ValueChanged(sender, e);
		}

		private void MatrixControl_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			DisposeD3D();
			endTest = true;
		}


		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.lblRotZ = new System.Windows.Forms.Label();
			this.lblRotY = new System.Windows.Forms.Label();
			this.LblRotX = new System.Windows.Forms.Label();
			this.chkAuto = new System.Windows.Forms.CheckBox();
			this.grpTranslation = new System.Windows.Forms.GroupBox();
			this.lblTranY = new System.Windows.Forms.Label();
			this.lblTranZ = new System.Windows.Forms.Label();
			this.lblTranX = new System.Windows.Forms.Label();

⌨️ 快捷键说明

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