using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace MDXSample
{
/// <summary>
///
/// </summary>
public partial class MainSample : IDisposable
{
/// <summary>
///
/// </summary>
private Vector3 _trans = Vector3.Empty;
/// <summary>
///
/// </summary>
private float _rotate = 0.0f;
/// <summary>
///
/// </summary>
private Vector3 _scale = new Vector3(1.0f, 1.0f, 1.0f);
/// <summary>
///
/// </summary>
/// <param name="topLevelForm"></param>
/// <returns></returns>
/// <remarks>
///
/// </remarks>
public bool InitializeApplication(MainForm topLevelForm)
{
this._form = topLevelForm;
this.CreateInputEvent(topLevelForm);
// PresentParameters。デバイスを作成する際に必須
PresentParameters pp = new PresentParameters();
// ウインドウモードなら true、フルスクリーンモードなら false を指定
pp.Windowed = false;
// スワップ効果
pp.SwapEffect = SwapEffect.Discard;
// 深度ステンシルバッファ
pp.EnableAutoDepthStencil = true;
// 自動深度ステンシル サーフェイスのフォーマット
pp.AutoDepthStencilFormat = DepthFormat.D16;
// 使用できるディスプレイモードを検索し、目的のモードを探す
bool flag = false;
// ディプレイモードを列挙し、サイズが「640×480」かつ
// リフレッシュレートが「60」のモードを探す
// (条件はアプリケーションの内容によって変えてください)
foreach (DisplayMode i in Manager.Adapters[0].SupportedDisplayModes)
{
if (i.Width == 640 && i.Height == 480 && i.RefreshRate == 60)
{
// 条件に見合えば使用する
pp.BackBufferWidth = 640;
pp.BackBufferHeight = 480;
pp.BackBufferFormat = i.Format;
pp.FullScreenRefreshRateInHz = 60;
// 見つかったことを示すフラグを立てる
flag = true;
break;
}
}
if (!flag)
{
// 目的のモードがなければそのまま終了
MessageBox.Show("指定したディプレイモードは見つかりませんでした。",
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
try
{
// Direct3D デバイス作成
this.CreateDevice(topLevelForm, pp);
this.CreateFont();
this.LoadXFileMesh("Deruderu.x");
}
catch (DirectXException ex)
{
MessageBox.Show(ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
this.CreateXYZLine();
this.SettingLight();
this._device.RenderState.NormalizeNormals = true;
return true;
}
/// <summary>
///
/// </summary>
public void MainLoop()
{
this.SettingCamera();
if (this._keys[(int)Keys.Down])
{
this._trans -= Vector3.TransformCoordinate(new Vector3(0.0f, 0.0f, -0.3f),
Matrix.RotationY(Geometry.DegreeToRadian(this._rotate)));
}
if (this._keys[(int)Keys.Up])
{
this._trans += Vector3.TransformCoordinate(new Vector3(0.0f, 0.0f, -0.3f),
Matrix.RotationY(Geometry.DegreeToRadian(this._rotate)));
}
if (this._keys[(int)Keys.Left])
{
this._rotate -= 5.0f;
}
if (this._keys[(int)Keys.Right])
{
this._rotate += 5.0f;
}
if (this._keys[(int)Keys.Z])
{
this._scale.X /= 1.01f;
this._scale.Y /= 1.01f;
this._scale.Z /= 1.01f;
}
if (this._keys[(int)Keys.A])
{
this._scale *= 1.01f;
}
Matrix modelTransform = Matrix.Identity;
modelTransform *= Matrix.Scaling(this._scale);
modelTransform *= Matrix.RotationY(Geometry.DegreeToRadian(this._rotate));
modelTransform *= Matrix.Translation(this._trans);
this._device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkBlue, 1.0f, 0);
this._device.BeginScene();
this._device.RenderState.Lighting = false;
this._device.SetTransform(TransformType.World, Matrix.Identity);
this.RenderXYZLine();
this._device.RenderState.Lighting = true;
this._device.SetTransform(TransformType.World, modelTransform);
this.RenderMesh();
this._font.DrawText(null, "[Escape]終了", 0, 0, Color.White);
this._font.DrawText(null, "[←→]回転 [↑↓]移動 [ZA]拡大縮小", 0, 12, Color.White);
this._font.DrawText(null, "θ:" + this._lensPosTheta, 0, 24, Color.White);
this._font.DrawText(null, "φ:" + this._lensPosPhi, 0, 36, Color.White);
this._font.DrawText(null,
"移動:"+this._trans.X+","+this._trans.Y+","+this._trans.Z, 0, 48, Color.White);
this._font.DrawText(null, "回転:" + this._rotate, 0, 60, Color.White);
this._font.DrawText(null, "拡大:" + this._scale.X, 0, 72, Color.White);
this._device.EndScene();
this._device.Present();
// アプリケーションの終了操作
if (this._keys[(int)Keys.Escape])
{
this._form.Close();
}
}
/// <summary>
///
/// </summary>
public void Dispose()
{
this.DisposeMesh();
this.DisposeXYZLine();
if (this._font != null)
{
this._font.Dispose();
}
if (this._device != null)
{
this._device.Dispose();
}
}
}
}
|