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 Chapter5Code
{
/// COPYRIGHT: this code is taken from
/// Managed DirectX 9 Graphics and Game Programming, Kick Start
/// at Sams, by Tom Miller
///
///
http://www.amazon.com/Managed-Direc...=sr_1_1?ie=UTF8&s=books&qid=1264541659&sr=8-1
///
/// lightly modified to take into account
/// modifications having occured since the publication
/// of the book.
///
/// You can resize the window, but not reloading the textures, the
x-file will then stay black.
///
/// See the original code to get comments, or consult the book to get
extra explanations.
public class Form1 : System.Windows.Forms.Form
{
private Device device = null;
private Mesh mesh = null;
private Material[] meshMaterials;
private Texture[] meshTextures;
private System.ComponentModel.Container components = null;
private float angle = 0.0f;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.Opaque, true);
}
public void InitializeGraphics()
{
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);
// Load our mesh, our function here down
LoadMesh(@"..\..\tiny.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
= mtrl.Material3D;
if ((mtrl.TextureFilename != null) &&
(mtrl.TextureFilename != string.Empty))
{
// We have a texture, try to load it
meshTextures = TextureLoader.FromFile(device,
@"..\..\" + mtrl.TextureFilename);
}
}
}
}
private void SetupCamera()
{
device.Transform.Projection =
Matrix.PerspectiveFovLH((float)Math.PI / 4, ((float) this.Width) /
this.Height, 1.0f, 10000.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,
580.0f), new Vector3(0,0,0), new Vector3(0,1,0));
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(0, -1, -1);
device.Lights[0].Enabled = true;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs
e)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
Color.CornflowerBlue, 1.0f, 0);
SetupCamera();
device.BeginScene();
DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f,
angle / (float)Math.PI / 4.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)
{
angle += 0.01f;
device.Transform.World = Matrix.RotationYawPitchRoll(yaw,
pitch, roll) * Matrix.Translation(x, y, z);
for (int i = 0; i < meshMaterials.Length; i++)
{
device.Material = meshMaterials;
device.SetTexture(0, meshTextures);
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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new Size(800,600);
this.Text = "Form1";
}
#endregion
static void Main()
{
using (Form1 frm = new Form1())
{
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
}
}