[Atelier Blue アトリエブルー]HomeプログラミングManaged DirectX3D>描画先を指定する(複数コントロールへの描画)

描画先を指定する(複数コントロールへの描画)

このテクニックは、「Device.Present」を呼び出すときにコントロールを渡すことで実現できます。

サンプル

ソースコード

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Project2
{
    /// <summary>
    /// md3d2 の概要の説明です。
    /// </summary>
    public class md3d2:Form
    {
        private System.Windows.Forms.PictureBox pictureBox1;

        public md3d2():base()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();

            this.pictureBox1.Location = new System.Drawing.Point(8, 8);
            this.pictureBox1.Name = "pictureBox2";
            this.pictureBox1.Size = new System.Drawing.Size(240, 184);
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;


            this.Controls.Add(this.pictureBox1);
            this.ResumeLayout(false);

            this.MinimumSize=new Size(80,60);
            this.ClientSize = new Size(300,300);
            this.Text ="Direct3D-My";
        }

        private Device device_;
        private PresentParameters presentParam_;


        /// <summary>
        /// 立方体メッシュ
        /// </summary>
        private Mesh boxMesh_;

        /// <summary>
        /// Direct3Dの初期化を行います。
        /// </summary>
        /// <returns>初期化が成功したかどうか</returns>
        public bool DXInitialize()
        {
            try
            {
                presentParam_ = new PresentParameters();

                presentParam_.Windowed =true;
                presentParam_.SwapEffect = SwapEffect.Discard;


                device_ = new Device(0,DeviceType.Hardware,this
                    ,CreateFlags.HardwareVertexProcessing,presentParam_);


                //メッシュを作成
                creatMesh();

                return true;
            }
            catch
            {
                return false;
            }

        }


        /// <summary>
        /// メッシュを作成する
        /// </summary>
        private void creatMesh()
        {
            boxMesh_ = Mesh.Box(device_,2,2,2);
        }

        public void Render()
        {
            if(device_==null)return;
            if(this.WindowState ==FormWindowState.Minimized)return;

            //回転と移動を行う
            device_.Transform.World =
                Matrix.RotationY(Environment.TickCount/600f);


            //カメラの設定を行う
            device_.Transform.View = Matrix.LookAtLH(
                new Vector3( 5.0f, 5.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,
                (float)this.ClientSize.Width / (float)this.ClientSize.Height,
                3.0f, 15.0f);

            //device_.RenderState.Lighting =false;
            //ライトの設定
            device_.Lights[0].Direction =Vector3.Normalize(new Vector3(-1,-2,-3));
            device_.Lights[0].Type = LightType.Directional;

            device_.Lights[0].Diffuse = Color.White;
            device_.Lights[0].Ambient = Color.FromArgb(255,40,40,40);

            device_.Lights[0].Enabled =true;
            device_.Lights[0].Update();



            //マテリアルの設定
            Material mat =new Material();
            mat.AmbientColor = new ColorValue(1.0f,1.0f,0.1f);
            mat.DiffuseColor = new ColorValue(1.0f,1.0f,0.1f);

            device_.Material = mat;

            device_.Clear(ClearFlags.Target,Color.Blue,1.0f,0);

            device_.BeginScene();

            boxMesh_.DrawSubset(0);

            device_.EndScene();

            try
            {
                //更新
                device_.Present();
                device_.Present(this.pictureBox1);
            }
            catch(DeviceLostException)
            {
                resetDevice();

            }

        }



        /// <summary>
        /// デバイスのリセットを行う
        /// </summary>
        private void resetDevice()
        {
            int result;
            if(!device_.CheckCooperativeLevel(out result))
            {
                if(result ==(int)ResultCode.DeviceLost)
                {
                    //ちょっと待つ
                    System.Threading.Thread.Sleep(10);
                }
                else if(result ==(int)ResultCode.DeviceNotReset)
                {
                    device_.Reset(presentParam_);
                }
            }
        }
    }

    /// <summary>
    /// エントリクラス
    /// </summary>
    class Program
    {
        public static void Main()
        {
            using(md3d2 dxform =new md3d2())
            {
                if(!dxform.DXInitialize())
                {
                    MessageBox.Show("Diret3Dの初期化に失敗しました。"
                        ,"初期化の失敗");
                    return;
                }
                dxform.Show();

                while(dxform.Created)
                {
                    dxform.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}

重要なところ

device_.Present();
device_.Present(this.pictureBox1);

2回Presentを呼び出しています。もちろん、「device_.Present();」をコメントアウトすれば、pictureBox1にだけ描画されます。


ページの一番上へ
前のページへ 一覧に戻る 次のページへ
初版2006-4-2
[Atelier Blue アトリエブルー]HomeプログラミングManaged DirectX3D>描画先を指定する(複数コントロールへの描画)