モデルの三階層構造

Page creation date :

The page you are currently viewing does not support the selected display language.

前回は「モデルの二階層構造」について説明しましたが、今回は「三階層構造」について説明します。「二階層出来れば三階層説明しなくてもいいじゃないか!」と思うかもしれませんが、実は初心者にとって「三階層目」の計算の仕方がわからない、という方も実は多いかと思うのです。

「三番目は一番目のマトリックスを引き継ぐの?」「それとも2番目?」「はたまた1番と2番をあわせるの?」などなど。もちろんこれが出来れば「四階層」以降は間単に出来ます。

ちなみに三階層の各モデルをそれぞれ「」「」「」と呼ぶことにします。(こういう決まりがあるわけではありません。この Tips のみの呼び方です。

操作は親のボックス「↑」「↓」キーで X 軸回転「←」「→」キーで Z 軸回転します。全てワールド座標で回転します。

子と孫のボックスは自動で自分の親のボックスの先で回転するようになっています。

赤ボックスが親青ボックスが子緑ボックスが孫です。

モデルの三階層構造

今回のメインコードファイルを載せます。

MainSample.cs

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 Mesh _box = null;

        /// <summary>
        /// 親ボックスの回転マトリックス
        /// <summary>
        private Matrix _parentBoxRotate = Matrix.Identity;


        /// <summary>
        /// アプリケーションの初期化
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        /// <returns>全ての初期化がOKなら true, ひとつでも失敗したら false を返すようにする</returns>
        /// <remarks>
        /// false を返した場合は、自動的にアプリケーションが終了するようになっている
        /// </remarks>
        public bool InitializeApplication(MainForm topLevelForm)
        {
            // フォームの参照を保持
            this._form = topLevelForm;

            // 入力イベント作成
            this.CreateInputEvent(topLevelForm);

            try
            {
                // Direct3D デバイス作成
                this.CreateDevice(topLevelForm);

                // フォントの作成
                this.CreateFont();

                // XYZライン作成
                this.CreateXYZLine();
            }
            catch (DirectXException ex)
            {
                // 例外発生
                MessageBox.Show(ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            // カメラの位置セット
            this.SetCameraPosition(8.0f, 270.0f, 30.0f);


            // ボックス作成
            this._box = Mesh.Box(this._device, 0.5f, 2.0f, 0.5f);


            // ライトの設定
            this.SettingLight();

            return true;
        }

        /// <summary>
        /// デバイスがロストしたとき
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_DeviceLost(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// デバイスがリセットしたとき
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_DeviceReset(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 更新処理
        /// </summary>
        public void Update()
        {
            // アプリケーションの終了操作
            if (this._keys[(int)Keys.Escape])
            {
                this._form.Close();
                return;
            }

            // カメラの設定
            this.SettingCamera();


            // 親ボックスの回転
            if (this._keys[(int)Keys.Down])
            {
                this._parentBoxRotate *= Matrix.RotationX(-0.1f);
            }
            if (this._keys[(int)Keys.Up])
            {
                this._parentBoxRotate *= Matrix.RotationX(0.1f);
            }
            if (this._keys[(int)Keys.Left])
            {
                this._parentBoxRotate *= Matrix.RotationZ(0.1f);
            }
            if (this._keys[(int)Keys.Right])
            {
                this._parentBoxRotate *= Matrix.RotationZ(-0.1f);
            }
        }

        /// <summary>
        /// 描画処理
        /// </summary>
        public void Draw()
        {
            // デバイスが使える状態か確認する
            if (!this.EnsureDevice())
            {
                return;
            }

            // 描画内容を単色でクリアし、Zバッファもクリア
            this._device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, Color.DarkBlue, 1.0f, 0);

            // 「BeginScene」と「EndScene」の間に描画内容を記述する
            this._device.BeginScene();


            // XYZライン描画
            this.RenderXYZLine();


            // マテリアル初期化
            Material mtrl = new Material();
            mtrl.Ambient = Color.FromArgb(255, 128, 128, 128);

            
            // マテリアル
            mtrl.Diffuse = Color.Red;
            this._device.Material = mtrl;

            // 親のマトリックス初期化
            Matrix parentBoxMatrix = Matrix.Identity;

            // 回転軸移動
            parentBoxMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

            // 回転
            parentBoxMatrix *= this._parentBoxRotate;

            // 座標変換
            this._device.SetTransform(TransformType.World, parentBoxMatrix);

            // 親ボックス描画
            this._box.DrawSubset(0);


            // マテリアル
            mtrl.Diffuse = Color.Blue;
            this._device.Material = mtrl;

            // 子のマトリックス初期化
            Matrix childBoxMatrix = Matrix.Identity;

            // 回転軸移動
            childBoxMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

            // 初期回転位置(傾き)
            childBoxMatrix *= Matrix.RotationZ((float)Math.PI / 2.0f);

            // 自動で回転
            childBoxMatrix *= Matrix.RotationY(Environment.TickCount / 1000.0f);

            // 親のボックスの一番上にくっつくよう移動
            childBoxMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

            // 親のマトリックスを受け継ぐ
            childBoxMatrix *= parentBoxMatrix;

            // 座標変換
            this._device.SetTransform(TransformType.World, childBoxMatrix);

            // 子ボックス描画
            this._box.DrawSubset(0);


            // マテリアル
            mtrl.Diffuse = Color.Green;
            this._device.Material = mtrl;

            // 孫のマトリックス初期化
            Matrix grandChildSphereMatrix = Matrix.Identity;

            // 回転軸移動
            grandChildSphereMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

            // 初期回転位置(傾き)
            grandChildSphereMatrix *= Matrix.RotationZ((float)Math.PI / 2.0f);

            // 自動で回転
            grandChildSphereMatrix *= Matrix.RotationY(Environment.TickCount / 200.0f);

            // 自分の親(子)のボックスの一番上にくっつくよう移動
            grandChildSphereMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

            // 自分の親(子)のマトリックスを受け継ぐ
            grandChildSphereMatrix *= childBoxMatrix;

            // 座標変換
            this._device.SetTransform(TransformType.World, grandChildSphereMatrix);

            // 孫ボックス描画
            this._box.DrawSubset(0);


            // 文字列の描画
            this.RenderText();

            // 描画はここまで
            this._device.EndScene();

            // 実際のディスプレイに描画
            this._device.Present();
        }

        /// <summary>
        /// テキストの描画
        /// </summary>
        private void RenderText()
        {
            this.DrawText("[Escape]終了", 0, 0, Color.White);
            this.DrawText("θ:" + this._lensPosTheta, 0, 12, Color.White);
            this.DrawText("φ:" + this._lensPosPhi, 0, 24, Color.White);
            this.DrawText("parentMatrix:" + this._parentBoxRotate.ToString(), 0, 36, Color.White);
        }

        /// <summary>
        /// リソースの破棄をするために呼ばれる
        /// </summary>
        public void Dispose()
        {
            // 作成したメッシュの破棄
            this.SafeDispose(this._box);
            
            // リソースの破棄
            this.DisposeResource();
        }
    }
}
MainSamplePartial.cs ファイルのコードはこちらです。

MainSamplePartial.cs

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
{
    public partial class MainSample
    {
        /// <summary>
        /// メインフォーム
        /// </summary>
        private MainForm _form = null;

        /// <summary>
        /// Direct3D デバイス
        /// </summary>
        private Device _device = null;

        /// <summary>
        /// Direct3D 用フォント
        /// </summary>
        private Microsoft.DirectX.Direct3D.Font _font = null;

        /// <summary>
        /// キーのプレス判定
        /// </summary>
        private bool[] _keys = new bool[256];

        /// <summary>
        /// 1つ前のマウスの位置
        /// </summary>
        private Point _oldMousePoint = Point.Empty;

        /// <summary>
        /// カメラレンズの位置(R)
        /// </summary>
        private float _lensPosRadius = 5.0f;

        /// <summary>
        /// カメラレンズの位置(θ)
        /// </summary>
        private float _lensPosTheta = 300.0f;

        /// <summary>
        /// カメラレンズの位置(φ)
        /// </summary>
        private float _lensPosPhi = 30.0f;

        /// <summary>
        /// XYZライン用頂点バッファ
        /// </summary>
        private VertexBuffer _xyzLineVertexBuffer = null;


        /// <summary>
        /// 入力イベント作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        private void CreateInputEvent(MainForm topLevelForm)
        {
            // キーイベント作成
            topLevelForm.KeyDown += new KeyEventHandler(this.form_KeyDown);
            topLevelForm.KeyUp += new KeyEventHandler(this.form_KeyUp);

            // マウス移動イベント
            topLevelForm.MouseMove += new MouseEventHandler(this.form_MouseMove);
        }

        /// <summary>
        /// キーボードのキーを押した瞬間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_KeyDown(object sender, KeyEventArgs e)
        {
            // 押されたキーコードのフラグを立てる
            if ((int)e.KeyCode < this._keys.Length)
            {
                this._keys[(int)e.KeyCode] = true;
            }
        }
        /// <summary>
        /// キーボードのキーを放した瞬間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_KeyUp(object sender, KeyEventArgs e)
        {
            // 放したキーコードのフラグを下ろす
            if ((int)e.KeyCode < this._keys.Length)
            {
                this._keys[(int)e.KeyCode] = false;
            }
        }

        /// <summary>
        /// マウス移動イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 回転
                this._lensPosTheta -= e.Location.X - this._oldMousePoint.X;
                this._lensPosPhi += e.Location.Y - this._oldMousePoint.Y;

                // φに関しては制限をつける
                if (this._lensPosPhi >= 90.0f)
                {
                    this._lensPosPhi = 89.9999f;
                }
                else if (this._lensPosPhi <= -90.0f)
                {
                    this._lensPosPhi = -89.9999f;
                }
            }
            // マウスの位置を記??
            this._oldMousePoint = e.Location;
        }

        /// <summary>
        /// Direct3D デバイスの作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        private void CreateDevice(MainForm topLevelForm)
        {
            // PresentParameters。デバイスを作成する際に必須
            // どのような環境でデバイスを使用するかを設定する
            PresentParameters pp = new PresentParameters();

            // ウインドウモードなら true、フルスクリーンモードなら false を指定
            pp.Windowed = true;

            // スワップ効果。とりあえず「Discard」を指定。
            pp.SwapEffect = SwapEffect.Discard;

            // 深度ステンシルバッファ。3Dでは前後関係があるので通常 true
            pp.EnableAutoDepthStencil = true;

            // 自動深度ステンシル サーフェイスのフォーマット。
            // 「D16」に対応しているビデオカードは多いが、前後関係の精度があまりよくない。
            // できれば「D24S8」を指定したいところ。
            pp.AutoDepthStencilFormat = DepthFormat.D16;

            try
            {
                // デバイスの作成
                this.CreateDevice(topLevelForm, pp);
            }
            catch (DirectXException ex)
            {
                // 例外発生
                throw ex;
            }
        }
        /// <summary>
        /// Direct3D デバイスの作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        /// <param name="presentationParameters">PresentParameters 構造体</param>
        private void CreateDevice(MainForm topLevelForm, PresentParameters presentationParameters)
        {
            // 実際にデバイスを作成します。
            // 常に最高のパフォーマンスで作成を試み、
            // 失敗したら下位パフォーマンスで作成するようにしている。
            try
            {
                // ハードウェアによる頂点処理、ラスタライズを行う
                // 最高のパフォーマンスで処理を行えます。
                // ビデオカードによっては実装できない処理が存在します。
                this._device = new Device(0, DeviceType.Hardware, topLevelForm.Handle,
                    CreateFlags.HardwareVertexProcessing, presentationParameters);
            }
            catch (DirectXException ex1)
            {
                // 作成に失敗
                Debug.WriteLine(ex1.ToString());
                try
                {
                    // ソフトウェアによる頂点処理、ハードウェアによるラスタライズを行う
                    this._device = new Device(0, DeviceType.Hardware, topLevelForm.Handle,
                        CreateFlags.SoftwareVertexProcessing, presentationParameters);
                }
                catch (DirectXException ex2)
                {
                    // 作成に失敗
                    Debug.WriteLine(ex2.ToString());
                    try
                    {
                        // ソフトウェアによる頂点処理、ラスタライズを行う
                        // パフォーマンスはとても低いです。
                        // その代わり、ほとんどの処理を制限なく行えます。
                        this._device = new Device(0, DeviceType.Reference, topLevelForm.Handle,
                            CreateFlags.SoftwareVertexProcessing, presentationParameters);
                    }
                    catch (DirectXException ex3)
                    {
                        // 作成に失敗
                        // 事実上デバイスは作成できません。
                        throw ex3;
                    }
                }
            }

            // デバイスの状態変更イベントを作成
            this._device.DeviceLost += new EventHandler(this.device_DeviceLost);
            this._device.DeviceReset += new EventHandler(this.device_DeviceReset);
        }

        /// <summary>
        /// フォントの作成
        /// </summary>
        private void CreateFont()
        {
            try
            {
                // フォントデータの構造体を作成
                FontDescription fd = new FontDescription();

                // 構造体に必要なデータをセット
                fd.Height = 12;
                fd.FaceName = "MS ゴシック";

                // フォントを作成
                this._font = new Microsoft.DirectX.Direct3D.Font(this._device, fd);
            }
            catch (DirectXException ex)
            {
                // 例外発生
                throw ex;
            }
        }

        /// <summary>
        /// XYZライン作成
        /// </summary>
        private void CreateXYZLine()
        {
            // 6つ分の頂点を作成
            this._xyzLineVertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
                6, this._device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);

            // 頂点バッファをロックして、位置、色情報を書き込む
            using (GraphicsStream data = this._xyzLineVertexBuffer.Lock(0, 0, LockFlags.None))
            {
                // 今回は各XYZのラインを原点(0.0f, 0.0f, 0.0f)からプラス方向に 10.0f 伸びた線を作成
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Red.ToArgb()));
                data.Write(new CustomVertex.PositionColored(10.0f, 0.0f, 0.0f, Color.Red.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Green.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 10.0f, 0.0f, Color.Green.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Blue.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 10.0f, Color.Blue.ToArgb()));

                this._xyzLineVertexBuffer.Unlock();
            }
        }

        /// <summary>
        /// カメラの位置をセット
        /// </summary>
        /// <param name="radius">半径(degree)</param>
        /// <param name="theta">水平方向角度(degree)</param>
        /// <param name="phi">垂直方向角度(degree)</param>
        private void SetCameraPosition(float radius, float theta, float phi)
        {
            this._lensPosRadius = radius;
            this._lensPosTheta = theta;
            this._lensPosPhi = phi;
        }

        /// <summary>
        /// ライトの設定
        /// </summary>
        private void SettingLight()
        {
            // 平行光線を使用
            this._device.Lights[0].Type = LightType.Directional;

            // ライトの方向
            this._device.Lights[0].Direction = new Vector3(1.0f, -1.5f, 2.0f);

            // 光の色は白
            this._device.Lights[0].Diffuse = Color.White;

            // 環境光
            this._device.Lights[0].Ambient = Color.FromArgb(255, 128, 128, 128);

            // 0 番のライトを有効
            this._device.Lights[0].Enabled = true;

            // 0 番のライトを更新
            this._device.Lights[0].Update();
        }

        /// <summary>
        /// カメラの設定
        /// </summary>
        private void SettingCamera()
        {
            // レンズの位置を三次元極座標で変換
            float radius = this._lensPosRadius;
            float theta = Geometry.DegreeToRadian(this._lensPosTheta);
            float phi = Geometry.DegreeToRadian(this._lensPosPhi);
            Vector3 lensPosition = new Vector3(
                (float)(radius * Math.Cos(theta) * Math.Cos(phi)),
                (float)(radius * Math.Sin(phi)),
                (float)(radius * Math.Sin(theta) * Math.Cos(phi)));

            // ビュー変換行列を設定
            this._device.Transform.View = Matrix.LookAtLH(
                lensPosition, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));

            // 射影変換を設定
            this._device.Transform.Projection = Matrix.PerspectiveFovLH(
                Geometry.DegreeToRadian(60.0f),
                (float)this._device.Viewport.Width / (float)this._device.Viewport.Height,
                1.0f, 100.0f);
        }

        /// <summary>
        /// デバイスを保証する
        /// </summary>
        /// <returns></returns>
        private bool EnsureDevice()
        {
            // デバイスが動作可能かチェック
            int deviceResult;
            if (!this._device.CheckCooperativeLevel(out deviceResult))
            {
                switch ((ResultCode)deviceResult)
                {
                    case ResultCode.DeviceLost:
                        // まだリセットできる状態ではないので少し待つ
                        System.Threading.Thread.Sleep(10);
                        return false;
                    case ResultCode.DeviceNotReset:
                        // リセット可能状態

                        // デバイスをリセット
                        this._device.Reset(this._device.PresentationParameters);
                        return true;
                    default:
                        // 原因不明(正確には上記以外)
                        // まだリセットできる状態ではないので少し待つ
                        System.Threading.Thread.Sleep(10);
                        return false;
                }
            }
            return true;
        }

        /// <summary>
        /// XYZライン描画
        /// </summary>
        private void RenderXYZLine()
        {
            // ライトの状態を記憶
            bool oldLightEnable = this._device.RenderState.Lighting;

            // ライトを無効
            if (oldLightEnable)
            {
                this._device.RenderState.Lighting = false;
            }

            // テクスチャー無効
            this._device.SetTexture(0, null);

            // 原点に配置
            this._device.SetTransform(TransformType.World, Matrix.Identity);

            // ストリームセット
            this._device.SetStreamSource(0, this._xyzLineVertexBuffer, 0);

            // 頂点フォーマットセット
            this._device.VertexFormat = CustomVertex.PositionColored.Format;

            // 描画
            this._device.DrawPrimitives(PrimitiveType.LineList, 0, 3);

            // ライトを戻す
            if (oldLightEnable)
            {
                this._device.RenderState.Lighting = oldLightEnable;
            }
        }

        /// <summary>
        /// テキストの描画
        /// </summary>
        /// <param name="text">描画する文字列</param>
        /// <param name="left">文字列の左位置</param>
        /// <param name="top">文字列の上位置</param>
        /// <param name="color">文字の色</param>
        private void DrawText(string text, int left, int top, Color color)
        {
            this._font.DrawText(null, text, left, top, color);
        }

        /// <summary>
        /// リソースの破棄
        /// </summary>
        private void DisposeResource()
        {
            // XYZラインの破棄
            this.SafeDispose(this._xyzLineVertexBuffer);

            // フォントのリソース解放
            this.SafeDispose(this._font);

            // Direct3D デバイスのリソース解放
            this.SafeDispose(this._device);
        }

        /// <summary>
        /// 安全なリソース破棄
        /// </summary>
        /// <param name="resource">破棄するリソース</param>
        private void SafeDispose(IDisposable resource)
        {
            if (resource != null)
            {
                resource.Dispose();
            }
        }
    }
}

ほとんどのコードは前回と同様なので詳しくは「モデルの二階層構造」を参照してください。今回は異なる点だけ説明します。


// マテリアル
mtrl.Diffuse = Color.Green;
this._device.Material = mtrl;

// 孫のマトリックス初期化
Matrix grandChildSphereMatrix = Matrix.Identity;

// 回転軸移動
grandChildSphereMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

// 初期回転位置(傾き)
grandChildSphereMatrix *= Matrix.RotationZ((float)Math.PI / 2.0f);

// 自動で回転
grandChildSphereMatrix *= Matrix.RotationY(Environment.TickCount / 200.0f);

// 自分の親(子)のボックスの一番上にくっつくよう移動
grandChildSphereMatrix *= Matrix.Translation(0.0f, 1.0f, 0.0f);

// 自分の親(子)のマトリックスを受け継ぐ
grandChildSphereMatrix *= childBoxMatrix;

// 座標変換
this._device.SetTransform(TransformType.World, grandChildSphereMatrix);

// 孫ボックス描画
this._box.DrawSubset(0);

今回は孫のメッシュを追加していますが、前回と変わっている部分はここだけです。前回の Tips にも書きましたが、親は子の存在を知らなくてもいいので、孫は勝手に親にくっついてしまえばいいたけのことです。

孫の自動回転に関しては「子(孫の親)」とまったく同じなので、計算理論を詳しく知りたい場合は前回の Tips を参考にしてください。

ここで重要なので下から3行目のたった1行だけです。「孫は親達のどの値を受け継ぐか」です。答えはコードにも書いてあるとおりですが「子(孫の親)の座標変換マトリックス」を掛け合わせればいいのです。「子自体が持つローカルマトリックス」ではないので、そこを注意してください。

分かりやすく書けば下のようになります。

親ボックスの座標変換マトリックス 親の座標変換マトリックス(ローカル)
子ボックスの座標変換マトリックス 子の座標変換マトリックス(ローカル)×親の座標変換マトリックス
孫ボックスの座標変換マトリックス 孫の座標変換マトリックス(ローカル)×子の座標変換マトリックス

または

孫の座標変換マトリックス(ローカル)×子の座標変換マトリックス(ローカル)×親の座標変換マトリックス