Code:
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 WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
Device device = null;
VertexBuffer vertexBuffer = null;
Texture texture = null;
bool pause = false;
Mesh mesh = null;
Microsoft.DirectX.Direct3D.Material[] meshMaterials;
Texture[] meshTextures;
public bool Init_3d()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
this.OnResetDevice(device, null);
this.OnCreateDevice(device, null);
pause = false;
return true;
}
catch
{
return false;
}
}
public void OnCreateDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormal), 100, dev, Usage.WriteOnly, CustomVertex.PositionNormal.Format, Pool.Default);
vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);
this.OnCreateVertexBuffer(vertexBuffer, null);
}
private void SetupMatrices()
{
device.Transform.View = Matrix.LookAtLH(
new Vector3( 0.0f, 0.0f,-5.0f ),
new Vector3( 0.0f, 0.0f, 0.0f ),
new Vector3( 0.0f, 1.0f, 0.0f ) );
device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4, 1.0f, 1.0f, 100.0f );
}
private void SetupLights()
{
System.Drawing.Color col = System.Drawing.Color.White;
Microsoft.DirectX.Direct3D.Material mtrl = new Microsoft.DirectX.Direct3D.Material();
mtrl.Diffuse = col;
mtrl.Ambient = col;
device.Material = mtrl;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = col;
device.Lights[0].Direction = new Vector3(-1.0f,0.0f,0.0f);
device.Lights[0].Position = new Vector3(0.0f, -5.0f, 0.0f);
device.Lights[0].Range = 1000.0f;
device.Lights[0].Commit();
device.Lights[0].Enabled = true;
device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render();
}
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
dev.RenderState.CullMode = Cull.None;
dev.RenderState.ZBufferEnable = true;
dev.RenderState.Lighting = true;
texture = TextureLoader.FromFile(dev,"me.bmp");
ExtendedMaterial[] materials = null;
mesh = Mesh.FromFile("tiger.x",
MeshFlags.SystemMemory,
dev,
out materials);
if (meshTextures == null)
{
// Extract the material properties and texture names.
meshTextures = new Texture[materials.Length];
meshMaterials = new Microsoft.DirectX.Direct3D.Material[materials.Length];
for( int i=0; i<materials.Length; i++ )
{
meshMaterials[i] = materials[i].Material3D;
// Set the ambient color for the material. Direct3D
// does not do this by default.
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
// Create the texture.
meshTextures[i] = TextureLoader.FromFile(dev,
materials[i].TextureFilename);
}
}
}
public void OnCreateVertexBuffer(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
// Create a vertex buffer (100 customervertex)
CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vb.Lock(0,0); // Lock the buffer (which will return our structs)
for (int i = 0; i < 50; i++)
{
// Fill up our structs
float theta = (float)(2 * Math.PI * i) / 49;
verts[2 * i].SetPosition(new Vector3((float)Math.Sin(theta), -1, (float)Math.Cos(theta)));
verts[2 * i].SetNormal(new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta)));
verts[2 * i + 1].SetPosition(new Vector3((float)Math.Sin(theta), 1, (float)Math.Cos(theta)));
verts[2 * i + 1].SetNormal(new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta)));
}
// Unlock (and copy) the data
vb.Unlock();
}
private void Render()
{
if (pause)
return;
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
device.BeginScene();
SetupLights();
SetupMatrices();
for( int i=0; i<meshMaterials.Length; i++ )
{
// Set the material and texture for this subset.
device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
// Draw the mesh subset.
mesh.DrawSubset(i);
}
device.EndScene();
device.Present();
}
public Form1()
{
//
// 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 );
}
#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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using(Form1 form = new Form1())
{
if(!form.Init_3d())
{
MessageBox.Show("Init_3d Failed");
return;
}
form.Show();
while(form.Created)
{
form.Render();
Application.DoEvents();
}
}
}
private void Form1_Resize(object sender, System.EventArgs e)
{
pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
}
}
}