/****************************************************/
/*★ Men4.x の Quaternion を使って回転 前田 稔 ★*/
/****************************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;
using System.Collections;
using System.IO;
namespace Anime
{
public class Meshes : Form
{
Device device = null; // Our rendering device
Mesh mesh = null; // Our mesh object in sysmem
Direct3D.Material[] meshMaterials; // Materials for our mesh
Texture[] meshTextures; // Textures for our mesh
PresentParameters presentParams = new PresentParameters();
bool pause = false;
InterRot interrot; //Interpolator の定義
X_Loader x_load; //X-File Load Class の定義
//回転情報の定義
InterRotDat[] Rot;
int tim= 0;
Matrix mat;
//Constructor
public Meshes()
{ // Set the initial size of our form
this.ClientSize = new System.Drawing.Size(400, 400);
this.Text = "Direct3D Teapot";
}
//D3DDevice の取得
bool InitializeGraphics()
{
try
{ presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
// Create the D3DDevice
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
this.OnResetDevice(device, null);
pause = false;
}
catch (DirectXException)
{ return false; }
return true;
}
//Direct3D の初期化
public void OnResetDevice(object sender, EventArgs e)
{
ExtendedMaterial[] materials = null;
Directory.SetCurrentDirectory(@"c:\data\xfile\");
Device dev = (Device)sender;
dev.RenderState.ZBufferEnable = true;
dev.RenderState.Ambient = System.Drawing.Color.White;
mesh = Mesh.FromFile("Men4Rot.x", MeshFlags.SystemMemory, device, out materials);
meshMaterials = new Material[materials.Length];
meshTextures = new Texture[materials.Length];
for(int i = 0; i < meshMaterials.Length; i++)
{ meshMaterials[i] = materials[i].Material3D;
meshMaterials[i].AmbientColor = meshMaterials[i].DiffuseColor;
if ((materials[i].TextureFilename != null) && (materials[i].TextureFilename.Length > 0))
{ meshTextures[i] = TextureLoader.FromFile(dev, materials[i].TextureFilename); }
}
// 法線情報がなければ計算して作成
if ((mesh.VertexFormat & VertexFormats.Normal) == 0)
{ Mesh temporaryMesh = mesh.Clone(mesh.Options.Value,
mesh.VertexFormat | VertexFormats.Normal, device);
// 法線を計算
temporaryMesh.ComputeNormals();
// 古いメッシュを破棄し、置き換える
mesh.Dispose();
mesh = temporaryMesh;
}
// アニメーションデータの入力
x_load = new X_Loader(@"c:\data\xfile\Men4Rot.x");
Rot = x_load.LoadRot();
// 回転 Interpolator の設定
interrot = new InterRot(Rot);
}
//描画環境の設定
void SetupMatrices()
{
//mat= interrot.GetMat(tim);
mat= interrot.GetMat(Environment.TickCount*4);
device.Transform.World = mat;
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f,1.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,500.0f);
}
//ライトの設定
private void SetupLights()
{ System.Drawing.Color col = System.Drawing.Color.White;
Direct3D.Material mtrl = new Direct3D.Material();
mtrl.Diffuse = col;
mtrl.Ambient = col;
device.Material = mtrl;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = System.Drawing.Color.FromArgb(0xF0F020);
device.Lights[0].Direction = new Vector3(50.0f, -40.0f, 100.0f);
device.Lights[0].Enabled = true;
device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);
}
//モデルの描画
private void Render()
{ if (device == null) return;
if (pause) return;
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Gray, 1.0f, 0);
device.BeginScene();
SetupLights();
SetupMatrices();
for(int i=0; i<meshMaterials.Length; i++)
{ device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
mesh.DrawSubset(i);
}
device.EndScene();
device.Present();
}
//マウスのクリックを検出
protected override void OnMouseDown(MouseEventArgs e)
{ tim+= 20; } //アニメーションの間隔
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{ this.Render(); }
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{ if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Dispose(); // Esc was pressed
}
protected override void OnResize(System.EventArgs e)
{ pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible); }
//☆ Main() メソッド
static void Main()
{ using (Meshes frm = new Meshes())
{ if (!frm.InitializeGraphics()) // Initialize Direct3D
{ MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return;
}
frm.Show();
// While the form is still valid, render and process messages
while (frm.Created)
{ frm.Render();
Application.DoEvents();
}
}
}
}
}
|