using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Microsoft; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using Microsoft.DirectX.DirectInput; namespace DrawMesh { public class Form1 : System.Windows.Forms.Form { private Microsoft.DirectX.Direct3D.Device device = null; private Mesh mesh = null; private Material[] meshMaterials; private Texture[] meshTextures; private Microsoft.DirectX.DirectInput.Device mouse; private System.ComponentModel.Container components = null; private float angle = 0.0f; private float fov = 1.0f; private bool movingIn; Vector3 cameraPosition = new Vector3(0.0f, 2.0f, 3.0f); Vector3 origin = new Vector3(0.0f, 0.0f, 0.0f); public Form1() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); } public void InitializeGraphics() { // Set our presentation parameters PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; // Create our device device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); // Load our mesh LoadMesh(@"dino.x"); } private void LoadMesh(string file) { ExtendedMaterial[] mtrl; // Load our mesh mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl); // If we have any materials, store them if ((mtrl != null) && (mtrl.Length > 0)) { meshMaterials = new Material[mtrl.Length]; meshTextures = new Texture[mtrl.Length]; // Store each material and texture for (int i = 0; i < mtrl.Length; i++) { meshMaterials[i] = mtrl[i].Material3D; if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty)) { // We have a texture, try to load it meshTextures[i] = TextureLoader.FromFile(device, mtrl[i].TextureFilename); } } } } private void SetupMouse() { mouse = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse); mouse.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); mouse.Acquire(); } private void SetupCamera() { // (float)Math.PI / 4, this.Width / this.Height device.Transform.Projection = Matrix.PerspectiveFovLH( fov, this.Width / this.Height, 1.0f, 1000.0f); device.Transform.View = Matrix.LookAtLH(cameraPosition, origin, new Vector3(0,1,0)); //device.RenderState.Ambient = Color.FromArgb(90, 90, 90); device.RenderState.Lighting = true; device.Lights[0].Type = LightType.Point; device.Lights[0].Diffuse = Color.White; device.Lights[0].Range = 10; device.Lights[0].Position = new Vector3(0.0f, -1.0f, 1.0f); device.Lights[0].Direction = new Vector3(0, 0, 0); device.Lights[0].Update(); device.Lights[0].Enabled = true; device.Lights[1].Type = LightType.Point; device.Lights[1].Diffuse = Color.AliceBlue; device.Lights[1].Range = 12; device.Lights[1].Position = new Vector3(-1.0f, 0.0f, 1.0f); device.Lights[1].Direction = new Vector3(0, 0, 0); device.Lights[1].Update(); device.Lights[1].Enabled = true; } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSeaGreen, 1.0f, 0); SetupCamera(); SetupMouse(); device.BeginScene(); // Draw our Mesh DrawMesh(angle / (float)Math.PI, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); device.EndScene(); device.Present(); this.Invalidate(); } private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z) { MouseState clicks = new MouseState(); clicks = mouse.CurrentMouseState; byte[] buttons = clicks.GetMouseButtons(); if (0 != buttons[0]) fov = fov - 0.005f; if (0 != buttons[1]) fov = fov + 0.005f; angle += 0.03f; device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z); for (int i = 0; i < meshMaterials.Length; i++) { device.Material = meshMaterials[i]; device.SetTexture(0, meshTextures[i]); mesh.DrawSubset(i); } } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.SuspendLayout(); // // Form1 // this.ClientSize = new System.Drawing.Size(792, 566); this.Name = "Form1"; this.Text = "Sean Carrica Mesh Assignment"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion static void Main() { using (Form1 frm = new Form1()) { // Show our form and initialize our graphics engine frm.Show(); frm.InitializeGraphics(); Application.Run(frm); } } private void Form1_Load(object sender, EventArgs e) { } } }